I enter a "2" to select area of a rectangle and then input two valid numbers but after I hit enter it asks the question again instead of exiting the switch statement
#include <iostream>
#include <fstream>
//Jacob Wilt
//Geometry Calculator
usingnamespace std;
int main()
{
ofstream outfile;
outfile.open("Geometry.txt");
int x, radius, length, width, base, height;
constdouble pie = 3.14;
cout << "1. Calculate the Area of a Circle:\n2. Calculate the Area of a Rectangle:\n3. Calculate the Area of a Triangle:";
cout << "\n4. Quit:\n\nEnter your choice (1-4):\n";
cin >> x;
while (x > 0 && x < 4)
{
switch(x)
{
case 1:
{
cout << "Enter the radius of the Circle.\n";
cin >> radius;
if (radius >= 0)
{
cout << "The area of the circle is " << radius * radius * pie << "\n\n";
}
else
{
cout << "Invalid Input!!";
}
break;
}
case 2:
{
cout << "Enter the length and width of the Rectangle.\n";
cin >> length >> width;
if (length >= 0 && width >= 0)
{
cout << "The area of the Rectangle is " << length * width << "\n\n";
break;
}
else
{
cout << "Invalid Input!!";
}
break;
}
case 3:
{
cout << "Enter the base and height of the Triangle.\n";
cin >> base >> height;
if (base >= 0 && height >= 0)
{
cout << "The area of the Triangle is " << base * height * .5 << "\n\n"; }
else
{
cout << "Invalid Input!!";
}
break;
}
cout << "1. Calculate the Area of a Circle:\n2. Calculate the Area of a Rectangle:\n3. Calculate the Area of a Triangle:";
cout << "\n4. Quit:\n\nEnter your choice (1-4):";
cin >> x;
}
}
return 0;
}
Then, once you enter the switch control structure, you provide input.
Then, you break out of the switch control structure.
Then, lines 62 - 64 ask you again.
yeah that's what I want to happen, but I'm stuck in "case 2:" and it asks the question in there repeatedly instead of breaking and displaying the menu again.