Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%d", &oper)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1 num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;}
}while(select == 1);
} ```
CodePudding user response:
I think you want something like bellow code.
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
do{
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
switch(oper){
case 1:
printf("The sum is %d\n", num1 num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
printf("Press 0 to stop and 1 to continue: ");
scanf("%d", &select);
} while(select == 1);
printf("Finish");
}
CodePudding user response:
change the following code bellow: while(select != 0);
CodePudding user response:
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1 num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
}while(select != 0);
}