Hello, I have a MacBook Pro 2011(i5). Right now I am learning to program in C with a book and i have a problem with the function getchar().
In the program below (if everything is correct) it seems as if option=getchar() is skipped when i get to execute...the program is the one below. Thank you very much, I hope you guys help me out!
In the program below (if everything is correct) it seems as if option=getchar() is skipped when i get to execute...the program is the one below. Thank you very much, I hope you guys help me out!
Code:
main()
{
int speed, distance;
char option;
float time;
printf("This program calculates how much time it takes to go at a certain speed over a distance \n");
printf("Introduce distance: \n");
scanf("%d", &distance);
printf("Introduce velocity: \n");
scanf("%d", &speed);
time=distance/speed;
printf("The answer is %f seconds \n", time);
printf("Want to try again (y/n)?\n");
option=getchar();
if (option=='y')
{
printf("Introduce distance: \n");
scanf("%d", &distance);
printf("Introduce speed: \n");
scanf("%d", &speed);
time=distance/speed;
printf("The answer is %f seconds \n", time);
}
else
{
printf("Thanks.");
}
}
Last edited by a moderator: