guessing game

Hello, I am a beginner trying to write a program on a Linux system using c++.
My goal is to have the user input an integer from 1 to 100 and give the computer 6 attempts to guess that number. I am trying to incorporate a score board were one point is given to computer for guessing within 6 guesses, and one is point awarded to the player for choosing a number the computer does not guess.
Also the option of playing again and keep track of how many games have been played and won.

I'll greatly appreciate any help!
Here is my incomplete work which does not run properly...

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
#include <iostream>
#include <string>
using namespace std;

const int lowerbound = 1;
const int upperbound = 100;
 
int main()
{
    int Min = lowerbound;
    int Max = upperbound;
    int guess; //.............computer guess
    int feedback; //..........player response to guess
    int plyrNum; //...........number chosen by player
    const int turn = 0; //....how many guesses taken
    const int plyrW = 0; //...how many player wins
    const int plyrL = 0; //...how many computer wins
    const int numGames = 0; //how many games played
    
    cout << endl << endl << endl;
    cout << "         Welcome to the Guessing Game" << endl;
    cout << endl;
    cout << endl;
    cout << "INSTRUCTIONS: " << endl;
    cout << endl;
    cout << "    Enter an integer in the range 1 to 100." << endl;
    cout << "I will try to guess your number with in 6 guesses."<<endl;
    cout << "If I can guess your number in 6 guesses, I Win!." <<endl;
    cout << " If you pick a number I don't guess, You Win!." <<endl;
    cout << endl;
    cout << " Would you like to play? " << endl;
    cout << " press 'Y' to play, or 'N' to quit: " << endl << endl;
    char play;
    cin >> play;
    cout << endl;
    cout << endl; 
    

         
    do {
        cout << endl;
        cout << "Games played: " << numGames << endl;
        cout << "Player Wins: " << plyrW << endl;
        cout << "Player Lost: " << plyrL << endl;
        cout << endl;        
        cout << "enter an integer from 1 to 100: ";
        cin >> plyrNum;
        cout << endl;
        
        int turn;
        while (turn < 6 && feedback != 'c') {
            
            
            int turn = turn + 1;
            guess = (Min + Max)/2;
            cout << "turn number: " << turn << endl<<endl;
            cout << "My guess is " << guess << endl<<endl;
            cout <<"is guess low (l), high (h), or correct (c)?"<<endl;
            cin >> feedback;
            if (feedback == 'l') {
            Min = guess + 1;
            
            }
            if (feedback == 'h') {
            Max = guess - 1;
            }
            if (feedback == 'c') {
            int plyrL = plyrL + 1;
            
            }
        }
        
        if (int turn = 6) {
            int plyrW = plyrW + 1;
        }    
        
        int numGames = numGames + 1;
        cout << "would you like to play again? " << endl;
        cin >> play;
    } while ((play == 'Y') || (play == 'y'));
    
    
    if ( play == 'N' || play == 'n') {
        cout << "good bye!" << endl;
    }

return 0;
}


Thank you for your time :)
Last edited on
First off,

using endl is recourse intensive/ slow and therefore (in this case) i recommend to use "\n" (since it does almost the same)

However that's a minor thing. Anyway, you want to initialize turn as a number, since it otherwise assigns a random number. So that'd be turn=0
You can just make a for loop out of it, instead of a while loop.

