In: Computer Science
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; }
Answer:
Give me a thumbs up if you like my work !!!
I have corrected your code.
Now, it is working perfectly.
Error in your program:
I have attached the Corrected Code under the Output Screenshot.
I have also attached the Output Screenshot that I got by running the below program.
Output:
***************************************************************************************************************
Corrected Code:
#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;
}