Hi every one I am wrting a little bs program just to practice c and this is a calorie counter that will get bigger but I am just starting with it to get the practice. Here is the code and I sperated the prolem code below when I get a value via scanf it skips the next fget.
Code:
#include<stdio.h>
#include<stdbool.h>
#include <string.h>
int maxAday = 2500;
int todaysCount = 0;
bool stop = false;
bool firstrun;
main()
{
int dowhat;
char ate[100];
int calIn = 0;
char yesno[10];
printf("What would you like to do? \n");
//printf("Add Calories use 0: \n Calculate Day 'cd'\n Calculate Month 'cm'\n");
printf("Type 0 to get started.");
scanf("%d", &dowhat);
switch (dowhat)
{
case 0:
do
{
if (firstrun)
{
printf("Type what you ate if or manual to input the amount of calories you can also type help for a list of supported options.\n");
}
fgets(ate,100,stdin);
*(&todaysCount) += returncals(ate);
if (firstrun)
{
printf("Okay we are at todays count of %d.\n", * (&todaysCount));
printf("Add more? yes or no\n");
fgets(yesno,10,stdin);
}
firstrun = true;
if (strcmp("yes\n", yesno) == 0){
stop = false;
}
if (strcmp("no\n", yesno) == 0){
stop = true;
}
} while(!stop) ;
break;
/*Will build onto
case cd:
break;
case cm:
break;
case default:
printf("I am sorry I did not understand");
break;*/
}
}
int returncals(char food[100])
{
int cal;
if (strcmp("manual\n", food) == 0)
{
printf("How many calories are in the food?\n");
scanf("%d", &cal);
}
else if (strcmp("banana\n", food) == 0)
{
cal = 105;
}
else
{
cal = 0;
}
return cal;
}
Here is the problem code.
Once I run scanf it completly skips the following fgets.
Code:
fgets(yesno,10,stdin);
The scanf gets a calorie value and goes back main and asks if the user wants to start the loop over but the loop starts over anyway and it is skipped. here is a image.
Does anyone know why it does this?