GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-May-2005, 00:57
dazzer143 dazzer143 is offline
New Member
 
Join Date: May 2005
Posts: 2
dazzer143 is on a distinguished road

help with displaying year, month, and date.


Hi all, I was trying to program to display Month name, date, and year.
the output should be something like this
Enter year: 2004
Enter month by using number 1 - 12 only: 1
Enter day: 1
January 1, 2004 is day number 1.
Enter year: ........... (and so on)

here are sample of my code. not sure what i did wrong...

CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

bool isLeapYear( int year );

int main()
{

    string month[13] = { "---", "January", "February", "March", "April", "May",
             "June", "July", "August", "September", "October", "November",
             "December" };
    int daysInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int index = 0;
    int monthNumber = 0;
    int monthSubscript = 0;
    const int FEB_SUBSCRIPT = 1;
    int year = 0;
    int dayNumber = 0;
    int maxDays = 0;
    const int FEBRUARY = 2;
    int day = 0;
    const int ZERO_LIMIT = 0;
    const int YEAR_LIMIT = 1800;
    const int MONTH_LIMIT = 12;
    const int COUNTER = 1;
    const int ADD_DAY = 1;      
    int monthCount = 0;

    cout << "Enter year: ";
    cin >> year;
    cout << "Enter month by using number 1 - 12 only: ";
    cin >> monthNumber;
    cout << "Enter day: ";
    cin >> day;
    
    monthSubscript = monthNumber - COUNTER;
    
    if (year >= YEAR_LIMIT)
    {
        if (monthNumber > ZERO_LIMIT && monthNumber <= MONTH_LIMIT)
        {
             maxDays = daysInMonth[monthSubscript];
        }
            if (day > ZERO_LIMIT && day <= maxDays)
            {
                 while (index < monthNumber)
                 {
                      monthCount = monthCount + daysInMonth[index];
                      index = index + COUNTER;
                 }
                 if (isLeapYear( year ) && monthNumber > FEBRUARY)
                 {             
                        if (monthNumber == FEBRUARY)
                        {
                             daysInMonth[FEB_SUBSCRIPT] = daysInMonth[FEB_SUBSCRIPT] + 1;     
                             dayNumber = monthCount - daysInMonth[monthSubscript];
                             cout << month[monthSubscript] << " " << day 
                                  << ", " << year << endl;
                        }
                        else       
                        { 
                             dayNumber = monthCount - daysInMonth[monthSubscript];
                             cout << month[monthSubscript] << " " << day 
                                  << ", " << year << endl;
                        }
                 }
                 else
                 {
                      cout << day << " is not a valid number for " 
                           << month[monthSubscript] << endl;
                 }
            }
        else
        {
             cout << "There are 12 months in a year. Choose months from 1 to 12."
                  << endl;
        }
    }
    else
    {
         cout << "The year cannot be below 1800." << endl;
    }

    cout << "Enter year: ";
    cin >> year;
    cout << "Enter month by using number 1 - 12 only: ";
    cin >> monthNumber;
    cout << "Enter day: ";
    cin >> day;
    
	system("PAUSE");
	return 0;
}

