Need help modifying this program

Well I'm currently attempting attempting some of the beginners exercises from this link http://www.cplusplus.com/forum/articles/12974/ and the one I'm stuck at the moment is the bracket searching where I have to do the reverse and have the computer guess my number. The problem that I'm having is that I don't know much of my code I would have to modify to achieve that result nor do I know how I would go about doing that. Some general pointers would be nice.

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
  #include <iostream>
#include <ctime>
#include <limits>
#include <random>

using namespace std;

enum difficulty {EASY,NORMAL,HARD} settings;

int generator1()
{
    mt19937 randomGenerator(time(NULL));
    uniform_int_distribution<int> randomNumber(1,10);
    return randomNumber(randomGenerator);
}

int generator2()
{
    mt19937 randomGenerator(time(NULL));
    uniform_int_distribution<int> randomNumber(1,100);
    return randomNumber(randomGenerator);
}

int generator3()
{
    mt19937 randomGenerator(time(NULL));
    uniform_int_distribution<int> randomNumber(1,1000);
    return randomNumber(randomGenerator);
}

int main()
{
    int guessNumber, correctNumber = 0, guessCount = 0, guessLimit = 0;
    string gameDifficulty;

    cout << "Welcome to my guessing game program. Let's us begin. \n";
    cout << "\nPress any button to continue... ";
    cout << endl;
    cin.get();

    cout << "                    ---------------------------------";
    cout << "\nSettings available: |EASY\tNORMAL\t\tHARD|" << endl;
    cout << "                    ---------------------------------";

    cout << endl;

    cout << "                 -----------------------------------------------------";
    cout << "\nGuessing limit:  | Easy:5 Guesses  Normal:25 Guesses  Hard:50 Guesses| " << endl;
    cout << "                 -----------------------------------------------------";

    cout << "\n\nPlease enter the difficulty that you would like. ";
    getline(cin, gameDifficulty, '\n');

    cout << endl;

    while(gameDifficulty != "EASY" && gameDifficulty != "NORMAL" &&
          gameDifficulty != "HARD" && gameDifficulty != "easy" &&
          gameDifficulty != "normal" && gameDifficulty != "hard")
    {
        cerr << "This setting is nonsense. Pick a real setting. ";
        cin >> gameDifficulty;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    if(gameDifficulty == "EASY" || gameDifficulty == "easy")
    {
        guessLimit = 5;
        settings = EASY;
    }
    else if(gameDifficulty == "NORMAL" || gameDifficulty == "normal")
    {
        guessLimit = 25;
        settings =  NORMAL;
    }
    else if(gameDifficulty == "HARD" || gameDifficulty == "hard")
    {
        guessLimit = 50;
        settings = HARD;
    }

    switch(settings)
    {
    case 0:
        correctNumber = generator1();
        break;
    case 1:
        correctNumber = generator2();
        break;
    case 2:
        correctNumber = generator3();
    }
    cout << "\nGuess What number I'm thinking. ";
    cin >> guessNumber;

    do
    {
        while(!(cin))
        {
            cerr << "\nIncorrect input. Please try again. ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> guessNumber;
        }
        if(guessNumber < 0)
        {
            cerr << "\nThe number that you entered is negative.";
            cerr << " Please enter a positive number to \ncontinue. ";
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> guessNumber;
        }
    }
    while(guessNumber < 0);

    cout << endl;


    while(guessNumber != correctNumber && guessCount < guessLimit)
    {
        cout << "\nSorry, no cigar. ";
        cin >> guessNumber;
        guessCount++;
    }

    if(guessNumber == correctNumber)
    {
        cout << "\nCongratulations, you guessed the right number. ";
        cout << "\n\nIt only took " << guessCount << " guesses to guess ";
        cout << "the correct number. " << endl;
    }
    else
        cout << "\nBetter luck next time sucker. " << endl;

}
Last edited on
Hi, since you are a beginner, maybe it's useful to look at some examples before trying to write your own programs. Here's an example I just wrote:

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
#include <iostream>

using namespace std;

int guess(int min, int max) {
	return (max + min) / 2;
}

int main() {

	int count = 0;

	cout << "NUMBER GUESSING" << endl << endl;

	cout << "Insert minimum value: ";
	int min_val;
	cin >> min_val;

	int max_val;
	do {
		cout << "Insert maximum value: ";
		cin >> max_val;
	} while (max_val < min_val);

	cout << "Pick a number from " << min_val << " to " << max_val << "..." << endl;

	char ans = 0;
	int guessed_number = 0;

	do {
		guessed_number = guess(min_val, max_val);
		++count;

		cout << endl << "Is your number " << guessed_number << "?" << endl
				<< "Yes (Y) - No, it's bigger (B) - No, it's smaller (S) ";
		cin >> ans;

		if (ans == 'b' || ans == 'B') {
			min_val = guessed_number;
		}
		else if (ans == 's' || ans == 'S') {
			max_val = guessed_number;
		}
	} while (ans != 'y' && ans != 'Y');

	cout << endl << "So your number was " << guessed_number << " and it took me "
                << count << " attempts to find it."
			<< endl;
        return 0;
}


If there is something you don't understand, feel free to ask!
Last edited on
Based on your example, I think I now understand the general structure on how want my code to look like. Thanks for your example.
You're welcome!
Hey Minomic, so this what I was able to come up with and the computer was able to guess it 3 tries(was probably luck). Tell if there is any way I can make it more efficient.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;

int binarySearch(int);

int main()
{
    srand(time(NULL));

    int value;

    cout << "Enter a number from 1-100 and I will guess your number! ";
    cin >> value;

    while(value > 100)
    {
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cerr << "\nThis value is too big. Only values 1 to 100 are accepted. ";
        cin >> value;
    }

    do
    {
        while(!(cin))
        {
            cerr << "\nIncorrect input. Please try again. ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> value;
        }
        if(value < 0)
        {
            cerr << "\nThe number that you entered is negative or past 100.";
            cerr << "\n\nPlease enter a positive number or something smaller to continue. ";
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> value;
        }
    }
    while(value < 0);


    cout << binarySearch(value);

    cout << endl;

    return 0;
}

int binarySearch(int value)
{
    int counter = 0;
    char response;

    int first = 0;
    int last = rand()%100+1;

    int middle = (first+last)/2;

    cout << "\nIs this your number " << middle << "? ";
    cin >> response;

    while(toupper(response) != 'Y' && toupper(response) != 'N')
    {
        cout << "Only Y,y,N or n are valid responses. ";
        cin >> response;
    }

    while(middle != value && counter < 7)
    {
        if(toupper(response) == 'Y')
        {
            break;
        }
        else if(middle < value && value - middle <= 5)
        {
            middle += 1;
        }
        else if(middle < value && value - middle <= 15)
        {
            middle += 5;
        }
        else if(middle < value)
        {
            middle += 25;
        }
        else if(middle > value && middle - value <= 5)
        {
            middle -= 1;
        }
        else if(middle > value && middle - value <= 15)
        {
            middle -= 5;
        }
        else if(middle > value)
        {
            middle -= 25;
        }
        else
            cout << "\nInvalid response. Try again";

        counter++;

        cout << "\nHow about this number: " << middle << "? ";
        cin >> response;

        while(toupper(response) != 'Y' && toupper(response) != 'N')
        {
            cout << "Only Y,y,N or n are valid responses. ";
            cin >> response;
        }
    }

    if(middle == value)
    {
        cout << "\n\nSo your number was " << middle << " and it only took me ";
        cout << counter << " tries. ";

        return value;
    }
    else
        cout << "\n\nI couldn't guess your number. ";

    return -1;
}
Last edited on
I think I optimized it pretty well. Time to tackle the tic-tac-toe beginner exercise.
Last edited on
Oh and if anybody finds any errors that I might have missed, please respond.
Ehm... Maybe I'm wrong but to me this seems like "cheating"! :)

You ask the user for the solution (i.e. the number he picked) but according to me you shouldn't. If you look at my code, I never ask the user for the solution: I just tell him to think about a number. Nevertheless, the program is still able to guess it. The reason is simple: at every iteration I split the possible range of values in two, so after an appropriate number of iterations I am able to track down the mysterious number...

The way you are doing is "suspect", because the solution is passed as a parameter do the binarySearch function. This means that the solution is actually used to find... the solution! And this doesn't make much sense, right? ;)
Topic archived. No new replies allowed.