3

In my previous question, I got this answer to work so that if the user inputs more than 5 characters in the nation name, it will output an error.

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

I want it so that it will loop back to the "cout << what is the name..." after it outputs the error.

Thanks!

Someone answered on my previous question how in the comments but I didn't get it to work/I don't know how or where to put it in my code.

Leroterr1
  • 73
  • 2
  • 2
  • 6

6 Answers6

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

At least to me, the responses you've gotten so far look a little clumsy. IMO, this calls for the little-used, much-neglected do/while loop:

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());
}

As to why I think this is better: first of all, unless it's completely unreasonable to do so, it's much better to write the condition for a loop as the loop condition, rather than a while (1) followed by a conditional break somewhere inside the loop. Second, if you always want a loop to execute at least once, a do/while loop is the right one for the job. A while loop should be used when there's a reasonable chance the loop will not execute at all (when/if the condition is false). Finally, although it's arguably a fairly minor point, this also tests the result of each input and output operation, and breaks out of the loop if either fails. If you don't do that, incorrect input can lead to an infinite loop, where the attempt to read fails, but leaves the data in the input stream, so the next iteration will fail again (without waiting for the user to enter more data).

As an aside: I'd advise against using std::endl so much (or probably at all). Get in the habit of using \n when you just want a new-line, and std::flush when/if you want to flush the stream.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

add a while loop in your function to continue input when wrong.

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;
}
tausun
  • 2,154
  • 2
  • 24
  • 36
0

You basically need a loop to keep asking the user to input new answer when length of input string is too long.

try the following (following your original logic):

#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;
}
taocp
  • 23,276
  • 10
  • 49
  • 62
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
4pie0
  • 29,204
  • 9
  • 82
  • 118
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;

}
EniGma
  • 1