bool isLeapYear( int year )
{
    bool result = false;
    result = (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
    return result;
}

thank you in advance
Last edited by LuciWiz : 18-May-2005 at 01:08. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 18-May-2005, 06:30
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 225
Kacyndra will become famous soon enough
does it give you an error? or are you getting the wrong output?
can you post either the output or the errors here also.
__________________
Xrum!
  #3  
Old 18-May-2005, 07:55
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about
Quote:
Originally Posted by dazzer143
not sure what i did wrong...
If you are not familiar with or do not have a debugger. You can debug like this.

CPP / C++ / C Code:
monthSubscript = monthNumber - COUNTER;
To check that you could do something like,
CPP / C++ / C Code:
cout << "monthNumber - COUNTER = " << monthSubscript << endl;

To check this,
CPP / C++ / C Code:
if (year >= YEAR_LIMIT)
maybe this
CPP / C++ / C Code:
cout << "year >= YEAR_LIMIT = " << boolalpha << (year >= YEAR_LIMIT) << endl;
or
CPP / C++ / C Code:
cout << year << " >= " << YEAR_LIMIT << " = " << boolalpha << (year >= YEAR_LIMIT) << endl;

This is where you might put them,
CPP / C++ / C Code:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

bool isLeapYear( int year );

int main()
{
    string month[13] = { "---", "January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October", "November",
        "December" };
    int daysInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int index = 0;
    int monthNumber = 0;
    int monthSubscript = 0;
    const int FEB_SUBSCRIPT = 1;
    int year = 0;
    int dayNumber = 0;
    int maxDays = 0;
    const int FEBRUARY = 2;
    int day = 0;
    const int ZERO_LIMIT = 0;
    const int YEAR_LIMIT = 1800;
    const int MONTH_LIMIT = 12;
    const int COUNTER = 1;
    const int ADD_DAY = 1;     
    int monthCount = 0;

    cout << "Enter year: ";
    cin >> year;
    cout << "Enter month by using number 1 - 12 only: ";
    cin >> monthNumber;
    cout << "Enter day: ";
    cin >> day;

    monthSubscript = monthNumber - COUNTER;
    cout << "monthNumber - COUNTER = " << monthSubscript << endl;//added
   
    cout << "year >= YEAR_LIMIT = " << boolalpha << (year >= YEAR_LIMIT) << endl;//added
    cout << year << " >= " << YEAR_LIMIT << " = " << boolalpha << (year >= YEAR_LIMIT) << endl;//added
    if (year >= YEAR_LIMIT)
    {
        cout << "monthNumber > ZERO_LIMIT && monthNumber <= MONTH_LIMIT = " << boolalpha
            << (monthNumber > ZERO_LIMIT && monthNumber <= MONTH_LIMIT) << endl;//added

        if (monthNumber > ZERO_LIMIT && monthNumber <= MONTH_LIMIT)
        {
            maxDays = daysInMonth[monthSubscript];
            cout << "maxDays = " << maxDays << endl;//added
        }
        cout << day << " > " << ZERO_LIMIT << " && " << day << " <= "
            << maxDays << " " << boolalpha << (day > ZERO_LIMIT && day <= maxDays) << endl;//added
        if (day > ZERO_LIMIT && day <= maxDays)
        {
            while (index < monthNumber)
            {
                monthCount = monthCount + daysInMonth[index];
                index = index + COUNTER;
            }
            if (isLeapYear( year ) && monthNumber > FEBRUARY)
            {            
                if (monthNumber == FEBRUARY)
                {
                    daysInMonth[FEB_SUBSCRIPT] = daysInMonth[FEB_SUBSCRIPT] + 1;    
                    dayNumber = monthCount - daysInMonth[monthSubscript];
                    cout << month[monthSubscript] << " " << day
                        << ", " << year << endl;
                }
                else      
                {
                    dayNumber = monthCount - daysInMonth[monthSubscript];
                    cout << month[monthSubscript] << " " << day
                        << ", " << year << endl;
                }
            }
            else
            {
                cout << day << " is not a valid number for "
                    << month[monthSubscript] << endl;
            }
        }
        else
        {
            cout << "There are 12 months in a year. Choose months from 1 to 12."
                << endl;
        }
    }
    else
    {
        cout << "The year cannot be below 1800." << endl;
    }

    cout << "Enter year: ";
    cin >> year;
    cout << "Enter month by using number 1 - 12 only: ";
    cin >> monthNumber;
    cout << "Enter day: ";
    cin >> day;

    system("PAUSE");
    return 0;
}

bool isLeapYear(int year)
{
    bool result = false;
    result = (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
    return result;
}
I did not add one everywhere but you get the idea and can add any additional ones.
  #4  
Old 18-May-2005, 15:46
dazzer143 dazzer143 is offline
New Member
 
Join Date: May 2005
Posts: 2
dazzer143 is on a distinguished road
I just got the wrong output .. but i figured it out now.. thank you guys.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to create a calendar eleet C Programming Language 4 17-Mar-2005 22:34
50% OFF Basic Reseller Plan - FREE Domain and DOUBLE Bandwidth - From $5 per month Dailyhosting Web Hosting Advertisements & Offers 0 16-Mar-2005 16:06
Array indexers and passing by reference ap6118 C++ Forum 2 07-Mar-2005 09:20
need a little help ap6118 C++ Forum 2 28-Feb-2005 11:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.