box-o-sand/lcthw-remnants-2/ex24.c

78 lines
1.8 KiB
C
Raw Normal View History

2016-04-16 02:53:06 +00:00
#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;
2016-04-16 03:05:43 +00:00
int rc = -1;
2016-04-16 02:53:06 +00:00
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.");
2016-04-16 03:05:43 +00:00
rc = sscanf(you.first_name, "%s", you.first_name);
check(rc > 0, "Failed to strip first name.");
2016-04-16 02:53:06 +00:00
printf("What's your Last Name? ");
in = fgets(you.last_name, MAX_DATA-1, stdin);
check(in != NULL, "Failed to read last name.");
2016-04-16 03:05:43 +00:00
rc = sscanf(you.last_name, "%s", you.last_name);
check(rc > 0, "Failed to strip last name.");
2016-04-16 02:53:06 +00:00
printf("How old are you? ");
2016-04-16 03:05:43 +00:00
rc = fscanf(stdin, "%d", &you.age);
2016-04-16 02:53:06 +00:00
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, "Enter a floating point number.");
printf("----- RESULTS -----\n");
2016-04-16 03:05:43 +00:00
printf("First Name: %s\n", you.first_name);
printf("Last Name: %s\n", you.last_name);
2016-04-16 02:53:06 +00:00
printf("Age: %d\n", you.age);
printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
printf("Income: %f\n", you.income);
return 0;
error:
return -1;
}