<分区>
我试图在没有 tryCatch-Block 的情况下捕获错误的输入。每当用户键入错误的值(例如“hh”或 -5)时,我的程序将再次询问,直到用户键入正确的值。然后继续,直到数组满为止。
int main() {
double array[6];
string str_array[7];
str_array[0] = "stringA";
str_array[1] = "stringB";
str_array[2] = "stringC";
str_array[3] = "StringD";
str_array[4] = "StringE";
str_array[5] = "StringF";
str_array[6] = "StringG";
double value;
for (int i = 0; i < 7; i++) {
bool exit = false;
while (!exit) {
cout << str_array[i] << ":";
if (cin >> value&& value> 0) {
array[i] = value;
cout << array[i] << endl;
exit = true;
} else {
cerr << "incorrect input" << endl;
cin.clear();
}
}
}
}