Adding an If Loop in Playagain instead of a Do While?

When i tried to give this a shot by pausing the video i did not think about bool or do/while instead created a loop (ignore any formatting). It works but not sure if its the right way to do it.

string PlayAgain()
{
cout << "Do you wanna play again? << “\n”;
string Response = “”;
cout << "Select (y/n): " << “\n”;
cin >> Response;

if (Response == "y") 
{
	main();
}
else
{
	cout << "\nThank you for playing!\n";
}

return Response; 

}

You can indent by 4 spaces or surround with 3 backticks ` to get a code block

string PlayAgain()
{
    cout << "Do you wanna play again?\n";
    string Response = "";
    cout << "Select (y/n):\n";
    cin >> Response;

    if (Response == "y") 
    {
        main();
    }
    else
    {
        cout << "\nThank you for playing!\n";
    }
    return Response; 
}

The problems with this method is that

  1. The standard forbids you from using main in your program so that includes calling it
  2. You aren’t actually using the return value
  3. Every call to main will keep the previous call stack in memory

2019-05-26%2009_52_03-Window

I see. Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms