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
|
#include <iostream>
#include <cstdlib> // Header file needed for srand and rand
#include <ctime> // Header file needed to use srand with time as seed
#include <iomanip> //Header to use setw
using namespace std;
int main()
{
int guess; //
int num; //my version of rand
srand(time(0)); //
num = rand() % 100 + 1; // random number to include 100
int counter = 1; //
int winner = 0; // variable to hold wins
int loser = 0; // variable to hold losses
int winCount = 0; //win counter
int loseCount = 0; // lose counter
char letter = 'Y'; // character to say yes or no
bool win = false; //
cout << "\n\n\n";
cout << setw(5) << " " << num << "\n"; //Print number to test
while (letter == 'Y') //loop to control number of games
{
while (guess != num && counter <= 5) // loop to control game //CHANGE HERE
{
cout << setw(5) << " " << "Guess a number between 1 and 100 then press enter: ";
cin >> guess;
cout << endl;
counter++;
if (guess == num)
{
cout << setw(5) << " " << "Bingo!!\n\n";
//win = true;
cout << "Do you want to play again? (Y/N)?"; //CHANGE HERE
cin >> letter; //CHANGE HERE
letter = toupper(letter); //CHANGE HERE
system("cls"); //CHANGE HERE
counter = 0; //CHANGE HERE
cout << endl; //CHANGE HERE
guess = -1;
num = rand() % 100 + 1;
cout << "\n\n\n";
cout << setw(5) << " " << num << "\n"; //Print number to test
}
else if (guess < num && counter < 6)// ****THIS SAYS ERROR EXPECTED 'WHILE'
{
cout << setw(5) << " " << "Your guess is too low try again: ";
}
else if (guess > num && counter < 6)
{
cout << setw(5) << " " << "Your guess is too high try again: ";
}
if (counter > 5)
{
cout << setw(5) << " " << "Sorry you lose. The number is: " << num << "\n";
cout << endl;
cout << "Do you want to play again? (Y/N)?";
cin >> letter;
letter = toupper(letter);
system("cls");
counter = 0;
cout << endl;
}
}
}
return 0;
}
|
Your problem is that If you want to be able to play the game over and over again, you can't have do while loops, because when the condition for the while statement isn't met any more, the do will not be executed any more and you can't go back to it.
If you look at how I edited your code: I replaced the
do while
loop by a simple
while
loop (line 29)
Also lines 36 through 39:
1 2 3 4
|
if (guess == num) //
cout << setw(5) << " " << "Bingo!!\n\n";
//win = true;
break;
|
With an
if
statement, if you don't put any {}, it will only affect the ONE following line. If you want to put multiple statements after
if
don't forget your {} (same goes for
if, else, else if, for, while, do
).
In that
if
statement I removed the
break;
because that, once again will cause your
while
loop to break. Since you want to be able to replay the game over and over again, you don't want that.
Lines 49 through 53:
1 2
|
guess = -1;
[code]num = rand() % 100 + 1;
|
cout << "\n\n\n";
cout << setw(5) << " " << num << "\n"; //Print number to test[/code]
This is when player guesses the right number and choses to continue.
guess = -1;
resets the guess variable to -1 (so that it can't be equal to
num
).
num = rand() % 100 + 1;
resets the variable num to another random number. Then the game continues...
You'll fine
//CHANGE HERE
on your code where I changed things around.
Hope this helps,
Regards,
Hugo.
EDIT: Compiling gives these errors:
In function 'int main()':
15:6: warning: unused variable 'winner' [-Wunused-variable]
16:6: warning: unused variable 'loser' [-Wunused-variable]
17:6: warning: unused variable 'winCount' [-Wunused-variable]
18:6: warning: unused variable 'loseCount' [-Wunused-variable]
20:7: warning: unused variable 'win' [-Wunused-variable] |
I don't know If you're planning on using those, but for now you're not using any of the following variables: winner, loser, winCount, loseCount, win.