0

Why won't my code work- I don't understand! I read the other question that this was the "duplicate" of and understand the situation with "scanf". But in this situation, I provided a switch statement with a case where the variable would be white space, in which it would just exit the loop. This wouldn't cause problems as the loop will continue as long as the user doesn't input anything - it would just go through the loop 2 times (one for the user input, 1 for the whitespace).

#include <stdio.h>

int main(void) {

    setbuf(stdout, 0);
    int op = 0;

    do{
        int num1;
        int num2;

        printf("\n\nMenu of operations\n1. + \n2. - \n3. * \n4. / \n5. %%");
        printf("\nEnter operation number to be executed: ");

        op = getchar();

        printf ("\nEnter first integer: ");
        scanf ("%d", &num1);

        printf ("Enter second integer: ");
        scanf ("%d", &num2);

        switch (op){
        case '1':
            printf ("\n%d + %d = %d", num1, num2, num1 + num2);
            break;

        case '2':
            printf ("\n%d - %d = %d", num1, num2, num1 - num2);
            break;

        case '3':
            printf ("\n%d * %d = %d", num1, num2, num1 * num2);
            break;

        case '4':
            printf ("\n%d / %d = %f", num1, num2, (float)num1/num2);
            break;

        case '5':
            printf ("\n%d %% %d = %d", num1, num2, num1%num2);
            break;

        case '\n':
        case '\t':
        case ' ' :
            break;

        default:
            printf ("An invalid menu option");
            break;
        }



    }while (op!=0);

    return 0;
}

And the results are like this:

    Menu of operations
1. + 
2. -
3. *
4. / 
5. %
Enter operation number to be executed: 4 //first time around- works fine

Enter first integer: 28
Enter second integer: 5

28 / 5 = 5.600000 

Menu of operations
1. + 
2. -
3. * 
4. / 
5. %

Menu of operations
1. + 
2. - 
3. * 
4. /
5. %
Enter operation number to be executed:
Enter first integer: 4 //so one cannot type after the statement "Enter operation number to be executed:" 

I understand that there's still a '\n' character in my buffer due to the enter which gets assigned to the op variable in the getchar() statement. But if that is the case, then the switch statement should take care of this- to be specific it should go to the case where the op variable is equal to '\n' or alternatively '\t', ' ' which I have written in my code.

        case '\n':
        case '\t':
        case ' ' :
            break;

This was how our lecturer taught us, but why won't this work in this case?

I found one more error I got that could possibly help find the source of this problem...

When I carry on with the second time inputting num1 and num2(where the 1st problem lies of not allowing me to input the variable op), the program gives this:

Enter operation number to be executed: 

Enter first integer: 2 Enter second integer: 4 You may ask why that's a second problem, but in my code, I stated that if op was anything else other than 1,2,3,4,5,\n,\t, ' ', it should run what is under default and print "An invalid menu option."

However, it does neither of these things. Does anyone have an idea of what's going on?

Also, I tried doing what someone suggested in a comment- so I edited my code like this:

      while (getchar() != '\n')
  {
      op = getchar();
  }

However, this still doesn't solve the problem... I get the exact same result. Since the newline/enter isn't the problem here, does anyone know what is?

breakstorm
  • 9
  • 1
  • 7
  • This didn't, And it sort of annoyed me that someone said this was a duplicate, cos now I can't get answers. Thankyou "very" much >:/ – breakstorm Aug 12 '15 at 10:47

1 Answers1

1

The problem occurs here, as you're not cleaning the input buffer after completion of the first input value.

To elaborate, the ENTER key press used to complete the input in case of the second integer (i.e., scanning the num2 value), the integer value is scanned, but the newline (\n) due to the ENTER remains in the buffer, which is being read by the next getchar() call.

Solution: Before going back to the top of loop, eat up anything spare in the input buffer. You can use something like (pseudo-code)

while (getchar() != `\n`);

at the end of the loop to clean the input buffer.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thankyou so much for explaining what was wrong to me! I understand the problem now, but this was something I tried to resolve in the code by adding the case where the character would be a newline, in which it would just exit the loop. Because the variable "op" would not be zero, I thought it would skip any enter characters (as it would just go to the statement "break;" then) and wait for the character to be entered by the user after the character "enter" is analysed. Why does this not happen here, if I may ask? :D I appreciate your help so much!! – breakstorm Aug 10 '15 at 15:50
  • @breakstorm hint: the condition check is done _after_ the loop body. :-) – Sourav Ghosh Aug 10 '15 at 16:06
  • Oh right xD That makes sense. – breakstorm Aug 10 '15 at 16:31
  • But one last thing- so sorry to bother you x( Ugh... If I went "case '\n':" as in my code, wouldn't that part of the switch statement run for when it's enter? This was what our lecturer taught us, so I don't know why that part won't work ... I'll comment on which part I mean in my original question so it's easy to see >_ – breakstorm Aug 10 '15 at 16:38
  • UPDATE: No one commented on my question, so I just decided to do the while thing as you suggested. But it still doesn't work :'( – breakstorm Aug 12 '15 at 10:43