Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why after I type 'y' it cannot return to the Enter name of Player1 ?
What wrong with my coding?
Does anyone know?








*I'm waiting for a kind-hearted person answered voluntarily,
and those who refuse to answer may ignored it. Thank you.

What I have tried:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    	string Player1, Player2;
    	bool PlayAgain=true;// declaration of play again
        cout << "Enter name of Player1 : ";
		getline(cin,Player1);
        cout << "Enter name of Player2: ";
		getline(cin,Player2);
		cout<<endl<<Player1<<" is Player1 so he/she will play first\n";
		cout<<Player2<<" is Player2 so he/she will play second\n";
		
       
        //Let players choose whether they want to play again or not.
        char again;
        cout << "Would you like to play again? Enter y or n: " << endl;
        cin >> again;

        if (again != 'y')
        {
            PlayAgain = false;
            return 0;  
        }
}
Posted
Updated 8-Jun-22 10:24am

This should solve your problem
C++
<pre>#include <iostream>
#include <string>
using namespace std;
int main()
{
	string Player1, Player2;

	while (true)
	{
		cout << "Enter name of Player1 : ";
		getline(cin, Player1);
		cout << "Enter name of Player2 : ";
		getline(cin, Player2);
		cout << endl << Player1 << " is Player1 so he/she will play first\n";
		cout << Player2 << " is Player2 so he/she will play second\n";


		//Let players choose whether they want to play again or not.
		string again;
		cout << "Would you like to play again? Enter y or n: " << endl;
		getline(cin, again);

		if (again[0] == 'n')
		{
			break;
		}
	}
}
 
Share this answer
 
v3
Comments
The Kings 8-Jun-22 15:19pm    
Oh, it is nice. Thank you.
Tony Hill 8-Jun-22 15:31pm    
I tidied up a bit to remove the 'PlayAgain' variable and use a break statement to make it a bit cleaner.
When you execute a method there are to ways to leave:
1) You can explicitly use a return statement:
C++
if (again != 'y')
{
    PlayAgain = false;
    return 0;
}
When return is executed, the method exits at that point and no further code is executed.

2) If your code reaches the closing curly bracket for the method, it exits the method as it has no further statements to run:
C++
int main()
{
    ...
}
This can prevent your code for even compiling as it doesn't specify a return value, so unless your method is declared as returning a void value, many compilers will issue an error on this.

And when your code exits the main method, your application ends as well.

So in your case, you need to provide some way to "go around again" in your method, or it will always close the app when the user has made a selection.

In this case, I'd suggest that you use a C++ do while Loop[^] and use the existing if condition to exit that instead of return.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900