Review

closed account (93v5LyTq)
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main()
{
int num, guess;
string firstName;
bool isGuessed = false;
char playAgain = 'y';

srand(time(0));
cout << "Welcome to the Guessing Game. What is your name?" << endl;
cin >> firstName;
cout << "Hi " << firstName << endl;


while (playAgain== 'y' || playAgain == 'Y')
{
while (!isGuessed)
{
num = rand() % 2 + 1;
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;

cin >> guess;
cout << endl;

if (guess < num)
{
cout << "I'm sorry, that guess is too small." << endl;
}

if (guess > num)
{
cout << "I'm sorry, that is too big." << endl;
}

if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}

cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
if (playAgain == 'y' || playAgain == 'Y')
{
isGuessed = false;
}

else
{
playAgain = 'n';
cout << "Thank you for playing!" << endl;
return false;
}

}

}

return 0;
}
Hi all, I was wondering if someone can review my code, I have to submit it tonight for a project. I am specifically scared to submit due to the return statement. Can anyone help me out? Let me know if the return statements are good and if the spacing and everything are good please.
Hi,

The returns seem to work perfectly, I would just indent the code so its easier to read.
The following line : num = rand() % 2 + 1; would get the remainder of the random number when its divided by 2, so the remainder would either be 0 or 1. So the only two cases are num =(0+1) = 1, or num = (1+1) = 2. So as opposed to picking through 1-100 in you're actually only picking between two real choices. I added the output of the num in each case so you will know it is working properly.

Also the program didnt work for me as copied/pasted without this line : #include <stdlib.h>


Here is how I would indent it

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
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
int num, guess;
string firstName;
bool isGuessed = false;
char playAgain = 'y';

srand(time(0));
cout << "Welcome to the Guessing Game. What is your name?" << endl;
    cin >> firstName;
cout << "Hi " << firstName << endl;


while (playAgain== 'y' || playAgain == 'Y')
{
    while (!isGuessed)
    {
        num = rand() % 2 + 1;
        cout << "I am thinking of a number between 1 and 100,"
        << " can you guess what it is?" << endl;
    
        cin >> guess;
        cout << endl;
    
        if (guess < num)
        {
            cout << "I'm sorry, that guess is too small." << endl;
            cout << "The number is: " << num << endl;
        }
        
        if (guess > num)
        {
            cout << "I'm sorry, that is too big." << endl;
            cout << "The number is: " << num << endl;
        }
        
        if (guess == num)
        {
            cout << "That is correct!"
            << " Congratulations on winning! Thank you for playing my game." << endl;
            isGuessed = true;
        }
        
        cout << "Would you like to play again? (y/n)" << endl;
        cin >> playAgain;
        
        if (playAgain == 'y' || playAgain == 'Y')
        {
             isGuessed = false;
        }
        
        else
        {
            playAgain = 'n';
            cout << "Thank you for playing!" << endl;
            return false;
        }

}
}
}
}


The only thing you have to fix is the random number variable and you'll be good (Also comment your code as im sure your professor is expecting it)
Last edited on
Topic archived. No new replies allowed.