i think i have gone overboard this time

This is a discussion on i think i have gone overboard this time within the C++ Programming forums, part of the General Programming Boards category; My assignment is to create a program that calculates the number of days from Jan 1, 2004 to a date ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    Unhappy 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. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    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. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    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. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    Programming is not a competition to see who can use the fewest longest lines
    rofllmfao

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    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. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > let me know if there is anything i should change or styles to make programming more simple.
    FORMATTING!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

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