# Thread: i think i have gone overboard this time

1. ## i think i have gone overboard this time

My assignment is to create a program that calculates the number of days from Jan 1, 2004 to a date in the year the user enters. It has to recognize invalid dates (such as feb 30) and the user should prompt the user to try again until they make a valid entry.

I really have no idea what I am doing, but I hope somebody can at least guide me through to help me set up the valid date recognitions. This is what I came up with so far. I know it is probably nowhere near correct, but I tried!

Code:
```#include <iostream>
using namespace std;

const char * const months [12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
int months;
int days;
cout << "This program calculates the number of days from Jan 1, 2004 to a date in the year 2004 that you specify." << endl;
cout << "Please enter a month (1-12): ";
cin >> months;
while (months<13)
{cout<< months << "is not a valid entry for the month.  Please enter a value between 1 and 12: ";
cin >> months;}
cout << "Please enter a day of the month: ";
cin >> days;
while (days[months-1], days[months-3], days[months-5], days[months-7], days[months-8], days[months-10], days[months-12] <=31; days[months-2]<=29; days[months-4], days[months-6], days[months-9], days[months-11] <=30)
{cout << days << "is not a valid day in the month of February.  Please enter a value between 1 and " << days[months-1] << ": ";
cin >> days;
}
return 0;
}```

2. Code:
`while (days[months-1], days[months-3], days[months-5], days[months-7], days[months-8], days[months-10], days[months-12] <=31; days[months-2]<=29; days[months-4], days[months-6], days[months-9], days[months-11] <=30)`
woah, let me collect myself after reading that line.......
Ok, you shouldn't subtract anything from months, what if they enter 1? it will blow up on all of them except the first. I wouldn't try and shove it all in one statement either, but you seem to be on the right track.

3. lol, you are right. should i try using the if else statement something like
if (days[months-1] >31)
cout << "Please enter a valid day: "; ?

should it be [months -1] since the array is from 0-11?

4. Most of the problem is that you have declared months and days twice at different scope levels. Give the variables different names. You might use chosen_month and chosen_day.

Then comment out the code and work line by line. Get one line working before uncommenting the next and so on.

5. yay! at least i got something to work!

Code:
```#include <iostream>
using namespace std;

const char * const months [12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
int chosen_month;
int chosen_day;
cout << "This program calculates the number of days from Jan 1, 2004 to a date in the year 2004 that you specify." << endl;
cout << "Please enter a month (1-12): ";
cin >> chosen_month;
while (chosen_month>13)
{cout<< chosen_month << "is not a valid entry for the month.  Please enter a value between 1 and 12: ";
cin >> chosen_month;}
cout << "Please enter a day of the month: ";
cin >> chosen_day;
while (chosen_day > days[chosen_month-1])
{cout << chosen_day << "is not a valid day in the month of "<< months[chosen_month] << ".  Please enter a value between 1 and " << days[chosen_month-1] << ": ";
cin >> chosen_day;}
return 0;
}```
can i just print a cout after the while statements? should i use a different statement, perhaps the if or the for statement?

6. could it be something like this? I am trying to figure out what is wrong with this code, but am i almost there?

Code:
```#include <iostream>
using namespace std;

const char * const months [12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
int chosen_month;
int chosen_day;
cout << "This program calculates the number of days from " << endl << "Jan 1, 2004 to a date in the year 2004 that you specify." << endl;
cout << "Please enter a month (1-12): ";
cin >> chosen_month;
while (chosen_month>13)
{cout<< chosen_month << "is not a valid entry for the month.  Please enter a value between 1 and 12: ";
cin >> chosen_month;}
cout << "Please enter a day of the month: ";
cin >> chosen_day;
while (chosen_day > days[chosen_month-1])
{cout << chosen_day << " is not a valid day in the month of "<< months[chosen_month] << endl
<< ".  Please enter a value between 1 and " << days[chosen_month-1] << ": ";
cin >> chosen_day;}
cout << "The date you have entered is: " << months[chosen_month] << " " << chosen_day << ", 2004." << endl;
cout << chosen_day = days[chosen_month] + chosen_day << " days elapse between January 1, 2004 and " << endl <<
months[chosen_month] << " " << chosen_day << ", 2004." << endl;
return 0;
}```

