Confusion over code that won't break right

I have a program I've been writing to test myself over a few things I've learnt in the past few days but it won't work right. I want it to stop when the user has guessed the correct number but if i add 'break;' after the congratulatory message it breaks after one guess and if i leave it out it continues looping until the count reaches 5. I tried to switch the starting points of the 'if' and 'do' functions but now I'm being told the variable 'guess' is being used without being initialised and the program crashes. If someone could set this straight I'd really appreciate it as I'm unsure what I've done wrong.

The code below is it's latest incarnation which crasges but the way it was peior to this was the 'if' and 'do' functions were switched with each other, as were the final 'else' and 'while' functions:

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

int main(void)
{
	using std::cout;
	using std::cin;

	int guess;
	int count = 0;

	cout << "Welcome to the psychic test. I am thinking of\na number between 1 and 10.\n"
		<< "You have 5 guesses. What is it?\n";
	if (guess != 4)
	{
		cout << "Please enter a number: ";
		cin >> guess;
		count ++;

		do
		{
			switch(guess)
			{
			default:
					cout << "You chose " << guess << ". That is not between 1 and 10.\n";
	case 1:
		cout << "1? I'm not so simple am I?\n\n";
		break;
	case 2:
		cout << "Hmm, nope. That would be too easy.\n\n";
		break;
	case 3:
		cout << "It may be a magic number, but it's not right this time.\n\n";
		break;
	case 5:
		cout << "Well this is embarrasing...\n\n";
		break;
	case 6:
		cout << "You really should get the hang of this.\n\n";
		break;
	case 7:
		cout << "Lucky for some, but not for you.\n\n";
		break;
	case 8:
		cout << "You'll have to try much harder.\n\n";
		break;
	case 9:
		cout << "I don't think you're getting this.\n\n";
		break;
	case 10:
		cout << "Nope, not this one.\n\n";
		break;
			}
		}
		while (count < 5);
	}
	else
			cout << "Congratulations!\n\n";
	return 0;
}
you need to assign a value for guess. In this case you want it to start at zero. Also get rid of count++ and replace it with guess ++
Last edited on
cmccmc: That does not solve the problem, in fact it worsens it. Sorry.

I would have the congratulations statement printed within your switch, and then instead of break, use return 0. It actually works.

Oh... and... your cin is outside of the loop.

-Albatross
Last edited on
Look at this part of your code:

1
2
3
	int guess;
	// some code
	if (guess != 4)


You're testing for guess's value without ever initializing it.

Anyway, what your code is saying is this: if guess is not 4, enter a loop until count >= 5. However, your count++ statement is outside the do-while's body, and will only be executed once. Your do-while loop, as it is, is an infinite loop.

Incidentally, there is no break in the default case, which will make execution "fall through" to the next case.
Ok, missed that break thing. Thanks, it's in there now. How then, do I get it to let the user assign the value of guess, execute the loop 5 times unless the user inputs the correct guess, in this case 4, and work properly?

So sorry for such a newb set of questions but thi is what happens when you spend a few days copying examples then trying to write them to test yourself, haha.
Last edited on
Don't increment guess's value, that is wrong.

To answer your second question, how do you expect the program to execute if(guess != 4) if guess has not been assigned a value?

You need to ask for user input inside a loop if you want to do it more than once. Then test for guess's value. If guess == 4, leave the loop, otherwise tell the user to try again and let the loop go on.
In line 13, you're comparing guess, which is uninitialized at that point.
You're having the user enter a number only once, because that particular piece of code is not in any loop. As a result, the loop with the switch never ends.

This is a working version:

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

int main()
{
    using std::cout;
    using std::cin;

    int count = 0;
    cout << "Welcome to the psychic test. I am thinking of\na number between 1 and 10.\nYou have 5 guesses. What is it?\n";
    do
    {
        cout << "Please enter a number: ";
        int guess;
        cin >> guess;
        count++;

        if (guess!=4)
        {
            switch(guess)
            {
            default:
                cout << "You chose " << guess << ". That is not between 1 and 10.\n";
                break;
            case 1:
                cout << "1? I'm not so simple am I?\n\n";
                break;
            case 2:
                cout << "Hmm, nope. That would be too easy.\n\n";
                break;
            case 3:
                cout << "It may be a magic number, but it's not right this time.\n\n";
                break;
            case 5:
                cout << "Well this is embarrasing...\n\n";
                break;
            case 6:
                cout << "You really should get the hang of this.\n\n";
                break;
            case 7:
                cout << "Lucky for some, but not for you.\n\n";
                break;
            case 8:
                cout << "You'll have to try much harder.\n\n";
                break;
            case 9:
                cout << "I don't think you're getting this.\n\n";
                break;
            case 10:
                cout << "Nope, not this one.\n\n";
                break;
            }
        }
        else
        {
          cout << "Congratulations!\n\n";
          break;
        }
    }
    while (count<5);
}
Last edited on
IMO that solution is overblown. If you eliminate the if-else clause and insert another case, and not calling break at the end of it and instead calling return 0, then you would save about 5 lines with that formatting.

-Albatross
I appreciate the formatting concerns and help but were I to do this succinctly would not have used the if-else, I'm just trying to test the things I've learnt since I started learning. Obviously I have a lot more studying to do. Thanks so much, I've learned a little and the program works now so cheers!
Topic archived. No new replies allowed.