Originally Posted by
7stud
You can do something like this:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
bool goodInput;
do
{
goodInput = true;
cout<<"Enter exactly 5 characters: ";
getline(cin, input);
if(input.length() != 5)
{
cout<<"Idiot!"<<endl;
goodInput = false;
}
}while(goodInput == false);
return 0;
}
Slight nit-picky change. Rather than change goodInput all the time you could use:
Code:
goodInput = false;
do
{
cout << "Enter exactly 5 characters: ";
getline(cin, input);
if (input.length() != 5)
{
cout << "Idiot!" << endl;
}
else
{
goodInput = true;
}
}while (goodInput == false);
And using a while you don't need an infinte loop with break:
Code:
goodInput = false;
while (goodInput == false)
{
cout << "Enter exactly 5 characters: ";
getline(cin, input);
if (input.length() != 5)
{
cout << "Idiot!" << endl;
}
else
{
goodInput = true;
}
}
And SPACEs help readability. Told you they were nit-picky...