Hello, I have been struggling with this for quite some time now and are needing help in how to repeat my program through the appropriate input. I am unsure on what I am to put in in order for it to repeat.
Here is my current main:
I am wanting to repeat the program once someone inputs 'Y' or 'y'
int main()
{
Names();
b = 0;
Display();
//loop that will run until a player has won or tied
while (1)
{
b++;
Input();
Display();
//win function if player has won
if (Win() == 'X')
{
system("cls");
cout << X << " has Won!" << endl;
cout << "Would you like to Play again, Y/N." << endl;
string choice2;
cin >> choice2;
if (choice2 == "Y" || "y")
{
/*Repeat the program*/
}
elseif (choice2 == "N" || "n")
{
return 0;
}
}
elseif (Win() == 'O')
{
system("cls");
cout << O << " has Won!" << endl;
cout << "Would you like to Play again, Y/N." << endl;
string choice2;
cin >> choice2;
if (choice2 == "Y" || "y")
{
/*Repeat the program*/
}
elseif (choice2 == "N" || "n")
{
return 0;
}
}
//if players tie function
elseif (Win() == '/' && b == 9)
{
system("cls");
cout << "It's a Tie!" << endl;
cout << "Would you like to Play again, Y/N." << endl;
string choice2;
cin >> choice2;
if (choice2 == "Y" || "y")
{
/*Repeat the program*/
}
elseif (choice2 == "N" || "n")
{
return 0;
}
}
TogglePlayer();
}
system("PAUSE");
return 0;
}
if (choice2 == "Y" || "y")
Each condition must be typed in full: if (choice2 == "Y" || choice2 == "y")
The original version will always be true. Why? A zero value is considered as logical false, and any non-zero as logical true. Hence the first version is the same as if ((choice2 == "Y") || (true) ) and anything || true evaluates as true.
Usually for a single-letter response such as 'y' or 'n' type char would be used rather than string. Remember to use single quotes for character literals 'Y' and 'y' etc. if you do that.