My first post ....12 element array


I'm trying to code a program that displays the number of days corresponding to the month numbered entered by the user
I'm wanting to declare a 12 element int array named days, assign the number of days in each month to the array.

I'm running into some issues.

1st - do I just copy the code here for review or attach a txt file ... whats the correct process?
[code]Your code[/code]
I'm not sure what that means


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{

//declare global arrays       
int days [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

int month [12] = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");




cout << "Enter a number of the month to see the number of days in that month: ";
cin >> days;


// display days per month

cout << "Days of this month equal: " << days << endl


 system("pause");

 return 0;       
You can't assign string values to an array of int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{

	int num; // Stores the month the user wants to see how many days are in.
	int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; 


	cout << "Enter a number of the month to see the number of days in that month: ";
         // getting the month the user wants to see the number of days in that month
	cin >> num;
          // decrementing num because arrays begin at 0.
	num--;

	// display days per month


	cout << "Days of this month equal: " << days[num] << endl; 

	return 0;
}
ok I think see what your saying ...thank you

What if you also want it to return an invalid selection, "invalid entry": , say if the user entered 13?

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
int main()
{

	int num;
	int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};


	cout << "Enter a number of the month to see the number of days in that month: ";
	cin >> num;

	// display days per month

	if(num > 0 && num <= 12)
	{
		--num;
		cout << "Days of this month equal: " << days[num] << endl;
	}
	else
	{
		cout << "Invalid month";
	}


	return 0;
}
ok

I was thinking it would be a while statement but the if/else makes sense.
Thanks for your help ... much appreciated
If you want it to run more then once then yes you would use a while loop. If you only want it to run once the if/else will do.
hi,I'm a chinese student.
eh,sorry,my english is poor.
I came here by chance. I wonder whether you are foreign people?
ok.

I may try to make hte while statement work then so it does ask more than once.
Thanks for the help
Topic archived. No new replies allowed.