Hey guys, so I'm trying to make a Paper, scissors, rock program (where you play against the computer). And i got pretty far, to the point where it seemed finished. But then i realized that i need to make the program exit itself after the player wins three times.
So i tried using a Do and While loop, but its not working.
Heres the main code...
/*
This is a simple Paper, Scissors, Rock program.
You press 1, 2 or 3 to select your choice and
once you win three times, the program closes.
*/
#include "Assignment4.h"
using namespace std;
int main()
{
srand ( time(NULL) );
int choice;
int compchoice = (rand()%2)+1;
int winCount = 0;
//Creating the interface
cout << "Welcome to the Rock, Paper, Scissors game." << endl;
cout << " 1 = Rock, 2 = Paper, 3 = Scissor, 4 = Exit Program" << endl;
cin >> choice;
do
{
if (choice == 1)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
else if (compchoice == 2)
{
cout << "Rock beats scissors! You win!\n\n\n\n";
winCount += 1;
}
}
if (choice == 2)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
{
cout << "Paper beats rock! You win!\n\n\n\n";
winCount += 1;
}
else if (compchoice == 2)
cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
}
if (choice == 3)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
{
cout << "Scissors beat paper! You win!\n\n\n\n";
winCount += 1;
}
else if (compchoice == 2)
cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
}
if (choice == 4)
{
return 0;
}
return main();
}
while (winCount >= 3);
{
cout << "You won three times!" << endl;
//return 0;
}
//return main();
}
These last few lines, after the While statement, is where the problem occurs; i thought it had something to do with my placement of the return main();
but whereever i place it, the program either exits the moment the computer (or the player) wins, or it doesnt actually exit at all and acts as if i never placed the loop (pressing 4 will still close it, however).
I'm pretty sure im missing something with this While function...can someone point me to where?