codingisfun
codingisfun

Reputation: 47

while loop keeps running even when the condition is false

In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E".

The problem is even after selecting upper or lower case "E", my program keeps running. I am using the "i" variable as the condition for my while loop. I initialized the variable to 2, for example, and set my while loop to 2 which means the condition is true and the while loop will keep running. I changed my "i" variable to 3 for example only when upper or lower case "E" is pressed. This according to my thinking should make the loop false and essentially not run the loop anymore but my loop keeps running

#include<stdio.h>

int main()
{
    char selection;
    float length, width, area, base, height, apothem, side;
    int i=2;
    while (i=2)
    {
    printf("Press R to calculate the area of a rectangle\nPress T to calculate the area of a right angled triangle\nPress M to calculate the area of a polygon\nPress E to exit the program\n");
    scanf(" %c", &selection);
    switch (selection)
    {
    case 'R':
    case 'r':
        printf("Enter the length of the rectangle\n");
        scanf("%f", &length);
        printf("Enter the width of the rectangle\n");
        scanf("%f", &width);
        area=length*width;
        printf("The area of the rectangle is %f\n", area);
        break;
    case 'T':
    case 't':
        printf("Enter the base of the triangle\n");
        scanf("%f", &base);
        printf("Enter the height of the triangle\n");
        scanf("%f", &height);
        area=(0.5)*base*height;
        printf("The area of the triangle is %f\n", area);
        break;
    case 'M':
    case 'm':
        printf("Enter the length of one side of the polygon\n");
        scanf("%f", &length);
        printf("Enter the apothem of the polygon\n");
        scanf("%f", &apothem);
        printf("Enter the number of sides of the polygon\n");
        scanf("%f", &side);
        area=0.5*length*side*apothem;
        printf("The area of the polygon is %f\n", area);
        break;
    case 'E':
    case 'e':
        printf("You are exiting the program\n");
        i=3;
        break;
    default:
        printf("You have selected an invalid input\n");
        break;
    }
    }
    return 0;
}

Upvotes: 0

Views: 1955

Answers (2)

Bryz
Bryz

Reputation: 61

your original code can work but you just have to change the sign = to == in the while condition.

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

The program has undefined behavior because in the condition of the first while loop there is used uninitialized variable selection

char selection;
float length, width, area, base, height, apothem, side;
while (!(selection == 'R' || selection == 'r') && !(selection == 'T' || selection == 't') && !(selection == 'M' || selection == 'm') && !(selection == 'E' || selection == 'e'))

You have to initialize the variable selection before the loop. You could do it for example the following way

char selection = '\0';

And the condition of the while loop should have only this expression

while (!(selection == 'E' || selection == 'e') )

All other enetered values are checked within the switch statement.

And instead of this call

scanf("%c", &selection);

use

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

Otherwise also white space characters as for example the new line character '\n' also will be inputted.

And remove these redundant calls

getchar();

Upvotes: 1

Related Questions