input number output the corresponding month

My program is suppose to prompt user to enter a number 1-12 and output the corresponding month.
Ok I know I am missing a very important part of this program but right know I am struggling to figure out what to use. Do I need to have a string that includes all the names of the months? Also I know that I need to have something to put after the cout<<"the month is"<< something has to go here so the answer will print but I not sure what right now. I also think I need at have int month= something but not sure if it should be 1-12 or monthname.
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
 #include <iostream>
#include <string>

using namespace std;
char chr;

int main()
{
	int month;

	cout<<" Enter a number from 1-12."<<endl;
	
	if (month ==1)
		cout<< "January";
	else if (month==2)
		cout<< "February";
	else if (month==3)
		cout<<"March";
	else if (month==4)
		cout<<"April";
	else if (month==5)
		cout<<"May";
	else if (month==6)
        cout<<"June";
	else if (month==7)
		cout<<"July";
	else if (month==8)
		cout<<"August";
	else if (month==9)
		cout<<"September";
	else if (month==10)
		cout<<"October";
	else if (month==11)
		cout<<"November";
	else if (month==12)
		cout<<"December";
	else 
		cout<<"Sorry I need a number from 1-12.";
	cout<< "The month is "<<month;
	cin>>chr;
	return 0;
Last edited on
You should put names of the months into an array
1
2
3
4
5
6
7
8
9
10
    const char *array_months[]={"January","February","March","4","5","6","7","8","9","10","11","12"}; // I'm too lazy to fill the names
    int month;

    cout<<" Enter a number from 1-12."<<endl;
    cin >>month;
// make sure that user entered a correct number. If user entered an invalid number and you do not chheck it, the program might crash
    if(month<1 || month>12) 
        cout <<"Month must be between 1 and 12\n";
    else cout <<array_months[month-1]; //month-1 because the first item in the array is array_mothns[0], 
Last edited on
We have not learned arrays yet. I changed this part and add string months. It prints the month name but how do I get it to print the cout with the month at the end?[code
cout<<"Sorry I need a number from 1-12."<<endl;
else if(month<=12)
cout<< "The month is "<<months;
cin>>chr;
return 0;
}






/code]
my program was working but now I am getting an error message saying the variable "month" is being used without being initialized. What does that mean and how do I fix it?
Well, this means that you have not input the value of month as "cin>>month". well i am also a beginner, and i don't understand the term string, but i had a program similar to this for my initial lessons.
there are two things, you use if, elseif or switch. i prefer switch, but if u prefer, if elseif, u can use the second code.
in your code, first you output the name of the month, then you otput the sentence!, it is not required.
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
#include<iostream.h>
#include<conio.h>
void main()
{
	int month;

	cout<<" Enter a number from 1-12.";
	cin>>month;
switch (month)
{
	case 1:
		cout<< "The month is January";
                break;
	case 2:
		cout<< "The month is February";
                break;
	case 3:
		cout<<"The month is March";
                break;
	case 4:
		cout<<"The month is April";
                break;
	case 5:
		cout<<"The month is May";
                break;
	case 6:
                cout<<"The month is June";
                break;
	case 7:
		cout<<"The month is July";
                break;
	case 8:
		cout<<"The month is August";
                break;
	case 9:
		cout<<"The month is September";
                break;
	case 10:
		cout<<"The month is October";
                break;
	case 11:
		cout<<"The month is November";
                break;
	case 12:
		cout<<"The month is December";
                break;
	default: 
		cout<<"Sorry I need a number from 1-12.";
                break;
}
getch();

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.h>
#include<conio.h>
void main()
{
	int month;

	cout<<" Enter a number from 1-12.";
	cin>>month;

	if (month ==1)
		cout<< "The month is January";
	else if (month==2)
		cout<< "The month is February";
	else if (month==3)
		cout<<"The month is March";
	else if (month==4)
		cout<<"The month is April";
	else if (month==5)
		cout<<"The month is May";
	else if (month==6)
                cout<<"The month is June";
	else if (month==7)
		cout<<"The month is July";
	else if (month==8)
		cout<<"The month is August";
	else if (month==9)
		cout<<"The month is September";
	else if (month==10)
		cout<<"The month is October";
	else if (month==11)
		cout<<"The month is November";
	else if (month==12)
		cout<<"The month is December";
	else 
		cout<<"Sorry I need a number from 1-12.";

getch();
Last edited on
Topic archived. No new replies allowed.