I'm working an an assignment for C++, and I've almost got it figured out--except for one little glitch. When I run the program, the prompt I am giving the user before I collect their data, shows up twice in row. Here's the part of the code I'm referring to:
//... blah blah blah, this is a bunch of other code that works fine...
//states phrase instructions for character
void phraseInstructions()
{
string instructions;
instructions =
"\nPlease enter a phrase or sentence >= 4 characters: \n";
cout << instructions;
}
//get theString from user
string getString()
{
string theString;
//while the phrase is less than or equal to four characters in length, keep user in the loop
cin >> theString;
while (theString.length() <=4)
{
phraseInstructions();
cin >> theString;
}
return theString;
}
What's likely happening is data is left in the input buffer, so the first time you prompt the user for input you are getting an empty string --- and since that is less than 4 characters, your loop trips and you give the prompt again.
What is the getCharacter function doing? Can you post that code? Even though the problem may not be in that function, seeing it will help explain what is happening.
Here's the getCharacter function, the only difference here is I used cin instead of getline:
//get keyCharacter from user
char getCharacter()
{
string keyString;
//while the character is not one, keep user in the loop
{
cin >> keyString;
while (keyString.length() != 1)
{
characterInstructions();
cin>>keyString;
}
}
Apologies, I shared an older version of the code with you. I switched out cin for getline so that the user could legally enter more than one word
//get theString from user
string getString()
{
string theString;
//while the phrase is less than or equal to four characters in length, keep user in the loop
{
getline (cin, theString);
while (theString.length() <=4)
{
phraseInstructions();
getline (cin, theString);
}
}