You are using a version without Ads of this website. Please, consider donating:

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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` `````` 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?
 ``123456`` ``````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:
 ``123456`` ``````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.

 ``12345678910111213141516171819202122232425262728`` ``````#include 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.

You are using a version without Ads of this website. Please, consider donating: