box-o-sand/lcthw-remnants/ex24.c
Dan Buch f2380eef05 Add 'lcthw-remnants/' from commit 'e172f73c8297b22a579c94558f0c171ca74a0e5c'
git-subtree-dir: lcthw-remnants
git-subtree-mainline: 4107485591
git-subtree-split: e172f73c82
2013-01-09 23:42:19 -05:00

77 lines
1.9 KiB
C

#include <stdlib.h>
#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;
char tmp[MAX_DATA];
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? ");
in = fgets(tmp, MAX_DATA-1, stdin);
check(in != NULL, "Failed to read age.");
you.age = atoi(tmp);
check(you.age != -1, "Failed to convert age to int.");
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;
in = fgets(tmp, MAX_DATA-1, stdin);
check(in != NULL, "Failed to read eye color selection.");
eyes = atoi(tmp);
check(eyes <= OTHER_EYES && eyes > 0, "Do it right, that's not an option.");
you.eyes = eyes - 1;
printf("How much do you make an hour? ");
in = fgets(tmp, MAX_DATA-1, stdin);
check(in != NULL, "Failed to read income.");
you.income = strtod(tmp, NULL);
check(you.income != -1, "Failed to convert income.");
printf("----- RESULTS -----\n");
printf("First Name: %s", you.first_name);
printf("Last Name: %s", you.last_name);
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;
}