user13212283
user13212283

Reputation:

Next printf comes before my scanf takes input

I am creating a calculator file in C with a while loop and a switch statement. The first time through the while loop, everything works fine, but when it goes through the second time, the my printf gets called before I have the opportunity to enter the data into the preceding scanf.

I have tried using '\n' before the text in the printf and I have also tried using fflush(stdout) before and after the scanf call.

Current output:

Welcome to the Calculator  
Operation choices:  Addition(A)
                    Subtraction(S)
                    Multiplication(M)
                    Division(D).  
Enter choice: A  
Enter both numbers in required sequence: 50 50  
// the output of the calculator does <>= 100 // Equal to 100.   
Welcome to the Calculator  
Operation choices:  Addition(A)
                    Subtraction(S)
                    Multiplication(M)
                    Division(D).  
Enter choice: Enter both numbers in required sequence:  

What I want:

Welcome to the Calculator  
Operation choices:  Addition(A)
            Subtraction(S)
            Multiplication(M)
            Division(D).  

Enter choice: A 

Enter both numbers in required sequence: 50 50  
// the output of the calculator does <>= 100 // Equal to 100.   
Welcome to the Calculator  
Operation choices:  Addition(A)
            Subtraction(S)
            Multiplication(M)
            Division(D).  
Enter choice: // then I can enter a new choice for the switch //

The code I've tried:

while(input != 'q'){
    printf("Welcome to the Calculator\nOperation choices:\tAddition(A)\n\t\t\tSubtraction(S)\n\t\t\tMultiplication(M)\n\t\t\tDivision(D)\nEnter choice: ");
    fflush(stdout);
    scanf("%c", &input);
    fflush(stdout);
    printf("\nEnter both numbers in required sequence: ");
    scanf("%f %f", &num1, &num2);

    switch(input){
    case 'A':
      result = num1 + num2;
      break;
    case 'S':
      result = num1 - num2;
      break;
    case 'M':
      result = num1 * num2;
      break;
    case 'D':
      result = num1 / num2;
      break;
    default:
      printf("Please choose a valid operation.");
      break;
    }

    if(result > 100){
      printf("Greater than 100.\n");
    }
    else if(result < 100) {
      printf("Less than 100.\n");
    }
    else{
      printf("Equal to 100.\n");
    }
  }
  printf("Quit the menu.\n");
  return(0);
}

Upvotes: 1

Views: 199

Answers (2)

hanie
hanie

Reputation: 1885

Try this : scanf(" %c", &input); (add space before %c)

scanf will likely take \n in buffer as input and placed in you character input.

Upvotes: 1

anastaciu
anastaciu

Reputation: 23802

The sequence of events in you program is correct, what happens is that scanf() reads a lingering '\n' new line character that is left in the stdin buffer from a previous input. The '\n' is consumed by scanf() and the program continues the execution.

You will need to clear the buffer before scanf() is executed.

Option 1 - Clear the buffer at the bottom of the while cycle:

//...
int c;
//...
else{
  printf("Equal to 100.\n");
}
while((c = fgetc(stdin)) != '\n' && c != EOF){}
//...

Option 2 (simpler) - Use a space before %c specifier:

scanf(" %c", &input);
       ^

Upvotes: 1

Related Questions