Question

In: Computer Science

In the given instruction, I am to use while loops only. The goal is to prompt...

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

Solutions

Expert Solution

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:

  • You have to write while(i == 2) but you wrote as while(i = 2)
  • Double Equal to Should be there in While(i == 2)
  • This is your mistake. otherwise, your code works well!!!.

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


Related Solutions

Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Please create using only For loops, as simple as possible as I am attending Java1 Create...
Please create using only For loops, as simple as possible as I am attending Java1 Create two Java programs that do the following: a. Use a for loop to print the numbers below on a single line as shown. 1 2 4 8 16 32 64 128. b. Ask the user to enter numbers using the for loop.  First ask the user how many numbers they will be entering in. Print the sum and average of the entered numbers. c. Write...
Python: If I am given a string consisting only of parentheses - (, ), {, },...
Python: If I am given a string consisting only of parentheses - (, ), {, }, [, and ], how would I write a Boolean function that takes a string and decides if it consists of valid nested parenthesis? One of the hints given was to use stack. For example --> {()[{}]}' is valid.
C++ I am a beginner at this and have a hard time with for loops.. I...
C++ I am a beginner at this and have a hard time with for loops.. I was given this prompt. part 1. 1. Create an array to store 8 characters. 2. Using a for loop, get an 8-letter word from the user, one letter at a time. 3. Then use another for loop to print the complete word onto the screen one letter at a time. 4. Then use a third for loop to print out the word backwards. part...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
How do I change my if statements into while and for loops, so that I can...
How do I change my if statements into while and for loops, so that I can make my bot go straight and turn exactly 12 times. /*********************************************************************** * Exp7_1_RotaryEncoder -- RedBot Experiment 7_1 * * Knowing where your robot is can be very important. The RedBot supports * the use of an encoder to track the number of revolutions each wheels has * made, so you can tell not only how far each wheel has traveled but how * fast...
I am practicing questions to studying with and the prompt is to: Write statements that assign...
I am practicing questions to studying with and the prompt is to: Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 7 b) 1 ≤ n ≤ 221 c) 0 ≤ n ≤ 49 d) 1000 ≤ n ≤ 1114 e) –2 ≤ n ≤ 9 f) –13 ≤ n ≤ 21 This is what I have thus far but when it displays my fourth value that is supposed to...
This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT