为什么这会形成一个无限循环
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;
}
}
return 0;
}
我正在尝试编写一个程序,要求用户从形状菜单中选择,然后要求每种类型的特定输入以确定面积。我现在的问题,是试图找出如何提高错误当人们输入的半径或高度和宽度的答案不是数字。我试图为矩形编写的错误适用于最初的错误输入,但是一旦用户被提示选择另一个形状,如果再次出现输入错误,则开始无限循环。
您正在使用cin.clear()和cin.ignore(1000,'n')来跳过矩形情况下的无效输入,这很好,但在其他情况下您不会这样做。
对于我来说,如果我再次输入s(对于square)和s,您的代码将进入循环。这段代码没有;它检查输入:
#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 inputn";
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 lengthn";
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 radiusn";
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 zeron";
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;
}
相关文章:
- 为什么这个递归函数会创建一个无限循环?
- 如何找出为什么我的程序一直有一个无限循环?
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- WinUsb_WritePipe进入了一个无限循环
- 为什么我会得到一个无限循环(因子)?
- 为什么我总是得到一个无限循环
- 这是一个无限循环吗?程序提前完成
- 为什么它是一个无限循环?
- 为什么我的程序在输入字符时有一个无限循环
- 为什么这段代码最终会进入一个无限循环,读取std::cin
- 如何使其成为一个无限循环
- Tcl_CreateInterp怎么会变成一个无限循环呢
- C++,得到一个无限循环
- C++ 使用 cin.fail(), cin.clear() ---我有一个无限循环,不知道为什么
- 这个递归函数(回文)变成了一个无限循环
- 当对象被卡在一个无限循环中而被破坏时会发生什么
- 代码在小输入时运行良好,但在大输入时就像一个无限循环
- 从文件中读取信息后,会发生一个无限循环
- 数独求解矩阵,而语句给出了一个无限循环
- 为什么这是c++中的一个无限循环