Why does this make an infinite loop
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double radius = 0.00;
double height = 0.00;
double width = 0.00;
double side = 0.00;
double Area = 0.00;
const double PI = atan(1.0)*4;
string input = "none";
while (input != "0") {
cout << "Shape Menu: \n (r) rectangle \n (s) square \n (c) circle \n (0) exit" << endl;
cin >> input;
if (input == "r"){
cout << "Please enter a floating point value for the height of the rectangle." << endl;
cin >> height;
cout << height;
while (height <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid. Please input a floating point value greater than 0.";
cin >> height;
}
cout << "Please enter a floating point value greater than 0 for the width of the rectangle." << endl;
cin >> width;
while (width <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid";
cin >> width;
}
Area = height*width;
cout << "The area of a rectangle with those dimensions is " << Area << "units squared." << endl;
}
else if(input == "s"){
cout << "Please enter a floating point value for the length of the side." << endl;
cin >> side;
if (cin.fail())
cout << "Please enter a floating point value.";
Area = side*side;
cout << "The area of a square with those dimensions is " << Area << "units squared" << endl;
}
else if(input == "c"){
cout << "Please enter a floating point value for the length of the radius." << endl;
cin >> radius;
if (cin.fail())
cout << "Please enter a floating point value.";
Area = radius*radius*PI;
cout << "The area of a circle with those dimensions is " << Area << "units squared." << endl;
}
else if(input == "0"){
break;
}
else{
cout << "Your input does not match one of the options suggested. Please type r, s, c to get the area of a shape, or type 0 to exit." << endl;
}
开发者_JAVA技巧 }
return 0;
}
I am trying to write a program that asks the user to pick from a menu of shapes, then ask for certain inputs for each type to determine the area. The problem I am having now, is trying to figure out how to raise errors when people input answers for the radius or height and width that are not numeric. The error I was trying to write for the rectangle worked for the initial incorrect input, however once the user is prompted pick another shape, if an input error occurs again, then it begins an infinite loop.
You are using cin.clear() and cin.ignore(1000,'\n') to skip over the invalid input in the rectangle case, which is good, but you aren't doing that in the other cases.
For me, your code would go into a loop if I typed s (for square) and s again. This code doesn't; it checks inputs as it goes:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double radius = 0.00;
double height = 0.00;
double width = 0.00;
double side = 0.00;
double Area = 0.00;
const double PI = atan(1.0)*4;
string input = "none";
while (input != "0")
{
cout << "Shape Menu: \n (r) rectangle \n (s) square \n (c) circle \n (0) exit" << endl;
if (!(cin >> input))
{
cout << "Break after reading input\n";
break;
}
cout << "Input: <<" << input << ">>" << endl;
if (input == "r")
{
cout << "Please enter a floating point value for the height of the rectangle." << endl;
if (!(cin >> height))
{
cout << "Break after reading height (1)\n";
break;
}
cout << height;
while (height <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid. Please input a floating point value greater than 0.";
if (!(cin >> height))
{
cout << "Break after reading height (2)\n";
break;
}
}
cout << "Please enter a floating point value greater than 0 for the width of the rectangle." << endl;
if (!(cin >> width))
break;
while (width <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid";
if (!(cin >> width))
break;
}
Area = height*width;
cout << "The area of a rectangle with those dimensions is " << Area << " units squared." << endl;
}
else if (input == "s")
{
cout << "Please enter a floating point value for the length of the side." << endl;
if (!(cin >> side))
{
cout << "Break after reading length\n";
break;
}
if (cin.fail())
cout << "Please enter a floating point value.";
Area = side*side;
cout << "The area of a square with those dimensions is " << Area << " units squared" << endl;
}
else if (input == "c")
{
cout << "Please enter a floating point value for the length of the radius." << endl;
if (!(cin >> radius))
{
cout << "Break after reading radius\n";
break;
}
if (cin.fail())
cout << "Please enter a floating point value.";
Area = radius*radius*PI;
cout << "The area of a circle with those dimensions is " << Area << " units squared." << endl;
}
else if (input == "0")
{
cout << "Break after reading zero\n";
break;
}
else
{
cout << "Your input does not match one of the options suggested.\n"
<< "Please type r, s, c to get the area of a shape, or type 0 to exit." << endl;
}
}
return 0;
}
精彩评论