Why is my string outputting twice?

Hey guys!

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:


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// method prototypes
void characterInstructions();
void phraseInstructions(); //
char getCharacter();
string getString(); //
string maskLetter(char keyCharacter, string theString); //
string removeCharacter(char keyCharacter, string theString); //
int countKey(char keyCharacter, string theString);//
string newStr1;
string newStr2;

//intialize main sequence
int main()
{
characterInstructions();
char keyCharacter = getCharacter();
cout << keyCharacter;
phraseInstructions();
string theString = getString();
cout << theString <<endl;
string maskStr = maskLetter(keyCharacter, theString);
cout << "mask string is:"<<maskStr<<endl;
string removeStr = removeCharacter(keyCharacter, theString);
cout << "remove string is:"<<removeStr <<endl;
}



//... 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.
Last edited on
I see what you're saying--that may be the case.

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

return keyString[0];
}
Here's the getCharacter function, the only difference here is I used cin instead of getline


??!!

Where are you using getline? I don't see it anywhere in your code.

If you are mixing the >> operator and getline... then yes, that might cause problems.

See this thread for info. It might help:

http://www.cplusplus.com/forum/beginner/128692/
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);
}
}

return theString;
}
Yup, that explains it.

I go into what's happening in great detail in the thread I linked previously.

'long double main' also gives a very simple solution to the problem in that thread as well.
Perfect, thanks so much!
Topic archived. No new replies allowed.