New here!! Another number guessing game ?

Pages: 12
Ok!!! Everyone who helped out, I just want to say thank you so much for you time, expertise and patients with my VERY beginning skills as a programmer. I have just about got everything that I wanted out of this program here.

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
using namespace std;

int main()
{
    
        bool playagain = true;
        while (playagain)
        {
    
    srand(time(0));
    int number = rand()%100+1;;
    int MEDIUMnumber = rand()%1000+1;;
    int HARDnumber = rand()%10000+1;;
    int guess;
    int numberOFguesses = 0;
    int numberOFlives = 10;
    char answer;
    short int difficulty;

        cout << "Hello, and welcome to Dylan's number guess game!!!\n";
        cout << "1-EASY\n";
        cout << "2-MEDIUM\n";
        cout << "3-HARD\n";
        cout << "Please select the difficulty level you want to play: ";
        cin >> difficulty;
 
 
    switch(difficulty)
            {
                case 1:
                    cout << "Im thinking of a number between 1-100.\n";
                    cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
                    cout << "Your guess is: ";
                    cin >> guess;
                    break;
                case 2:
                    cout << "I'm thinking of a number between 1-1000.\n";
                    cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
                    cout << "Your guess is: ";
                    cin >> guess;
                    break;
                case 3:
                    cout << "I'm thinking of a number between 1-10000.\n";
                    cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
                    cout << "Your guess is: ";
                    cin >> guess;
                    break;
            }
 
if(difficulty == 1)
{
    while(guess != number && numberOFlives > 0)
    {
        if(guess < number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
        
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << number << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
        if(guess > number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
        
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << number << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
    }
        if(guess == number && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
        }
}
if(difficulty == 2)
{
    while(guess != MEDIUMnumber && numberOFlives > 0)
    {
        if(guess < MEDIUMnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
                    
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << MEDIUMnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
        if(guess > MEDIUMnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
                    
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << MEDIUMnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
    }
        if(guess == MEDIUMnumber && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
        }
}
if(difficulty == 3)
{
    while(guess != HARDnumber && numberOFlives > 0)
    {
        if(guess < HARDnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
            
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << HARDnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
        if(guess > HARDnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
                    
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << HARDnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
    }
        if(guess == HARDnumber && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
        }
}
    cout << "Would you like to play again? (y or n)" << endl;
    cin >> answer;
    if(answer != 'y')
    {
        playagain = false;
        cout << "Hope you had fun playing!\n";
    }
        }
}


Please tell me what you all think!!! Any and all criticism is welcome!!
The fact that so much of your code is duplicated should raise some serious red flags.
He might not have learned subprograms yet or is not allowed to use them. I do agree though xismn that he should use subprograms so he doesn't have to repeat so much code.
Thank you guys for your input, could you tell me what subprograms are? I will do a bit of research myself on this topic as well. I kind of noticed that there should be a better way instead of repeating everything. I am still very new to this and this is a huge learning experience for me as I have never had any previous programming experience in my life up until a couple of months ago. Also I would like to add that I am using XCode to program as I use a Mac for school. I do plan to run bootcamp and have windows at some point in the near future.
I can't say so much about subprograms. I assume giblit means functions (I expect he/she'll correct me if not).

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
subprogram / subfunction are basically functions outside of the main function/program for example say we have a sub function for adding two integers it would look like this:
1
2
3
4
int add( int x , int y )
{
    return( x + y ); //adding two and returning that value (type int)
}


Most times you want to prototype it ( tell the compiler what the param types and return type are ). Then you define the function after the main function like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//prototype
int add ( int x , int y );
//You don't have to name the variable names
//The type is good enough
//But its best to name them so you know
//what will be in it 
//This name can vary from the definition


//int main would go here

//defintion
int add( int x , int y )
{
    return( x + y );
}


And to call this function we will put add( param1 , param2 );
where param1 is your first integer value being passed into 'x' and param2 being your second integer value being passed into 'y'. You can pass constant variables, variables, or just values.

I will show you how to add 2 + 3 then assign that value to a variable called c then we will output c.

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

int add( int x , int y );

int main()
{
    const int a = 2 , b =3;
    int a2 = 2 , b2 = 3;
    int c1 = 0 , c2 = 0 , c3 = 0;

    c1 = add( a , b ); //const variables being passed
    c2 = add( a2 , b2 ); //variables being passed
    c3 = add( 2 , 3 );  //values being passed
    //you can mix and match those 3 data types

    std::cout << c1 << std::endl;
    std::cout << c2 << std::endl;
    std::cout << c3 << std::endl;

    return( 0 );
}

int add( int x , int y )
{
    return( x + y );
}
5
5
5
Ok, I think I understand for the most part. My only question is will this eliminate the need for the switch statement? Or would my subfunction look something like this?

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
short int difficulty(case 1, case 2, case 3)
{
    while(guess != MEDIUMnumber && numberOFlives > 0)
    {
        if(guess < MEDIUMnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
                    
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << MEDIUMnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
        if(guess > MEDIUMnumber && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
                    
            if(numberOFlives == 0)
            {
                cout << "Sorry!! The number we were looking for is " << MEDIUMnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;
            }
        }
    }
        if(guess == MEDIUMnumber && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
        }
}


Now that I look at it, it doesn't seem like this will work.
You would put

1
2
3
4
                cout << "Sorry!! The number we were looking for is " << MEDIUMnumber << endl;
                cout << "                  -------------" << endl;
                cout << "                  | GAME OVER |" << endl;
                cout << "                  -------------" << endl;


In a sub program then call that instead of having that typed twice. Basically anything that is repeated or you want to use multiple times then put it in a subprogram and call that.
Topic archived. No new replies allowed.
Pages: 12