gpt4 book ai didi

C++ - 你如何在用户输入后循环?

转载 作者:搜寻专家 更新时间:2023-10-31 01:49:42 25 4
gpt4 key购买 nike

在我之前的问题中,我得到了这个答案,如果用户在国家名称中输入超过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com