请看下面的代码
#include <iostream>
#include <iomanip>
using namespace std;
double hypontenuse(double,double);
int main()
{
double side1 = 0;
double side2 = 0;
cout << "Enter side1 (-1 to exit)" << endl;
cin >> side1;
while(true)
{
if(side1==-1)
{
break;
}
cout << "Enter side2" << endl;
cin >> side2;
double result = hypontenuse(side1,side2);
cout << "The Hypontenuse of the Right Triangle is: " << setprecision(2) << fixed << result << endl;
cout << "Enter side1 (-1 to exit)" << endl;
cin >> side1;
}
}
double hypontenuse(double side1, double side2)
{
double result = (side1*side1)+(side2*side2);
return result;
}
我是 C++ 新手。在这段代码中,如果我输入了无效的输入(空格、制表符、字母等),这段代码会突然变成一个无限循环。我需要忽略这些无效输入,显示一条消息,然后回到起始位置。我怎样才能做到这一点?请帮忙!