Using atoi and strtod instead of fscanf

This commit is contained in:
Dan Buch 2011-11-06 17:04:46 -05:00
parent 74e3d00e44
commit 340b1f207d

16
ex24.c
View File

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <stdio.h>
#include "dbg.h"
@ -26,6 +27,7 @@ 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);
@ -36,7 +38,10 @@ int main(int argc, char *argv[])
check(in != NULL, "Failed to read last name.");
printf("How old are you? ");
fscanf(stdin, "%d", &you.age);
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++) {
@ -45,12 +50,17 @@ int main(int argc, char *argv[])
printf("> ");
int eyes = -1;
fscanf(stdin, "%d", &eyes);
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? ");
fscanf(stdin, "%f", &you.income);
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);