Switch Classwork?

I am supposed to be writing a program that will say what a user inputted number is (low odd, low even, etc.), but I do not know how.

Here is my code so far:

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
#include <iostream>
using namespace std;
int main()
{
	int num;
	cout<<"Enter a number from 1 to 10: ";
	cin>>num;
	cout<<endl;
	switch(num)
	{
	case 0:
		if(num==1||num==3);
		cout<<"Your number is a low odd number."<<endl;
		break;
	case 1:
		if(num==2||num==4)
		cout<<"Your number is a low even number."<<endl;
		break;
	case 2:
		if(num==5)
		cout<<"Your number is the middle odd number."<<endl;
		break;
	case 3:
		if(num==6)
		cout<<"Your number is the middle even number."<<endl;
		break;
	case 4:
		if(num==7||num==9)
		cout<<"Your number is a high odd number."<<endl;
		break;
	case 5:
		if(num==8||num==10)
		cout<<"Your number is a high even number."<<endl;
		break;
	default:
		cout<<"That is an invalid number."<<endl;
	}
	
return 0;
}


It is not outputting the messages, and I need to know how to get them to output.
Last edited on
If you reach line 12, num will never be 1 or 3 and your cout will never execute. You can only reach line 12 if num is 0 (that's what the switch statement does). Ditto for the other branches of your switch statement.

Either get rid of your switch statement, or get rid of your if statements and associate your couts with the correct cases.

p.s. Why do you have a case 0, if the user is only supposed to enter 1-10?
Last edited on
AbstractionAnon wrote:

If you reach line 12, num will never be 1 or 3 and your cout will never execute. You can only reach line 12 if num is 0 (that's what the switch statement does). Ditto for the other branches of your switch statement.

Either get rid of your switch statement, or get rid of your if statements and associate your couts with the correct cases.

p.s. Why do you have a case 0, if the user is only supposed to enter 1-10?


I had no idea how to use switches when I asked for help on this. My teacher was able to help me, but she had a lot of other students to help as well. I fixed the code, unfortunately I do not remember anything about switches, but I took a good amount of notes during class, so I will refer back to those. Thanks for the help.
Topic archived. No new replies allowed.