Hey i'm doing a project, and i'm at the part where the user must enter a phrase or sentence and it must be compared to the phrase or sentence already imput to the computer via file. I'm asking a user to enter the sentence / phrase using cin.getline, but when i run the program, with the debugger i see that it is skipping over the cin.getline and just going straight into the next line of code. Why is this?

Here's my code

bool solve(int players, int score[], string phrase, string&
bool solved = false;
const int SIZE = 81;
char sentence[SIZE];
int count = 0;
int length;


cout << "You chose to solve the puzzle!" << endl;
cout << "DIRECTIONS: Type in your solution on one line." << endl;
cout << "Separate each word with one space." << endl;
cout << "Solution -> ";
cin.getline(sentence, SIZE);
cout << "Youve have entered: " << sentence << endl;

length = phrase.length();

	for (count; count < length; count++)
			if (sentence[count] != phrase[count])
				solved = false;
				solved = true;

	if (solved == true)
		for (count = 0; count < length; count++)
			puzzle[count] = sentence[count];
		cout << "You have solved the puzzle!" << endl;
		cout << "That is incorrect, you lose your turn" << endl;

return (solved);

Am i doing something wrong? is there a better way to store user imput into an array for this?

6 Years
Discussion Span
Last Post by gmfreak1991

It is probably because you've got a cin >> foo; somewhere else in your program.

You can't blithely mix >> and getline() input, because the user will always press ENTER at the end of every input.

Your inputs, then, should look like this:

// get something with >>
int n;
cin >> n;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

// get something with getline()
string s;
getline( cin, s );

If you do it like this then you will have read everything the user typed, up-to and including the ENTER key he pressed at the end.

Hope this helps.


I changed the sentance variable to string, and changed the code to

getline(cin, sentence);

but now i get a string subscript out of range error, and even when i ignore it i still can't imput anything.

Is something still wrong?


When i run it with the debugger i noticed its still skipping the

getline(cin, sentence);

and then i get the error code at the

cout << "Youve have entered: " << sentence << endl;

which accounts for the subscript out of range error


cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

fixed it. thanks!

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.