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

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.
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 !=...
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...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
***This is the only information I am given. **** Week 2 Date Transaction description 9 Purchased...
***This is the only information I am given. **** Week 2 Date Transaction description 9 Purchased 14 Specialist Tennis Raquets from Addax Sports for $232 each, terms net 30. 10 Mick's Sporting Goods paid the full amount owing on their account. Since Mick's Sporting Goods has been a loyal customer from the day the business commenced, a 10% discount was given for this early repayment. 11 Paid the full amount owing to Sports 'R Us, Check No. 873. Payment fell...
Hi! I am taking a managerial accounting class this semester. the instruction for the journal entry...
Hi! I am taking a managerial accounting class this semester. the instruction for the journal entry follow: "Record the entry for Overhead costs applied to Work in Process Inventory." I am not sure how to calculate the numbers for this post?
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT