hi! can someone help debug my codes?
getline() works properly in my main() but if i'm putting it to another function, it ignores the first getline() w/o entering any words and goes to the 2nd getline.

my program works like this:
i have a menu() that lets the user to choose a category [easy-'e', ave-'a', hard-'h']. for example, if the user chose the letter 'e' it will jump to the easy().
and as soon as i entered the letter 'e', it goes straight to player 2 or the 2nd getline. the easy() consists of two getline (string answers for player 1 and 2)

If i use the simple cin, the program works fine.

how can i fix this?

this is the code segment with the getline() in my easy function..

cout << "\n\nPLAYER 1 Answer: ";
			getline (cin, easyAnswerPlayer1); // i declared this as a string. the program ignores this
			replace(easyAnswerPlayer1.begin(), easyAnswerPlayer1.end(), ' ', '_');
			cout << "PLAYER 2 Answer: ";
			getline (cin, easyAnswerPlayer2); // and also this
			replace(easyAnswerPlayer2.begin(), easyAnswerPlayer2.end(), ' ', '_');

			answerEasy(easyAnswerPlayer1, easyAnswerPlayer2);

and this is the menu ()...

cout << "\nPlease choose a level of difficulty."
			"\n[e] Easy \n[a] Average \n[h] Hard \n[q] Quit\n\n"
			"Letter: ";
		cin >> level;

		if (level == 'e')
		else if (level == 'a')
		else if (level == 'h')


Recommended Answers

All 3 Replies

Without seeing the code for your main() function, my guess is that you are using soemthing like

cin >> ch;

to read in the character where the player chooses an option, correct? If so, you will want to clear the input buffer using cin.ignore() immediately after that. What is happening is that the newline after the character is still in the buffer, and you need to clear it before you can read in a new line.

Yes, as seen above in the menu() function, he/she uses:

cin >> level;

Use cin.ignore(), as Schoil-R-LEA suggested, after that statement.

wow! thanks guys! its working now!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.