Program refuses close at set intervals.
Oct 17, 2011 at 4:54am UTC
Hello fellow programmers. The program below has already been completed and runs perfectly except for one tiny issue. The loop refuses to close out after it meets the intervals I set for it. As you can see in line 24, the program is supposed to close once the the computer or the player accumulates 10 points. Anyone have a clue as to what I could do to resolve this? Thanks ahead of time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int uWon = 0;
int cWon = 0;
//Prototypes
void RockbeatsScissorsW();
void RockbeatsScissorsL();
void PaperbeatsRockW();
void PaperbeatsRockL();
void ScissorsbeatsPaperW();
void ScissorsbeatsPaperL();
void menu();
void tie();
int main()
{
cout << "Welcome to Rock, Paper, Scissors." << endl;
while ((uWon < 11) || (cWon < 11))
{
int uChoice;
unsigned cChoice = time(0);
srand(cChoice);
cChoice = 1 + rand() % 3;
menu();
cin >> uChoice;
cout << endl;
if (uChoice == 1) // User chose rock
{
if (cChoice == 1)
tie();
else if (cChoice == 2)
PaperbeatsRockL();
else if (cChoice == 3)
RockbeatsScissorsW();
}
if (uChoice == 2)
{
if (cChoice == 1) // User chose paper
PaperbeatsRockW();
else if (cChoice == 2)
tie();
else if (cChoice == 3)
ScissorsbeatsPaperL();
}
if (uChoice == 3) // User chose scissors
{
if (cChoice == 1)
RockbeatsScissorsL();
else if (cChoice == 2)
ScissorsbeatsPaperW();
else if (cChoice == 3)
tie();
}
}
system("PAUSE" );
return EXIT_SUCCESS;
}
void tie()
{
cout << "You tied with the computer. Try again." << endl << endl;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void RockbeatsScissorsW()
{
cout << "The computer chose scissors. Rock smashes scissors. You win." << endl;
uWon++;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void RockbeatsScissorsL()
{
cout << "The computer chose rock. Rock smashes scissors. You lose." << endl;
cWon++;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void PaperbeatsRockW()
{
cout << "The computer chose rock. Paper covers rock. You win." << endl;
uWon++;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void PaperbeatsRockL()
{
cout << "The computer chose paper. Paper covers rock. You lose." << endl;
cWon++;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void ScissorsbeatsPaperW()
{
cout << "The computer chose paper. Scissors cut paper. You win." << endl;
uWon++;
cout << "Computer has " << cWon << " points. "
<< "You have " << uWon << " points." << endl;
}
void ScissorsbeatsPaperL()
{
cout << "The computer chose scissors. Scissors cut paper. You lose." << endl;
cWon++;
cout << "Computer has " << cWon << " points."
<< "You have " << uWon << " points." << endl;
}
void menu()
{
unsigned cChoice = time(0);
srand(cChoice);
cChoice = 1 + rand() % 3;
cout << "Choose an option to play:" << endl;
cout << "1) Rock." << endl
<< "2) Paper." << endl
<< "3) Scissors." << endl;
}
Oct 17, 2011 at 5:08am UTC
You might want an && instead of ||. As it is, so long as either of he players has less than 11 points it will loop.
Topic archived. No new replies allowed.