Here's the code i changed for you with explanations.

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
#include <iostream>
using namespace std;

 
#include <iostream>
using namespace std;

 
int main()
{
/*    int lowerbound = 1; //im not sure, but i think using const int here rapes the whole iteration later on
    int upperbound = 100; //also there's no need to use them in the global scope, so just declare them here*/

    int Min = 1, Max=100, guess, feedback, plyrNum;
    int turn=0, plyrW=0,plyrL=0, numGames=0; //you can put all these declarations in one or 2 lines ;)
    //also you declare turn a lot of times, everytime using another variable for it, try avoiding this :)
    char play; 
    
    cout<<"\n\n\n         Welcome to the Guessing Game\n\n\n"; //using \n is faster
    cout<<"INSTRUCTIONS: \n\n";
    cout << "    Enter an integer in the range 1 to 100.\n";
    cout << "I will try to guess your number with in 6 guesses.\n";
    cout << "If I can guess your number in 6 guesses, I Win!.\n";
    cout << " If you pick a number I don't guess, You Win!.\n\n";
    cout << " Would you like to play?\n";
    cout << " press 'Y' to play, or 'N' to quit: \n\n";
    cin >> play;
    cout << "\n\n";
             
    while(play!='n'||play!='N'){
        cout << "\n";
        cout << "Games played: " << numGames << "\n";
        cout << "Player Wins: " << plyrW << "\n";
        cout << "Player Lost: " << plyrL <<"\n\n";        
        cout << "enter an integer from 1 to 100: ";
        cin >> plyrNum;
        cout <<"\n";
        
        for(int turn=0; turn<7 && feedback !='c'; ++turn)//using a for loop here, making it <7 so it can do the if statement
        { 
            guess = (Min + Max)/2;
            cout << "turn number: " << turn <<"\n\n";
            cout << "My guess is " << guess << "\n\n";
            cout <<"is guess low (l), high (h), or correct (c)?"<<"\n";
            cin >> feedback;
            if (feedback == 'l') {
            Min = guess + 1;
            
            }
            else if (feedback == 'h') { //use else if here ;)
            Max = guess - 1;
            }
            else if (feedback == 'c') {
            int plyrL = plyrL + 1;
            
            
            }
        }
        
        if (int turn = 6) { 
            int plyrW = plyrW+ 1;
        }    
        
        int numGames = numGames + 1;
        cout << "would you like to play again?\n ";
        cin >> play;
}
    
        cout << "good bye!\n"; //don't need an if statement here, since the program'll only come here if play!=Y

return 0;
}


This code isn´t working yet, but I can´t seem to correct it )been on it for 30mins now...
I think it´s the char´s messing it up, but even then i can't fix it (probably cuz im not good enough yet :P)

Remember had a problem like this once too..

I'll continue trying :)

Cheers!
I greatly appreciate your time kaduuk.
Have a good thanks-giving holiday!
Sorry I never got to finish your original code, got called away. Also, never got a chance to put in comments on the changes made, which means you 'll have to go through and check. Just quickly though, you had multiple initialisations of the same int and you made constants out of integers that you wanted updated ie lines 15-18. Also you had feedback as an int when it should have been a char.
NOTE Ive only managed to get this partly working, as I said I got called away, but anyway its a start...

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
#include <iostream>
#include <string>
using namespace std;

const int lowerbound = 1;
const int upperbound = 100;
 
int main()
{
    int Min = lowerbound;
    int Max = upperbound;
    int guess; //.............computer guess
    char feedback; //..........player response to guess
    int plyrNum; //...........number chosen by player
    int turn = 0; //....how many guesses taken
    int plyrW = 0; //...how many player wins
    int plyrL = 0; //...how many computer wins
    int numGames = 0; //how many games played
    
    
    cout << "         Welcome to the Guessing Game\n";
    cout << "INSTRUCTIONS: \n";
    cout << "    Enter an integer in the range 1 to 100.\n";
    cout << "I will try to guess your number with in 6 guesses.\n";
    cout << "If I  guess your number with 6 guesses, I Win!.\n";
    cout << " Otherwise... you win!.\n";
    cout << " Would you like to play? \n";
	char play;
    cout << " press 'Y' to play, or 'N' to quit: \n";
    cin >> play;   
          
    do {        
        cout << "Games played: " << numGames << endl;
        cout << "Player Wins: " << plyrW << endl;
        cout << "Player Lost: " << plyrL << endl;
          
        cout << "enter an integer from 1 to 100: ";
        cin >> plyrNum;                    
            char feedback;
            turn = turn + 1;
            guess = (Min + Max)/2;
            cout << "turn number: " << turn << endl<<endl;
            cout << "My guess is " << guess << endl<<endl;
            cout <<"is guess low (l), high (h), or correct (c)?"<<endl;
            cin >> feedback;
            if (feedback == 'l') {
            Min = guess + 1;
            
            }
            if (feedback == 'h') {
            Max = guess - 1;
            }
            if (feedback == 'c') {
            int plyrL = plyrL + 1;
            }
        
         } while ((play == 'Y') || (play == 'y'));


        if (int turn = 6) {
            int plyrW = plyrW + 1;
        }    
        
        numGames = numGames + 1;
        cout << "would you like to play again? " << endl;
        cin >> play;
   
    
    
    if ( play == 'N' || play == 'n') {
        cout << "good bye!" << endl;
    }

return 0;
}
Last edited on
Thank you AlphaBravo,
this is a big help
Topic archived. No new replies allowed.