【问题标题】:C++ - How do you loop back after user input?C ++ - 用户输入后如何循环?
【发布时间】:2013-04-22 02:34:46
【问题描述】:
在我之前的问题中,我得到了这个答案,因此如果用户在国家名称中输入超过 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
谢谢!
有人回答了我之前的问题,如何在 cmets 中使用,但我没有让它工作/我不知道如何或将它放在我的代码中的什么位置。
【问题讨论】:
标签:
c++
loops
if-statement
cin
iomanip
【解决方案1】:
至少在我看来,到目前为止你得到的回复看起来有点笨拙。 IMO,这需要很少使用、被忽视的 do/while 循环:
bool show_error() {
return std::cout << "The input is too long.\n";
}
int main() {
static const int maxchar = 5;
std::string nationname;
do {
std::cout << "What is the name of your nation?\n";
} while (std::cin >> nationname && nationname.size() > maxchar && show_error());
}
至于为什么我认为这样更好:首先,除非这样做完全不合理,否则将循环的条件写为循环条件要好得多,而不是while (1)后跟条件@ 987654323@ 循环内的某处。其次,如果您总是希望一个循环至少执行一次,那么 do/while 循环就是适合该工作的循环。当有合理的机会循环根本不会执行时(当/如果条件为假时),应该使用while 循环。最后,虽然它可以说是一个相当小的点,但这也测试了每个输入和输出操作的结果,如果其中一个失败,则退出循环。如果不这样做,错误的输入会导致无限循环,尝试读取失败,但将数据留在输入流中,因此下一次迭代将再次失败(无需等待用户输入更多数据)。
顺便说一句:我建议不要使用std::endl 这么多(或可能根本不使用)。养成在只需要换行时使用\n 的习惯,并在/如果您想刷新流时使用std::flush。
【解决方案2】:
while(1){
cin >> nationname;
if (nationname.size() <= maxchar)
break;
else{
cout << "The input is too long." << endl;
}
}
【解决方案3】:
在您的函数中添加一个 while 循环,以便在出错时继续输入。
int main()
{
using namespace std;
const int maxchar = 5;
string nationname;
while(1)
{
cout << "What is the name of your nation?" << endl;
cin >> nationname;
if (nationname.size() > maxchar)
{
cout << "The input is too long." << endl;
}
else
{
break;
}
}
return 1;
}
【解决方案4】:
当输入字符串的长度太长时,您基本上需要一个循环来不断要求用户输入新答案。
尝试以下操作(按照您的原始逻辑):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int maxchar = 5;
string nationname;
while (true)
{
cout << "What is the name of your nation?" << endl;
cin >> nationname;
if (nationname.size() > maxchar)
{
cout << "The input is too long." << endl;
continue;
}
else
{
cout << "input nation is: " << nationname <<endl;
break;
}
}
return 0;
}
【解决方案5】:
const int maxchar = 5;
string nationname;
while(!(nationname.size()>0 && nationname.size()<maxchar)){
nationname="";
cout << "The input has to be smaller than "<<maxchar<<" characters and has"
<<" to contain at least 1 character."
cin >> nationname;
}
//nationname is as supposed to be
【解决方案6】:
#include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
//declare a string variable called
nationName
std::string nationName;
std::cout << " Please enter youre
Nation name,\n Names must be no
longer than 20 characters in
length\n and must not contain any
numbers. . ." "\n""\n";
//Get input from user
getline(std::cin, nationName);
//validate the length of the name
variable, if it is greater than 20
characters display an error
while (name.length() >= 20)
{
std::cout << " sorry you have
entered incorect data\n Please try
again . . . ""\n""\n";
getline(std::cin, name);
}
//if the name is entered correctly
loop exists and displays
std::cout << nationName <<
std::endl;
return 0;
}