One-deminsional arrays

I am building a program that allows the user to pick a number 1 - 12 (for the 12 months in a year). Once they choose a number the program will display how many days are in that month. This is what I have so far.

#include <iostream>
using namespace std;

int main()
{
//declare arrays
int types[12] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'};
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//declare variables
int month = 0;

//get month to display
cout << "Which month would you like to display: " << endl;
cin >> month;


Is this correct? What should I do next?
Looks alright, though you don't need to initialize month to zero, since you assign another value to it anyway before using it, and you can remove the 12 in types[12] and days[12]. They're not needed since you assign all the values to them at the same time you declare them. You don't need to make those changes though, just a few tips to improve your code a tiny bit. Also, you forgot return 0 and a closing bracket at the end of your code. And please use [ code ] tags.

What you should do after that, is to output the correct amount of days in the month entered. You can for example do it like this: cout << "Days in the entered month: " << days[month-1] << endl; The reason month-1 is there, is because the first integer in the array is days[0], not days[1]. So February for example is days[1], not days[2]. Also, if you do it this way, you don't need types[]. Hope this helps!
Last edited on
What part is missing that would apply the days to the month? I guess you would say the calcuation part?!?!
Last edited on
I updated my answer. Did it a little bit late though :3
Last edited on
Something like this?


#include <iostream>
using namespace std;

int main()
{
//declare arrays
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//declare variables
int month = 0;

//get month to display
cout << "Days in the entered month: " << days[month-1] << endl;
cin >> month;

return 0;
}
No. Just keep the original cout you had right before the cin, and add the new line of code under cin >> month.
#include <iostream>
using namespace std;

int main()
{
//declare arrays
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//declare variables
int month = 0;

//get month to display
cout << "Which month would you like to display: " << endl;
cin >> month;
cout << "Days in the entered month: " << days[month-1] << endl;

system ("pause");
return 0;

}

Works perfect!

How do I set this up with a sentinel? I want to be able to go through all the dates until I stop the program. Do I use the While?
Do you mean you want to display the amount of days in all 12 months? If so, a for loop would do the job.
Basically it keeps giving you the option to enter a number until you stop the program or you enter an invalid number.
Aah. Yes, then a while loop would fit.
Ok. I am sitting here working on this loop, and I have to wonder is there an easier way to write this. Do I "need" to write out each month as while(1==31), (2==28), and so on?
You can't make a while loop like that, because 1 will never, ever be equal to 31. A loop will run as long as its condition (the code within the parentheses) is true. What you can do is to make a loop like this from after int month, to before return 0:

1
2
3
4
while(month >= 1 && month <= 12)
{
     //enter month number here and output amount of days
}


This loop will run as long month is larger than or equals 1, and less than or equals 12. Also, you would need to define month as 1, not 0. Unless you know how to use a do while loop.
Last edited on
This 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
#include <iostream>
using namespace std;

int main()
{
//declare arrays
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//declare variables
int month = 0;

//get month to display

do {
cout << "Which month would you like to display (0 to stop): ";
cin >> month;
if (month >= 1 && <=12); {
cout << "Days in the entered month: " << month << endl;
}
else { 
cout << "Number entered is invalid: "; << endl;
}
} while(month >= 1 && month <= 12);


system ("pause");
return 0;
}


Does any of this look correct? I am still getting this off the wall number when I enter the sentinel or and invalid number.
You need to remove the semicolon after the condition in your if-statement, and the semicolon before endl in your else. And regarding the output, please read my first post again. Also, have you tried running your program? Your compiler should complain and give you errors.
Last edited on
Thanks for the help!
if (month >= 1 && <=12)
should have been
if (month >= 1 && month <=12)
Everything works perfect...
Again, thank you!
Topic archived. No new replies allowed.