Hi-Low Game

Hi, my name is Mike and this is my first question on cplusplus.com. I am a beginner that barely knows the basics. I am writing a simple High/Low Game where the user guesses whether a random number will be high or low. I have noticed through trial and error, that when the random number is over 7 (8,9,10,11,12,13) the program does not display what is suppose to happen. Although, if the number is less than 7 (6,5,4,3,2,1) it displays everything correctly. I believe it has something to do with the conditional statements but I've have not figured it out. Thanks for any help in advance.

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
#include <iostream>
#include <ctime>           //Allows for random number
#include <cstdlib>

using namespace std;

int main()
{
    int points = 1000;     // Original Value
    int addpoints;
    int bet;
    int prediction;
    int randomnumber;
    char again;

    while (again!='n'&&points>0)   //will play game until user does not want to play or points = 0
    {
    cout << "You have " << points << " points" << endl;           //Displays amount of points and ask user for bet
    cout << "How many points to risk?? ";
    cin >> bet;

    while(bet>points)
    {
    cout << "You don't have " << bet << " points!!!\n";
    bet-=bet;                                                //Cant bet more than amount of points the user has
    cout << "How many points to risk?? ";
    cin >> bet;
    }

    cout << "Predict ( 1=High , 0=Low ) ";              // High/Low Prediction
    cin >> prediction;

    srand (time(NULL));           //generates seed for random number
    randomnumber = rand() % 13 + 1;     //generates random number 1-13
    cout << randomnumber;


    if (randomnumber==7)      //if random number = 7 the user only loses bet
    {
        cout << "\nNumber is 7" << endl;
        cout << "You only lose your bet" << endl;
        bet-=bet;                     // resets bet back to 0
    }
    else if (prediction==1&&randomnumber<=6)            //Guess High and number low = Lose
    {
        cout << "Number is " << randomnumber << endl;
        cout << "You Lose." << endl;
        points-=bet;             //bet is subtracted from points
        bet-=bet;
    }
    else if (prediction=0&&randomnumber>7&&randomnumber<=13)      // Guess Low and number high = Lose
    {
        cout << "Number is " << randomnumber << endl;
        cout << "You Lose." << endl;
        points-=bet;
        bet-=bet;
    }
    else if (prediction==1&&randomnumber>7&&randomnumber<=13)    // Guess High and number high = Win
    {
        cout << "Number is " << randomnumber << endl;
        cout << "You Win." << endl;
        addpoints=bet*2;
        points+=addpoints;
        bet-=bet;
    }
    else if (prediction==0&&randomnumber<=6)   // Guess Low and number low = Win
    {
        cout << "Number is " << randomnumber << endl;
        cout << "You Win." << endl;
        addpoints=bet*2;      //Doubles bet
        points+=addpoints;    //adds doubled bet to points
        bet=bet-bet;
    }

    cout << "Play again??(y or n)   ";
    cin >> again;                                   // asks user to play again
    system ("CLS");    // clear screen
    }

    cout << "Final Amount = " << points;                // if user stops playing or points = 0

    if (points==0)
    {
        cout << "You ran out of points!!!!!!";
    }


    return 0;
}
@mickeraiello

Line 51 should have double equals also, as you have on lines 38, 44, 58 and 66.

Also, you should move the srand, line 33, to before the start of the while loop, line 16. It doesn't need to be called more than once.
Last edited on
OMG!!!!! That is such a silly mistake. I've looked over the code so many times and couldn't find that little error. Thanks for pointing it out. I appreciate the help.
@mickeraiello

No problem. Just FYI, we all do little mistakes like that. It's just a part of programming glithes.
Topic archived. No new replies allowed.