Question: This is the output i get when i run the code, but it only does it once I have posted my code below. Every time


This is the output i get when i run the code, but it only does it once
I have posted my code below. Every time i execute the code it only does it once, and not five times like its supposed to, what am i doing wrong ?
// my hw01q2_2 code
#include
int main()
{
char ch;
int a=10;
int b=20;
printf("Enter math operation: ");
scanf("%c",&ch);
switch(ch)
{
case '+':
{
printf("ch=%c ",ch);
printf("f=%d ",a+b);
break;
}
case '-':
{
printf("ch=%c ",ch);
printf("f=%d ",a-b);
break;
}
case '*':
{
printf("ch=%c ",ch);
printf("f=%d ",a*b);
break;
}
case '/':
{
printf("ch=%c ",ch);
printf("f=%f ",(float)a/b);
break;
}
default:
printf("Invalid operator ");
}
return 0;
}
2.2 Edit the code above or write a new code to use a for-loop (that runs 5 times) to ask the user for a math operation as input ( +, -, *,/). Use an input statement such as ch = getchar(); or scanf("%c ", &ch); to replace the assignment statement ch = '+' in the code above. Expected result shown in figure below. Submit the revised program as hw0192_2.c. [8 points] Enter math operation: + ch = + f = 30 Enter math operation: - ch = - f = -10 Enter math operation: / ch = / f = 0.5 Enter math operation: * ch = * f = 200 Enter math operation: $ ch = $ invalid operator kbagewad@general2:/cse240$ Note, when you add the loop, you may need to use a fflush(stdin) or a getchar() to flush the newline In' character left behind by a previous operation;. You can use one ch = getchar() at the beginning of the loop and one before the end of the loop: for (...) printf("please enter a char"); ch = getchar(); other code; ch = getchar(); This line will flush the ' ' character left behind by the previous getchar(). \/* This C program demonstrates the switch statement without using breaks. */ #include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
