在我之前的问题中,我得到了这个答案,如果用户在国家名称中输入超过 5 个字符,它将输出错误。
#include <iostream>
#include <iomanip>
int main()
{
using namespace std;
const int maxchar = 5;
string nationname;
cout << "What is the name of your nation?" << endl;
cin >> nationname;
if (nationname.size() > maxchar)
{
cout << "The input is too long." << endl;
return 1;
}
}
我想让它在输出错误后循环回到“cout << what is the name...”。
谢谢!
有人在评论中回答了我之前的问题,但我没有让它工作/我不知道如何或在哪里将它放在我的代码中。
最佳答案
while(1){
cin >> nationname;
if (nationname.size() <= maxchar)
break;
else{
cout << "The input is too long." << endl;
}
}
关于C++ - 你如何在用户输入后循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138585/