String (Month Name)

How to do this same thing with string library? Storing January in 1? Is this possible?

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
#include <iostream>
using namespace std;
int main()
{
	int num;

	cout << "Enter order number of month: ";
	cin >> num;

	if (num == 1)
	{
		cout << "January";
	}
	else if (num == 2)
	{
		cout << "February";
	}
	else if (num == 3)
	{
		cout << "March";
	}
	else if (num == 4)
	{
		cout << "April";
	}
	else if (num == 5)
	{
		cout << "May";
	}
	else if (num == 6)
	{
		cout << "June";
	}
	else if (num == 7)
	{
		cout << "July";
	}
	else if (num == 8)
	{
		cout << "August";
	}
	else if (num == 9)
	{
		cout << "September";
	}
	else if (num == 10)
	{
		cout << "October";
	}
	else if (num == 11)
	{
		cout << "November";
	}
	else if (num == 12)
	{
		cout << "December";
	}
	else if (num < 1 || num > 12)
	{
		cout << "There is no month exist with your given number.";
	}

	cout << endl << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string month[] = { "", "January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December" };
   int num;
   cout << "Enter number of month: ";   cin >> num;
   if ( num < 1 || num > 12 ) cout << "No such month\n";
   else                       cout << month[num] << '\n';
}
lastchance just curious why is "" added before January, when I removed it. Then put in 2 and it goes to March. What exact does " " does?
Last edited on
Array indices start at 0. The empty string at index 0 just offsets the number that the month corresponds with, to match how we actually use dates (1 = January, 2 = February, etc.)
Last edited on
@soulworld05,
Yes, Ganado read it right. Sometimes the fact that C/C++ array indices start at 0 is inconvenient, for example for months of the year, ... or pay-day.

The 0th element won't be used, but it is not costly to ensure that array index corresponds directly to the conventional calendar month. Sometimes I make the intention more obvious by writing the array size as
1
2
   string month[1+12] = { "", "January", "February", "March", "April", "May", "June",
                              "July", "August", "September", "October", "November", "December" }


Hello HelpMeBr,

The next challange, if you have learned it, would be to change the if/else if statements to a switch.

The other thing I would is make the string a constant because you do not want the program changing any of these values.

Andy
Thanks, everybody.
Andy, I have a lecture on switches today. I studied if/else if statements and practiced a lot of programs and still have many to do(I am getting these programs from random websites). I am really in love with programming. I will practice on switches from today too. Thanks for your advice.
Also, can you upload a link here for strings in detail? Like, how to set range in strings etc.
Last edited on
Thanks.
Topic archived. No new replies allowed.