Easier way to write a switch or if statement

This is a Homework assignment, so I do not want the answer just a hint in the right direction. I am trying to write a code using if or switch statements that will take a number the user enters and reply with the corresponding Month, such as 5 = May. I am tired so it is racking my brain a little. I know this may seem like a simple statement, and I could use a ton of if statements, but I was looking for a better way if possible? thanks in advance for ANY advice.
this is what I have:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main()

{
	

	int num1, num2; 

		cout << "Please enter two numbers 1 - 100 seperated by a space, then press Enter,  and the computer will tell you which number is greater." << endl;
		cin >> num1 >> num2; 
		if (num1 > num2)
			cout << num1 << " ""is greater than " << num2 << endl; //my if statements, 
		if (num2 > num1)
			cout << num2 << " ""is greater than " << num1 << endl;
		if (num1 == num2)
			cout << num1 << " ""is eqaul to " << num2 << endl;
		cout << " " << endl;
		cout << "Now enter in a number from 1 - 12 and the program will tell you it's corresponding month. For example 5 = May." << endl;
        
	
		
		int month;

			cin >> month;
			if (month = 1)
				cout << "January" << endl;
			if (month = 2)
				cout << "February" << endl; 
			if (month = 3)
				cout << "March" << endl;
// There has to be a shorter way of doing this, or is there not? 
Arrays.
Thank you. I was thinking this, but we have not covered it yet. I do know how to do this though so I think I will add an array. Thanks again
I forgot to mention, (because I overlooked it) it has to be a switch statement. I have completed the code but was just wondering if there was a more simple way to write it? if not then cool thanks a million, if there is I would love to know it.
also yes I know system ("pause") is not an acceptable way to stop a program but it is what we are told to do.

this is what I have:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

using namespace std;

int main()

{
	

	int num1, num2; 

		cout << "Please enter two numbers seperated by a space, then press Enter,  and the computer will tell you which number is greater." << endl;
		cin >> num1 >> num2; 
		if (num1 > num2)
			cout << num1 << " ""is greater than " << num2 << endl; //my if statements, 
		if (num2 > num1)
			cout << num2 << " ""is greater than " << num1 << endl;
		if (num1 == num2)
			cout << num1 << " ""is eqaul to " << num2 << endl;
		cout << " " << endl;
		cout << "Now enter in a number from 1 - 12 and the program will tell you it's corresponding month. For example 5 = May." << endl;
        
	

		
		
		int month;


			cin >> month;
			cout << " " << endl;

			switch (month){
				case 1:
					cout << "Januaryis the month"" ";
					break;
				case 2:
					cout << "February is the month"" ";
					break;
				case 3:
					cout << "March is the month"" ";
					break;
				case 4:
					cout << "April is the month"" ";
					break;
				case 5:
					cout << "May is the month"" ";
					break;
				case 6:
					cout << "June is the month"" ";
					break;
				case 7:
					cout << "July is the month"" ";
					break;
				case 8:
					cout << "August is the month"" ";
					break;
				case 9:
					cout << "September is the month"" ";
					break;
				case 10:
					cout << "October is the month"" ";
					break;
				case 11:
					cout << "November is the month"" ";
					break;
				case 12:
					cout << "December is the month"" ";
					break;
				default:
					cout << "error you must enter a number 1-12"" ";
			}
			cout << " " << endl;

			system ("pause");
			return 0;
}
Yes, like helios said, use arrays.
Why is your teacher making you use a switch statement? Next time you are at that class ask them about arrays
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
#include <iostream>

using namespace std;

int main()

{
	int num1, num2, month; 
	string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

	cout << "Please enter two numbers seperated by a space, then press Enter,  and the computer will tell you which number is greater." << endl;
	cin >> num1 >> num2; 
	if (num1 > num2)
		cout << num1 << " ""is greater than " << num2 << endl; //my if statements, 
	else if (num2 > num1)
		cout << num2 << " ""is greater than " << num1 << endl;
	else
		cout << num1 << " ""is equal to " << num2 << endl;
	cout <<  endl;
	
	cout << "Now enter in a number from 1 - 12 and the program will tell you it's corresponding month. For example 5 = May." << endl;
    cin >> month;
	cout << endl;

	if (month > 0 && month < 13)
	{
		cout << months[month] << " is the month." << endl;
	}
	else
	{
		cout << "You must enter a number between 1 and 12." << endl;
	}
	
	system ("pause");
	return 0;
}
Line 27 should be cout << months[month - 1] << " is the month." << endl;.
yes because array starts from 0, January = 0
yea my bad I just realized when looking at it today lol
yes, that is a nice switch statement.
exactly correct.

A+
Topic archived. No new replies allowed.