From 340b1f207d9d20602e2f59dad70db90ebad8bdf7 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 6 Nov 2011 17:04:46 -0500 Subject: [PATCH] Using atoi and strtod instead of fscanf --- ex24.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ex24.c b/ex24.c index 5f4033d..67b4dd6 100644 --- a/ex24.c +++ b/ex24.c @@ -1,3 +1,4 @@ +#include #include #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);