Ex.24 Extra Credit (use scanf only with atoi)

I was able to complete the extra credit challenge without using atoi() anywhere. Is this problematic in the long run or is my compiler doing something here?

  1 #include <stdio.h>
  2 #include "dbg.h"
  3 #include <stdlib.h>
  4 
  5 #define MAX_DATA 100
  6 
  7 // provides corresponding int values for the const char *EYE_COLOR_NAMES[]
  8 // changing the order of these will fuck up the order of the color names
  9 typedef enum EyeColor {
 10         BLUE_EYES,
 11         GREEN_EYES,
 12         BROWN_EYES,
 13         BLACK_EYES,
 14         OTHER_EYES
 15 } EyeColor;
 16 
 17 // seems like a hash table or a dict would be better
 18 const char *EYE_COLOR_NAMES[] = {
 19         "Blue",
 20         "Green",
 21         "Brown",
 22         "Black",
 23         "Other"
 24 };
 25 
 26 typedef struct Person {
 27         int age;
 28         char first_name[MAX_DATA];
 29         char last_name[MAX_DATA];
 30         EyeColor eyes;
 31         float income;
 32 } Person;
 33 
 34 int main(int argc, char* argv[]) {
 35         Person you = {.age = 0};
 36         int i = 0;
 37         char *in = NULL;
 38 
 39         printf("What's your first name? ");
 40         in = fgets(you.first_name, MAX_DATA - 1, stdin);
 41         check(in != NULL, "Failed to read first name.");
 42 
 43         printf("What's your last name? ");
 44         in = fgets(you.last_name, MAX_DATA - 1, stdin);
 45         check(in != NULL, "Failed to read last name.");
 46 
 47         printf("How old are you? ");
 48         int rc = scanf("%d", &you.age);
 49         check(rc > 0, "You have to enter a number.");
 50 
 51         printf("What color are your eyes?\n");
 52         for(i = 0; i < OTHER_EYES; i++) {
 53                 printf("%d) %s\n", i + 1, EYE_COLOR_NAMES[i]);
 54         }
 55         printf("> ");
 56 
 57         int eyes = -1;
 58         rc = scanf("%d", &eyes);
 59         check(rc > 0, "You have to enters a number.");
 60 
 61         you.eyes = eyes - 1;
 62         check(you.eyes <= OTHER_EYES && you.eyes >= 0, "Do it right, that is not an option.");
 63 
 64         printf("How much do you make an hour? ");
 65         rc= scanf("%f", &you.income);
 66         check(rc > 0, "Enter a floating point number.");
 67 
 68         printf("----- RESULTS -----\n");
 69 
 70         printf("First name: %s", you.first_name);
 71         printf("last name: %s", you.last_name);
 72         printf("Age: %d\n", you.age);
 73         printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
 74         printf("Income: %.2f\n", you.income);
 75 
 76         return 0;
 77 error:
 78         return -1;
 79 }

image

1 Like

Looks fine to me.

What do you mean?

The extra credit says to use atoi, but I didn’t need to and it worked. I was just wondering if the text is wrong or something had changed in the compiler since the book was written.

Ah, I see. No, what you’re doing is fine. I think it’s the usual way to do this.
If you can, you should avoid atoi anyway because it’s unsafe (the usual problem with non null-terminated C-strings).

Thank you once again lol. I think you should get paid for this. :wink:

A free service run by Zed A. Shaw for learncodethehardway.org.