How to clean up this code?

I wrote a program where the computer will guess a random number between 1 and 100, and then it will play a guessing game against itself to try and guess that number. Based on how far away the guess is from the actual number, I have different if statements to give a hint at how far away the computer is. Based on that, the computer will try a different guess.

My question is this: I'm using a goto statement in each "if" statement because if the computer happens to guess the number correctly, it will not go through the "Is the number ___?" because the do while statement will break. I know people don't like goto statements, and this seems kind of messy to me, so I was wondering if there was a better way to do 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
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>


using namespace std;

int randRange (int low, int high)
{
    return rand() % (high - low + 1) + low;
}

int main()
{
    cout << "Welcome to the random number guesser.\nThe computer will pick a random number between 1 and 100 and then \nwill play a guessing game against itself." << endl;
    cout << "You don't have to do anything but sit back and watch the magic!" << endl;

    int picked_number;
    int guessed_number;
    srand( time( NULL ));
    picked_number = randRange (1, 100);
    //Calling program to sleep for a few seconds both to get a new time seed and
    //allow the user to read the introduction.
    Sleep(6000);
    guessed_number = randRange (1, 100);

    cout << "Okay, I'm thinking of a number. What is it?" << endl;

    do
    {
        beginning:

        cout << "Is it " << guessed_number << "?" << endl;
        Sleep(2000);

        //pngn_dif means "Picked Number Guessed Number difference"
        int pngn_dif;
        pngn_dif = guessed_number - picked_number;

        if (pngn_dif <= -20)
        {
            cout << "Not even close. Too low." << endl;
            guessed_number = guessed_number + 20;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif >= 20)
        {
            cout << "Not even close. Too high." << endl;
            guessed_number = guessed_number - 20;
            Sleep(1300);
            goto beginning;
        }

        else if (pngn_dif <= -10)
        {
            cout << "Way too low." << endl;
            guessed_number = guessed_number + 10;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif >= 10)
        {
            cout << "Way too high." << endl;
            guessed_number = guessed_number - 10;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif <= -5)
        {
            cout << "Too low." << endl;
            guessed_number = guessed_number + 5;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif >= 5)
        {
            cout << "Too high." << endl;
            guessed_number = guessed_number - 5;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif <= -1)
        {
            cout << "Just a little too low." << endl;
            guessed_number = guessed_number + 1;
            Sleep(1300);
            goto beginning;
        }
        else if (pngn_dif >= 1)
        {
            cout << "Just a little too high." << endl;
            guessed_number = guessed_number - 1;
            Sleep(1300);
            goto beginning;
        }
    } while (guessed_number != picked_number);

    cout << "Yes!!! " << guessed_number << " is the answer!" << endl;

    return 0;
}
Last edited on

My question is this: I'm using a goto statement in each "if" statement because if the computer happens to guess the number correctly, it will not go through the "Is the number ___?" because the do while statement will break

What? Why will the do/while() statement break?

Get rid if the goto statements, you don't need them since the code is already in a loop.
Topic archived. No new replies allowed.