benjamin_lambe
07-05-2003, 05:44 PM
I have a question, i hope someone can answer it for me.
Q. If i leave out the code on line 11, scanf("%c", &temp); the program runs though and doesnt let you enter a choice, why is this? thanks to anyone who wishes to answer this.
#include <stdio.h>
int main()
{
char option, temp;
int num1, num2;
printf("\n Input two integers: ");
scanf("%d%d", &num1, &num2);
scanf("%c", &temp);
printf("\n Do you want to: \n");
printf("\t a) add them\n");
printf("\t b) subtract them\n");
printf("\t c) multiply them\n");
printf("\t d) divide them\n");
printf("\n Input your choice: ");
scanf("%c", &option);
switch(option)
{case 'a': printf("%d + %d = %d\n", num1, num2, num1 + num2);
break;
case 'b': printf("%d - %d = %d\n", num1, num2, num1 - num2);
break;
case 'c': printf("%d * %d = %d\n", num1, num2, num1 * num2);
break;
case 'd': printf("%d / %d = %d\n", num1, num2, num1 / num2);
break;
default : printf("invalid option\n");
}
return 0;
}
Q. If i leave out the code on line 11, scanf("%c", &temp); the program runs though and doesnt let you enter a choice, why is this? thanks to anyone who wishes to answer this.
#include <stdio.h>
int main()
{
char option, temp;
int num1, num2;
printf("\n Input two integers: ");
scanf("%d%d", &num1, &num2);
scanf("%c", &temp);
printf("\n Do you want to: \n");
printf("\t a) add them\n");
printf("\t b) subtract them\n");
printf("\t c) multiply them\n");
printf("\t d) divide them\n");
printf("\n Input your choice: ");
scanf("%c", &option);
switch(option)
{case 'a': printf("%d + %d = %d\n", num1, num2, num1 + num2);
break;
case 'b': printf("%d - %d = %d\n", num1, num2, num1 - num2);
break;
case 'c': printf("%d * %d = %d\n", num1, num2, num1 * num2);
break;
case 'd': printf("%d / %d = %d\n", num1, num2, num1 / num2);
break;
default : printf("invalid option\n");
}
return 0;
}