Switch statments

How would you go about creating a program that guesses a number from 1 to 5 that uses a switch statement, that has the user input a number from 1 to 5 and gives a statement for each value, including if they guess the number correct?
You mean, a program that guesses a number? So I ask you 3, and you can answer "it's right", or "bigger" or "smaller"... This kind of problem is not related to the switch case, but to a for loop
You can do it either way to be honest. For example
the switch statement could look 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	switch (number)
	{
	case 1:
		{
			if (number == guess)
			{
				cout << "A correct guess!";
			}
			else
			{
				cout << "An incorrect guess";
			}
			break;
		}
	case 2:
		{
			if (number == guess)
			{
				cout << "A correct guess!";
			}
			else
			{
				cout << "An incorrect guess";
			}
			break;
		}
	case 3:
		{
			if (number == guess)
			{
				cout << "A correct guess!";
			}
			else
			{
				cout << "An incorrect guess";
			}
			break;
		}
	case 4:
		{
			if (number == guess)
			{
				cout << "A correct guess!";
			}
			else
			{
				cout << "An incorrect guess";
			}
			break;
		}
	case 5:
		{
			if (number == guess)
			{
				cout << "A correct guess!";
			}
			else
			{
				cout << "An incorrect guess";
			}
			break;
		}
	}
Thanks a lot brain stormer, but don't we need to ask the user to enter a digit 1-5 first?
For instance something like cout<< "Enter a digit 1-5";

Does number in line 1 have to be declared using int? Thx for your help.
@Brainstormer: I know that is just an example, but if that is your switch statement then there is no need for a switch statement.

@Banshee1: Please post the code that you have written. I read your first post as "do my homework".
Is it not better to write this?
1
2
3
4
5
6
int number,guess=3;
cin >> number;
if (number==guess)
   cout << "Guessed";
else
   cout << "Error";


You are complicating something that's very easy, guys.
He said:
that uses a switch statement
I assumed from this that the assignment required a switch statement, I did not say that it was the best choice. And yep that's right Banshee1, I was just providing an example for the switch statement, you seem to know yourself what to do for the rest.

But yes, you are right of course. I started righting it as if 1 == guess etc. and for some reason changed it before posting.
Last edited on
Yes, brainstormer is correct, I need to use a switch statement. I am still having trouble and will post my code, hopefully someone will be able to spot my mistakes. thanks,

# include <iostream>
using namespace std;

int main()
{
//Prompt the user to enter a digit 1-5
cout << "Enter a digit 1-5";
int number;
cin >> number;
int guess = 3


switch (number)
{
case 1:
{
if (1 == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 2:
{
if (2 == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 3:
{
if (3 == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 4:
{
if (4 == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 5:
{
if (5 == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 5:
{
if (intNumber>5 or <1 == guess)
{
cout << "Invalid guess!";

}
break;

}

return 0;

}
bump...
You have two case 5:.

An important part of the switch is the default case, which happens if you haven't made a case for that occurence.

This switch defaults unless you type 1:
1
2
3
4
5
6
cin >> number;
switch(number)
{
case 1:  cout <<  number << std::endl; break;
default: cout << -number << std::endl;
}


(think my quote button is gone for good this time, g and h are next... This 12 yr old laptop is getting close to retirement. Has a real tough time with displaying fancy web pages...)

Anyway, the default is where you handle if the user enters numbers that are out of range.
Last edited on
You can also use switch statements like if statements... although I'm not sure that would fit your assignment.

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

int main()
{
    int guess, number = 3;
    std::cout << "Enter an integer between 1 and 5: ";
    std::cin >> guess;

    switch (guess > 0 && guess < 6)
    {
        case true:
            {
                switch (guess == number)
                {
                    case true:
                        std::cout << "You guessed " << guess << "and you were correct!";
                        break;
                    default:
                        std::cout << "Incorrect!";
                }
            }
            break;
        default:
            std::cout << "Error! You must enter a number between 1 and 5!";
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.