Shizuka Kamishima avatar Shizuka Kamishima committed 67dfaf9 Draft

ex24 - input/output

Comments (0)

Files changed (1)

+#include <stdio.h>
+#include "dbg.h"
+
+#define MAX_DATA 100
+
+typedef enum EyeColor {
+    BLUE_EYES, GREEN_EYES, BROWN_EYES,
+    BLACK_EYES, OTHER_EYES
+} EyeColor;
+
+const char *EYE_COLOR_NAMES[] = {
+    "Blue", "Green", "Brown", "Black", "Other"
+};
+
+typedef struct Person {
+    int age;
+    char first_name[MAX_DATA];
+    char last_name[MAX_DATA];
+    EyeColor eyes;
+    float income;
+} Person;
+
+int main(int argc, char *argv[]) {
+    Person you = {.age = 0};
+    int i = 0;
+    char *in = NULL;
+
+    printf("What's your first name? ");
+    in = fgets(you.first_name, MAX_DATA-1, stdin);
+    check(in != NULL, "Failed to read first name");
+
+    printf("What's your last name? ");
+    in = fgets(you.last_name, MAX_DATA-1, stdin);
+    check(in != NULL, "Failed to read last name");
+
+    printf("How old are you? ");
+    int rc = fscanf(stdin, "%d", &you.age);
+    check(rc > 0, "You have to enter a number");
+
+    printf("What color are your eyes:\n");
+    for (i = 0; i <= OTHER_EYES; i++) {
+        printf("%d) %s\n", i+1, EYE_COLOR_NAMES[i]);
+    }
+    printf("> ");
+    int eyes = -1;
+    rc = fscanf(stdin, "%d", &eyes);
+    check(rc > 0, "You have to enter a number");
+    you.eyes = eyes - 1;
+    check(you.eyes <= OTHER_EYES && you.eyes >= 0, "Do it right, that's not an option");
+
+    printf("How much do you make an hour? ");
+    rc = fscanf(stdin, "%f", &you.income);
+    check(rc > 0, "You have to enter a floating point number");
+
+    printf("---RESULTS---\n");
+    printf("First name: %sLast name:  %sAge:        %d\nEyes:       %s\nIncome:     %f\n",
+            you.first_name, you.last_name, you.age, EYE_COLOR_NAMES[you.eyes], you.income);
+
+    return 0;
+
+error:
+    return 1;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.