0
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    char op;
    float a, b;
  printf("\n Operators are ( +,-,*,/,^) \n");
        printf("Enter 0 to quit \n");
    while(1)
    {

        printf("Enter your choice: \n");
        scanf("%c", &op);



        switch(op)
            {
            case '+':
                printf(" Enter your two numbers: \n");
                scanf("%f %f", &a, &b);
                printf("%f + %f = %f", a, b, a+b);
            break;
            case '-':
                printf(" Enter your two numbers: \n");
                scanf("%f %f", &a, &b);
                printf("%f - %f = %f", a, b, a-b);
            break;
            case '*':
                printf(" Enter your two numbers: \n");
                scanf("%f %f", &a, &b);
                printf("%f x %f = %f", a, b, a*b);
            break;
            case '/':
                printf(" Enter your two numbers: \n");
                scanf("%f %f", &a, &b);
                printf("%f / %f = %f", a, b, a/b);
            break;
            case '^':
                printf(" Enter your two numbers: \n");
                scanf("%f %f", &a, &b);
                printf("%f ^ %f = %f", a, b, pow(a, b));
            break;
            case '0':
                exit(0);


        }
        }
    
     return 0;

    }

This program is basically a switch case basic calculator. After the while loop start, the program prints "Enter your choice" twice instead of once, can anyone tell how to fix this. Apart from that it works fine. The while loop is for menu driven program.

PAT
  • 1
  • 1
  • Have you used your step-through debugger? If not, why not? – Dai Oct 02 '20 at 17:23
  • I am a complete newbie so dont know about the step through debugger, how does it work? – PAT Oct 02 '20 at 17:25
  • 1
    Change `"%c"` to `" %c"` (add space before `%c`), and it will solve the problem because the space will have it ignore whitespace characters including newline characters. – MikeCAT Oct 02 '20 at 17:27
  • Thanks a lot for this fix! But can u explain more on this whitespace character thing? – PAT Oct 02 '20 at 17:30

0 Answers0