Wilson Almeida
Wilson Almeida

Reputation: 53

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;
}

Upvotes: 1

Views: 402

Answers (6)

Jawad Saadat
Jawad Saadat

Reputation: 13

couple of things. 1- operator is a reserve word, you should change the variable name 2- (semicolon ;) after while()

Check update code:

        #include <stdio.h>

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

          n = content = 0;
          operator1 = 'a';

          printf("Begin Calculations:\n");

          while(operator1 != 'E')
          {
            scanf("%c", &operator1);

            switch(operator1)
            {
              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");
            operator1 = 'E';
            break;

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

Upvotes: 0

chqrlie
chqrlie

Reputation: 144550

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;
}

Upvotes: 1

Charanjeet Singh
Charanjeet Singh

Reputation: 1

You need to remove the semi-colon from while loop because semi-colon means end of statement so your program is not executing further

Upvotes: 0

user5555929
user5555929

Reputation:

while(operator != 'E');

The correct syntax is while (...) { ... } without semicolon or do { ... } while (...); with.

printf("End of calculations\n");
operator == 'E';

The assignment operator is a single =. Change == into = and it should be fine.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59987

  1. You need to remove the semi-colon at the end of while
  2. You need to take into account both cases

So change

while(operator != 'E');

to

while(operator != 'E' && operator != 'e')

Upvotes: 2

yaba
yaba

Reputation: 911

Remove the semicolon at the end of the while statement:

while(operator != 'E')
{

The semicolon ends the body of the while statement, in other words it behaves as if you would write this:

while(operator != 'E')
{
}

causing an infinite loop.

Upvotes: 2

Related Questions