7. Programming is not a competition to see who can use the fewest longest lines

Basic and consistent formatting will keep you from making a whole bunch of mistakes.
Code:
```#include <iostream>
using namespace std;

const char * const months [12] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
const int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
int chosen_month;
int chosen_day;

cout << "This program calculates the number of days from " << endl
<< "Jan 1, 2004 to a date in the year 2004 that you specify." << endl;
cout << "Please enter a month (1-12): ";
cin >> chosen_month;
while (chosen_month>13)
{
cout << chosen_month << "is not a valid entry for the month.  "
<< "Please enter a value between 1 and 12: ";
cin >> chosen_month;
}

cout << "Please enter a day of the month: ";
cin >> chosen_day;
while (chosen_day > days[chosen_month-1])
{
cout << chosen_day << " is not a valid day in the month of " << months[chosen_month] << endl
<< ".  Please enter a value between 1 and " << days[chosen_month-1] << ": ";
cin >> chosen_day;
}

cout << "The date you have entered is: " << months[chosen_month]
<< " " << chosen_day << ", 2004." << endl;
int elapsed_days = days[chosen_month] + chosen_day;
cout << elapsed_days
<< " days elapse between January 1, 2004 and " << endl
<< months[chosen_month] << " " << chosen_day << ", 2004." << endl;
return 0;
}```

8. Originally Posted by Salem
Programming is not a competition to see who can use the fewest longest lines
rofllmfao

Quzah.

9. while (days[months-1], days[months-3], days[months-5], days[months-7], days[months-8], days[months-10], days[months-12] <=31; days[months-2]<=29; days[months-4], days[months-6], days[months-9], days[months-11] <=30)
Programming is not a competition to see who can use the fewest longest lines
Agreed.
Nor is a competition to see who can use the longest lines. That has got to be the absolute longest line of code I've seen in a long time.

He should get an award or something, not that we should reward that type of code, but if we did give out awards for most characters and expressions in one line of code....he'd win it for sure.

10. lol. i honestly had no idea what i was doing at first. i think i figured out the code. let me know if there is anything i should change or styles to make programming more simple.

Code:
```#include <iostream>
using namespace std;

const char * const months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
int chosen_month;
int chosen_day;
int sum, i;
cout << "This program calculates the number of days from " << endl << "Jan 1, 2004 to a date in the year 2004 that you specify." << endl;
cout << "Please enter a month (1-12): ";
cin >> chosen_month;
while (chosen_month>13)
{cout<< chosen_month << "is not a valid entry for the month.  Please enter a value between 1 and 12: ";
cin >> chosen_month;}
cout << "Please enter a day of the month: ";
cin >> chosen_day;
while (chosen_day > days[chosen_month-1])
{cout << chosen_day << " is not a valid day in the month of "<< months[chosen_month-1] << endl
<< ".  Please enter a value between 1 and " << days[chosen_month-1] << ": ";
cin >> chosen_day;}
cout << "The date you have entered is: " << months[chosen_month-1] << " " << chosen_day << ", 2004." << endl;
/*for (chosen_day = days[0]; chosen_day <= days[chosen_month-1]; chosen_day+ = days[chosen_month-1]
cout << chosen_day << " days elapse between January 1, 2004 and " << endl <<
months[chosen_month-1] << " " << chosen_day << ", 2004." << endl);*/
sum = chosen_day;
for (i=0; i<chosen_month - 1; i++)
sum += days[i];
cout << sum << " days elapsed since January 1, 2004 and " << months[chosen_month -1] << " " << chosen_day << ", 2004." << endl;
return 0;
}```

11. > let me know if there is anything i should change or styles to make programming more simple.
FORMATTING!!!