Wilson Almeida Wilson Almeida - 2 months ago 18
C Question

C - while loop hanging on the condition

I'm a newbie learning code so this might be pretty simple but I can't understand what is happening.

When I try to run the following code it seems to hang on the

while
loop condition and doesn't execute any code after that. Is the
!=
operator not suited for this?

I also tried a
do {} while
and everything ran fine but as soon as I set the operator to
'E'
, again the condition seems to hang there and doesn't get out of the loop.

#include <stdio.h>

int main(void) {
float n, content;
char operator;

n = content = 0;
operator = 'a';

printf("Begin Calculations:\n");

while (operator != 'E');
{
scanf("%f %c", &n, &operator);

switch (operator) {
case 's':
case 'S':
content = n;
printf("= %f\n", content);
break;

case '+':
content = content + n;
printf("= %f\n", content);
break;

case '-':
content = content - n;
printf("= %f\n", content);
break;

case '*':
content = content * n;
printf("= %f\n", content);
break;

case '/':
content = content / n;
printf("= %f\n", content);
break;

case 'e':
case 'E':
printf("End of calculations\n");
operator == 'E';
break;

default:
printf("Invalid input\n");
break;
}
}
return 0;
}

Answer

You have a classic bug in your while loop:

while (operator != 'E');
{

The ; after the condition is parsed as en empty statement, hence your while runs forever if operator is different from 'E'.

Using the classic Kernighan and Ritchie brace style makes this kind of bug obvious and much less likely to occur:

while (operator != 'E') {

Also note that you should exit your loop from the switch statement instead of testing for E in the while condition, this would allow for both e and E to be handled correctly (your statement operator == 'E'; is a no op, it should be written operator = 'E';. Also check the return value of scanf to avoid looping endlessly at end of file.

Here is an improved version:

#include <stdio.h>

int main(void) {
    float n, content;
    char operator;
    int active = 1;

    n = content = 0;

    printf("Begin Calculations:\n");

    while (active && scanf("%f %c", &n, &operator) == 2) {
        switch (operator) {
        case 's':
        case 'S':
            content = n;
            printf("= %f\n", content);
            break;

        case '+':
            content = content + n;
            printf("= %f\n", content);
            break;

        case '-':
            content = content - n;
            printf("= %f\n", content);
            break;

        case '*':
            content = content * n;
            printf("= %f\n", content);
            break;

        case '/':
            content = content / n;
            printf("= %f\n", content);
            break;

        case 'e':
        case 'E':
            printf("End of calculations\n");
            active = 0;
            break;

        default:
            printf("Invalid input\n");
            break;
        }
    }
    return 0;
}
Comments