1

I am trying to program a simple calculator.
Here is my code first:

#include <stdio.h>
#include <stdlib.h>

int main(void) { 

char operator = 0;
float num1 = 0.0;
float num2 = 0.0;
float sol = 0.0;


while (operator != 'q') {

    printf("Operator: ");
    scanf("%c", &operator);
    printf("First Number: ");
    scanf("%f", &num1);
    printf("Second Number: ");
    scanf("%f", &num2);

    switch (operator)
    {
    case '+': sol = num1 + num2; break;
    case '-': sol = num1 - num2; break;
    case '*': sol = num1 * num2; break;
    case '/': sol = num1 / num2; break;
    case 'q': printf("Finished!"); exit(0);
    default: printf("Error!"); exit(0);
    }

    printf("The solution is: %.2f\n\n", sol); 
}

return 0;
}

So for me the code is fine. As you can see I did this with a while loop that lets you calculate until you type in 'q' as operator. The first run of the loop works fine but then it gets creepy (my console):

Operator: +
First Number: 5
Second Number: 4
The solution is: 9.00

Operator: First Number:

Why does the program not let me enter an operator in the second loop run?

1
  • When you handle an error, don't just say "Error!". You need to generate as much information as possible. printf ("Error! Didn't expect operator %c (%d)\n", operator, operator); What's happening is you're capturing the newline at the end of the first line of input.
    – QuestionC
    Nov 10, 2015 at 15:53

2 Answers 2

6

Most format specifiers with scanf will skip leading whitespace. %c does not.
scanf("%f", &num2); at the end of the first iteration leaves a newline in the input buffer.
scanf("%c", &operator); at the start of the second iteration, reads the newline and proceeds.
using a space before %c in scanf(" %c", &operator); will allow %c to skip the leading whitespace and capture the operator.

0

You should check scanf for errors:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char operator = 0;
    float num1 = 0.0;
    float num2 = 0.0;
    float sol = 0.0;

    while (operator != 'q') {

        printf("Operator: ");
        if((scanf(" %c", &operator)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        printf("First Number: ");
        if((scanf("%f", &num1)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        printf("Second Number: ");
        if((scanf("%f", &num2)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        switch (operator){
            case '+': sol = num1 + num2; break;
            case '-': sol = num1 - num2; break;
            case '*': sol = num1 * num2; break;
            case '/': sol = num1 / num2; break;
            case 'q': printf("Finished!"); exit(0);
            default: printf("Error!"); exit(0);
        }

        printf("The solution is: %.2f\n\n", sol);
    }

    return 0;
}

and as you can see I changed scanf("%c", &operator); to this scanf(" %c", &operator); to make scanf to ignore (skip) Whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.