Reputation: 33
I have a problem where I need to write a menu driven program using function pointers in C. The user picks a choice 1-3, and addition is executed if response = 1, subtract if response = 2, multiplication if response =3.
The first run through is perfect, but once I loop and try to do a second calculation, it searches for the choice AND the first number before it displays what choice you have made.
The math in each function works, passing the variables work, just the printf and scanf statements are out of order after the first run through.
(There are subtraction and multiplication functions, they are exactly the same, except instead of the "+" operator there is the "-" and "*" operator, respectively.)
I searched an issue similar to this and tried fflush and setvbuf commands, these did not work.
void addition(int num1, int num2);
void subtraction(int num1, int num2);
void multiplication(int num1, int num2);
int main(void) {
void(*m[3])(int, int) = { addition, subtraction, multiplication };
size_t choice;
int num1, num2;
printf_s("Would like to add, subtract, or multiply?\nType 1 for
addition, 2 for subtraction, 3 for multiplication.\n");
scanf_s("%d", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
if (choice >= 1 && choice <= 3) {
(*m[choice - 1])(num1, num2);
while (choice >= 1 && choice <= 3) {
printf_s("Would like to add, subtract, or multiply?\nType 1
for addition, 2 for subtraction, 3 for multiplication.\n");
choice = 0;
scanf_s("%d\n", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
(*m[choice - 1])(num1, num2);
}
printf("execution complete");
}
return 0;
}
void addition(int num1, int num2) {
int i = 0;
i = num1 + num2;
printf("%d + %d = %d\n", num1, num2, i);
}
If I input the following: 1 2 3, 2 3 1, i expect the output to be
"1"
"What two numbers would you like to work with?"
"2"
"3"
"2+3=5"
"would you like to add, subtract, or multiply?"
"Type 1 (...) for multiplication"
"2"
"what two numbers (...)"
"3"
"1"
"3-1 =2"
However I get this:
"1"
"What two numbers would you like to work with?"
"2"
"3"
"2+3=5"
"would you like to add, subtract, or multiply?"
"Type 1 (...) for multiplication"
"2"
"3"
"what two numbers (...)"
"1"
"3-1 =2"
As you can see, the math is right, but the second time around it required 2 numbers before it asks for the 2 numbers. I don't see how this is possible as there is only one variable being scanned before the following printf statement is executed. Even though the "3" i multiplied was saved in num1, it scanned before the printf, even though the statement is after the printf so this shouldn't be possible. I'm so confused!
Upvotes: 3
Views: 495
Reputation: 153602
Remove '\n'
.
scanf_s("%d\n"...
will not return until non-white-space is entered after the number.
// scanf_s("%d\n", &choice);
scanf_s("%d", &choice);
Other problems may exist too.
Upvotes: 1
Reputation: 2479
Use scanf_s("%d\n",&var);
doing so you 'force' the scanf to read the new line (enter) character inserted when you 'commit' your data entry. Otherweise that character will reamin in stream and affect the next scanf and, being that character an enter, will make your code go further.
Upvotes: 0
Reputation: 114
I would do it like this :
int main(void) {
void(*m[3])(int, int) = { addition, subtraction, multiplication };
size_t choice = 1;
int num1, num2;
while (choice >= 1 && choice <= 3) {
printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
choice = 0;
scanf_s("%d", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
(*m[choice - 1])(num1, num2);
}
return 0;
}
Your problem was - you had the scanf_s("%d\n", &choice);
- you should do it without \n - scanf_s("%d", &choice);
and the loop you can do much easier.
Remove the if and set choice to 1.
Upvotes: 2
Reputation: 32596
why that so long and complicated code ?
just replace
printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
scanf_s("%d", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
if (choice >= 1 && choice <= 3) {
(*m[choice - 1])(num1, num2);
while (choice >= 1 && choice <= 3) {
printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
choice = 0;
scanf_s("%d\n", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
(*m[choice - 1])(num1, num2);
}
printf("execution complete");
}
by
for (;;) {
printf("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
if (scanf("%d", &choice) != 1) {
puts("abort");
break;
}
if ((choice < 1) || (choice > 3))
break;
printf("what two numbers would you like to work with?\n");
if (scanf("%d %d", &num1, &num2) != 2) {
puts("abort");
break;
}
(*m[choice - 1])(num1, num2);
}
printf("execution complete");
Upvotes: 2