【发布时间】:2015-07-19 21:48:44
【问题描述】:
我试图弄清楚如何组织我的代码,以便在我测试它时停止导致无限循环。我输入了一个字符串,结果突然死循环了。
// 填写代码定义并初始化为1的变量month 浮动总计 = 0,雨,月 = 1;
cout << "Enter the total rainfall for month " << month << endl;
cout << "Enter -1 when you are finished" << endl;
// Fill in the code to read in the value for rain
cin >> rain;
// Fill in the code to start a while loop that iterates
// while rain does not equal -1
if(cin.fail())
{
cout << "You have inputted an invalid value." << endl << "Please enter an integer value for rain. (-1 to exit)";
cin >> rain;
}
while (rain != -1)
{
// Fill in the code to update total by adding it to rain
// Fill in the code to increment month by one
cout << "Enter the total rainfall in inches for month "
<< month << endl;
cout << "Enter -1 when you are finished" << endl;
// Fill in the code to read in the value for rain
【问题讨论】:
标签: c++ validation loops input infinite