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.
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!
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?
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.
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.