2013-04-22 84 views
2

在我之前的问题中,我得到了这个答案,以便如果用户在国名中输入5个以上的字符,它会输出一个错误。C++ - 如何在用户输入后循环回来?

#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 < <名称是什么?”之后输出错误。

谢谢!

有人回答我的上一个问题如何在评论中,但我没有得到它的工作/我不知道如何或在哪里把它放在我的代码。

回答

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

谢谢!我终于搞定了。 – Leroterr1 2013-04-22 02:45:34

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 
2

至少对我来说,到目前为止,你已经得到的答复看起来有点笨拙。国际海事组织,这需要很少使用,很多被忽视的做/ 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循环是正确的。当有合理的机会循环完全不会执行时(当/如果条件为假时),应使用循环(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; 

}