Hey guys, I have a homework problem that I'm having issues with. I've been having a difficult time understanding classes and what the point of them is when I feel like I can just write everything in main without using a class, but I'm forced to use them anyways. This is the homework problem:

write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for the day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)

And my code so far:
#include <iostream>

using namespace std;

class invalidMonth 
{
    public:
        invalidMonth () throw();
        invalidMonth (const exception&) throw();
        invalidMonth& operator= (const exception&) throw();
}

class invalidDay 
{
    public:
        invalidDay () throw();
        invalidDay (const exception&) throw();
        invalidDay& operator= (const exception&) throw();
}

int main()
{
    int month, day, year;

    cout << "Enter the # of the month" << endl;
    cin >> month;

    cout << "Enter the day" << endl;
    cin >> day;

    cout << "Enter the year" << endl;
    cin >> year;

    cout << "The date is " << month << "-" << day << "-" << year << "." << endl;

    if(month==1)
        cout << "January ";
    else if(month==2)
        cout << "February ";
    else if(month==3)
        cout << "March ";
    else if(month==4)
        cout << "April ";
    else if(month==5)
        cout << "May ";
    else if(month==6)
        cout << "June ";
    else if(month==7)
        cout << "July ";
    else if(month==8)
        cout << "August ";
    else if(month==9)
        cout << "September ";
    else if(month==10)
        cout << "October ";
    else if(month==11)
        cout << "November ";
    else if(month==12)
        cout << "December ";
    else
        cout << "Invalid date";
    return 0;
}

I know I'm doing it wrong because I believe I'm supposed to be putting the if statement in one of my functions in the classes. I just don't know where to put it and how I'm supposed to write it. I'm having a hard time with parameters and the point of &'s so going into this new exception class issue is incredibly difficult for me when I'm already behind. I'm not asking for an answer for my homework, I'm just asking for help on where I should be putting the information. Thanks for your time!

To start with the reason you have to use classes for a program that you could just write in main is so that you get used to classes. While you are studying all of you exercises are contived and often fairly small you do them to learn the techniques that you will need once you get into the real world and start working on applications with 10,000s of lines of code. But it is easier to explorer and understand the techniques on small example classes/examples.

I would have thought you would want a data class that had 2 methods (in addition to a default constructor), 1 method to read the data from standard in and 1 method to write the date to standard out. The method to read from standard in would throw your exceptions.

You can then write a main that instantiates this class and uses a try clause to encapsulate the read/write operation outputting errors when it catches an exception.

Could you show me an example of what you mean?

For this program I'm not sure what to put in the constructors & default constructor because in the past we just assign the variables a definition. Is that what I need to do here? If so could you give me any tips as to what my variable/definition is?

Imagine you have a data class, then you would probably just need to assign the values to something valid like 0

class Date
{
public:
    Data() :
        day(0),
        month(0),
        year(0)
    {
    }

    void read(istream& in);    // Read function taking stream to read from
    void write(ostream& out);  // Write function taking stream to write to

    string getMonthString();  // Convert the month integer to a string January Feb etc

private:
    int day;
    int month;
    int year;
};

.

int main()
{
   try
   {
       Date date;

       date.read(cin);
       data.write(cout);
   }
   catch ... write catch blocks here
}

Thank you for the post, but for my homework problem it says I need 2 exception classes, not just any class. So should I write my main problem in "int main"? And how would I write everything for the exception classes? I'm sure I could figure it out if I just had an example for one of them. Thanks again.

There is nothing special about an exception class, in fact if anything I would expect it to be simpler than a normal class, if you are in an exceptional case you do not want to be using lots of additional resources.

I have given you an outline for a class and main, you can write your own exception classes or take the ones from your own post 1, I would expect the code in read to output the exceptions if the data provided was invalid.

program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980.The program then outputs the date of birth in the form: august 27, 1980.Your program must contain at least two exception classes; invalidDay and invalidMonth.
can some one pls help, i'm new to exception classes

@naysa: Don't hijack an old thread like this. You should make a new thread to ask your question. And don't forget to show some efforts of your own towards solving your problem. We don't just provide ready-made answers to people's homework assignments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.