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.

This is the output i get when i run the code, but it only does it onceEvery time i execute the code it only does it once, and

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 main() { char ch = '+'; int a = 10, b = 20; double f; printf("ch = %c ", ch); switch (ch) { case '+': f = a + b; printf("f = %d ", f); case '-': f = a - b; printf("f = %d ", f); case '*': f = a + b; printf("f = %d ", f); case '/' : f = a / b; printf("f = %d ", f); default: printf("invalid operator "); ch = '-'; printf("ch = %c ", ch); switch (ch) { case '+': f = a + b; printf("f = %d ", f); case '-': f = a - b; printf("f = %d ", f); case '*': f = a * b; printf("f = %d ", f); case '/' : f = a / b; printf("f = %d ", f); default: printf("invalid operator "); ch = '*'; printf("ch = %c ", ch); switch (ch) { case '+': f = a + b; printf("f = %d ", f); case '-': f = a - b; printf("f = %d ", f); case '*': f = a + b; printf("f = %d ", f); case '/' : f = a / b; printf("f = %d ", f); default: printf("invalid operator "); } ch = '/'; printf("ch = %c ", ch); switch (ch) { case '+': f = a + b; printf("f = %d ", f); case '-': f = a - b; printf("f = %d ", f); case '*': f = a * b; printf("f = %d ", f); case '/' : f = a / b; printf("f = %d ", f); default: printf("invalid operator "); } ch = *%'; printf("ch = %c ", ch); switch (ch) { case '+': f = a + b; printf("f = %d ", f); case '-': f = a - b; printf("f = %d ", f); case '*': f = a + b; printf("f = %d ", f); case '/': f = a / b; printf("f = %d ", f); default: printf("invalid operator "); } [mzamor11@general3:~/CSE240/d3$ ls hw0192_1 hw0192_1.Chw0192_2 hw0192_2.c hw01q3answer hw01q3answer.c [mzamor11@general3:~/CSE240/d3$ ./hw01q2_2 Enter math operation: ch=/ f=0.500000 mzamor11@genera13:/CSE240/d3$

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!