Thread: Display day, month, year in different format

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    Display day, month, year in different format

    Hi!
    I'm trying to write a program that will take in three numbers (day, month, year), and display them in different format (MM/DD/YYYY; Month in words, DD, YYYY; and DD Month in words YYYY).
    Here's what I have so far, I can't figure out what's wrong.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    const int NUM_MONTHS = 12;
    const int NAMESIZE = 10;
    
    class Date
    {
    private:
            int month, day, year;
            char names[NUM_MONTHS][NAMESIZE];
           
    public:
    
    
    Date::Date
    { setNames(); }
    
    Date::Date(int m, int d, int y)
    {
           setMonth(m);
           setDay(d);
           setYear(y);
           setNames();
    }
    
    
    
    void Date::setNames()
    {
         strcpy(names[0], "January");
         strcpy(names[1], "Feburary");
         strcpy(names[2], "March");
         strcpy(names[3], "April");
         strcpy(names[4], "May");
         strcpy(names[5], "June");
         strcpy(names[6], "July");
         strcpy(names[7], "August");
         strcpy(names[8], "September");
         strcpy(names[9], "October");
         strcpy(names[10], "November");
         strcpy(names[11], "December");
    }
    
    void Date::setMonth(int m)
    {
         if (m >= 1 && m <= 12)
         month=m;
         else
         {
             cout << m << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::setDay(int d)
    {
         if (d >= 1 && d <= 31)
         day=d;
         else
         {
             cout << d << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::setYear(int y)
    {
         if (y <= 1)
         year=y;
         else
         {
             cout << y << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::showDate1()
    {
         cout << month << "/"
         << day << "/"
         << year << endl;
    }
             
    void Date::showDate2()
    {
         cout << names[month+1]
         << " " << day << ", "
         << year << endl;
    }     
    
    void Date::showDate3()
    {
         cout << day << " "
         << month << " "
         << year << endl;
    }
    
    int Date::getMonth() const
    {
           return month;
    }
    
    int Date::getDay() const
    {
           return day;
    }
    
    int Date::getYear() const
    {
           return year;
    }
    
    };
    
    
    int main()
    {
        int number;
        Date show;
        Date month;
        Date day;
        Date year;
            
        cout << "What is the month? ";
        cin >> number;
        month.setMonth(number);
        
        cout << "What is the day? ";
        cin >> number;  
        day.setDay(number);
        
        cout << "What is the year? ";
        cin >> number;  
        year.setYear(number);
    
        system("PAUSE");
        return 0;
    }
    On line 19 (I bolded it), it tells me "expected unqualified-id before { token". I tried searching through this forums for similar problems, but none of them applies to me (some I dont quite understand - still new to C++).

    And in main (I boded them also), I can't quite figure out how I should fix the problem.


    Thank you so much!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably
    Code:
    Date::Date()
    { setNames(); }
    But why not to have names as static member? Why to copy all this strings to each instance of the class?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Date::
    You don't need the class scope on all the functions you declare within the class itself.

    You also only need one Date variable inside main, which you set the day, month, year for.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    I haven't gotten to the chapter on static member yet. But, I'll look into that and see if I can make this work somehow.

    Thank you for your reply!

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Thank you guys!
    I fixed the errors and decided to just input data manually. How do I display the final product?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    const int NUM_MONTHS = 12;
    const int NAMESIZE = 10;
    
    class Date
    {
    private:
            int month, day, year;
            char names[NUM_MONTHS][NAMESIZE];
           
    public:
    
    
    
    Date::Date(int m, int d, int y)
    {
           setMonth(m);
           setDay(d);
           setYear(y);
           setNames();
    }
    
    
    
    void Date::setNames()
    {
         strcpy(names[0], "January");
         strcpy(names[1], "Feburary");
         strcpy(names[2], "March");
         strcpy(names[3], "April");
         strcpy(names[4], "May");
         strcpy(names[5], "June");
         strcpy(names[6], "July");
         strcpy(names[7], "August");
         strcpy(names[8], "September");
         strcpy(names[9], "October");
         strcpy(names[10], "November");
         strcpy(names[11], "December");
    }
    
    void Date::setMonth(int m)
    {
         if (m >= 1 && m <= 12)
         month=m;
         else
         {
             cout << m << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::setDay(int d)
    {
         if (d >= 1 && d <= 31)
         day=d;
         else
         {
             cout << d << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::setYear(int y)
    {
         if (y <= 1)
         year=y;
         else
         {
             cout << y << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    void Date::showDate1()
    {
         cout << month << "/"
         << day << "/"
         << year << endl;
    }
             
    void Date::showDate2()
    {
         cout << names[month+1]
         << " " << day << ", "
         << year << endl;
    }     
    
    void Date::showDate3()
    {
         cout << day << " "
         << month << " "
         << year << endl;
    }
    
    int Date::getMonth() const
    {
           return month;
    }
    
    int Date::getDay() const
    {
           return day;
    }
    
    int Date::getYear() const
    {
           return year;
    }
    
    };
    
    
    int main()
    {
        int number;
    Date today (12, 25, 2006);
    today.showDate1();
    
    today.setMonth(12);
    today.setDay(25);
    today.setYear(2006);
    
    today.showDate2();
    today.showDate3();
    
    system("PAUSE");
    return 0;
    }
    Thank you!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    names[month+1]
    probably month-1?

    Why not to use std:strings instead of C-strings?
    And vector instead of array?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Can anyone please help me with my code?
    By dlwlsdn in forum C Programming
    Replies: 45
    Last Post: 11-24-2008, 02:09 PM
  3. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 11:21 PM
  4. Year, month and day
    By Newbie999 in forum C Programming
    Replies: 15
    Last Post: 12-01-2006, 10:17 AM
  5. Getting the Following Date
    By BB18 in forum C Programming
    Replies: 2
    Last Post: 10-10-2004, 12:39 PM