well, i'm writing a text-based game, and my 'NameEnter' function is acting up, basically, if you confirm that the name that you input is correct, it works fine, but if you type 'no' or something else, it doesn't accept the name reentry, i am not sure exactly what i have done wrong, but the outputs are below, and below that is the code. The name entered in all the examples is 'matthew'.

'yes' output let's begin, matthew 'no' output

Enter your name correctly this time: so, your name is , correct?

Just type 'yes' or 'no'

note: you are not prompted to reenter your name at all.

else output

Please enter 'yes' or 'no' - no capitals

Enter your name correctly this time: so, your name is ,correct?

Just type 'yes' or 'no's

note: you are not prompted to reenter your name at all.

void NameEnter () {
	//declerations start
	string PlrName;
	getline(cin , PlrName);
	string (PlrNameConfirm);
	//declerations end

	cout << "So, your name is " << PlrName << " ,correct?" << endl;
	cout << "\n";
	cout << "Just type 'yes' or 'no'" << endl;
	cin >> PlrNameConfirm;

	do
	if (PlrNameConfirm == "yes") {
		cout << "\n";
		cout << "let's begin, "  << PlrName << endl;

		// below here is the problem//
	} else if (PlrNameConfirm == "no") {
		cout << "Enter your name correctly this time: ";
		getline(cin , PlrName);
		cout << "so, your name is " << PlrName << " ,correct?" << endl;
		cout << "\n";
		cout << "Just type 'yes' or 'no'";
		cin >> PlrNameConfirm;

	} else {
		cout << "Please enter 'yes' or 'no' - no capitals" << endl;
		cout << "\n";
		cout << "Enter your name correctly this time: ";
		getline(cin , PlrName);
		cout << "so, your name is " << PlrName << " ,correct?" << endl;
		cout << "\n";
		cout << "Just type 'yes' or 'no'";
		cin >> PlrNameConfirm;
	}
	while (PlrNameConfirm != "yes");
	// above here is the problem //
}
string (PlrNameConfirm);

Why do you have parentheses in this declaration? They're really not necessary.

You seem to be missing some braces. Your loop seems to work correctly, but the formatting isn't technically correct.

do { //<---I don't see this brace
  /*... statements ...*/
} while (condition); //<--- I also don't see this brace

Your problem appears to come from the fact that you are combining multiple input methods. They have significant behavioral differences. If you are going to combine them, you need to be aware of these differences and compensate for them.

When you press the ENTER key, a "newline" character is placed on the input stream. This newline is where your problem is coming from. The getline() function detects and extracts the character that represents "newline" ('\n'), and the extraction operator ('>>') does not. As a result, you have a "dangling" newline in the input stream when you get to the next getline() statement. This statement grabs the newline as it's input creating the illusion of the command being skipped when it's actually not.

Read this thread.

thanks for your help, i wrote the code late so that's probably why i missed braces and added brackets. Quick follow up question though, how would i clear the input stream before the next getline() statement?

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.