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
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.
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!";
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.