C++ - 用户输入后如何循环返回?

3
在我之前的问题中,我得到了这个答案,让如果用户在国家名称中输入超过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…”。谢谢!有人在我的先前问题的评论中回答了如何做,但我无法使其正常工作/不知道在我的代码中如何或在哪里放置它。
6个回答

2
while(1){
    cin >> nationname;
    if (nationname.size() <= maxchar)
        break;
    else{
       cout << "The input is too long." << endl;
    }
}

2

至少对我来说,到目前为止你得到的回答看起来有点笨拙。在我看来,这需要使用很少使用、被忽视的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)然后跟随一个条件性的break。其次,如果您总是希望循环至少执行一次,则do/while循环是最适合的。当存在循环可能根本不执行(当/if条件为false时)的情况下,应使用while循环。最后,虽然这可能是一个相对较小的问题,但它还测试了每个输入和输出操作的结果,并在任一操作失败时退出循环。如果不这样做,不正确的输入可能会导致无限循环,在此循环中,尝试读取数据失败,但将数据留在输入流中,因此下一次迭代也会再次失败(而不等待用户输入更多数据)。
顺便说一句:我建议不要经常使用std::endl(或者可能根本不用)。养成使用\n来换行的习惯,如果需要刷新流,则使用std::flush

0
#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;

}

0
在你的函数中添加一个 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;
}

0
你需要一个循环来不断询问用户输入新的答案,当输入字符串的长度过长时。
尝试以下代码(遵循你原本的逻辑):
#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;
}

0
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接