0
I want to execute this again mean after the one task the program ask do u want more calculation how can I do In this program
#include <stdio.h> int main() { int a,b; char operand; printf("Please choose the operand (+,-,*,/):"); scanf("%c",&operand); printf("Enter the value of A and B"); scanf("%d %d",&a,&b); switch(operand) { case '+': printf("%d+%d=%d",a,b,a+b); break; case'-': printf("%d-%d=%d",a,b,a-b); break; case'*': printf("%d*%d=%d",a,b,a*b); break; case'/': printf("%d/%d=%d",a,b,a/b); break; default: printf("Error"); } return 0; }
6 ответов
+ 1
just put main() before return 0 and you gat what you want, or use a loop which is a better option
+ 1
Why u take 1 in the while loop?
+ 1
You know in C/C++ any non zero integral value is considered a boolean true, passing in 1 is actually telling it to repeat forever, but we trap the loop with the confirmation of <again> at the bottom, the loop is broken if input for <again> neither equal to 'Y' nor 'y'.
+ 1
You can also choose to use `do...while` loop, I think it makes more sense somehow. Because `do...while` loop runs at least once, before the loop condition is evaluated.
0
Hey asterisk I don't get the point?
0
Here's a picture of how you can do it with a loop. Hope this clears your doubt.
int main()
{
int a, b;
char operand, again; // add <again>
while (1)
{
// code to read operation here (+ - * / etc)
// code to read 2 numbers here
// code to calculate here (switch)
// ask whether to repeat again
printf("\nDo you want to calculate again? ");
scanf(" %c", &again);
if (again != 'Y' && again != 'y')
break;
}
return 0;
}