hello
i have given a program which uses the class. It reads the date of birth. And do all the necessary checks on the given value. e.g. What is the limit of that particular month, whether it is a leap year or not. I am writting the first program using classes. I am having problemin my switch statements which i used for checks the more I try to solve the more I get confused. Need your help. Please check my program. I know it is having many other mistakes also.

#include<iostream.h>
#include<conio.h>
class date
{
public:
date();
void setdate(int, char, int);
void printdate();
private:
int day;
char month[50];
int year;
};
date::date()
{
day=1;
month= 'January';
year= 1990;
}
void date::setdate(int d, char m[50], int y)
{
day = d;
month =m; 
year = (y>= 1900 && y<= 3000)? y : 1900;
switch (m)
{
case 1: 'January'
    d>=1&&d<=31;
    break;
case 2: 'Febrary'
    {
    if (y%4==0)
    d>=1&&d<=29;
    else
    d>=1&&d<=28;
    }
    break;
case 3: 'March'
    d>=1&&d<=31;
    break;
case 4: 'Apirl'
    d>=1&&d<=30;
    break;
case 5: 'May'
    d>=1&&d<=31;
    break;
case 6: 'June'
    d>=1&&d<=30;
    break;
case 7: 'July'
    d>=1&&d<=31;
    break;
case 8: 'August'
    d>=1&&d<=31;
    break;
case 9: 'September'
    d>=1&&d<=30;
    break;
case 10: 'October'
    d>=1&&d<=31;
    break;
case 11: 'November'
    d>=1&&d<=30;
    break;
case 12: 'December'
    d>=1&&d<=31;
    break;
}
}
void date::printdate()
{
cout<<day<<" "<<month<<" "<<year<<endl;
}
void main()
    {
    date da;
    da.setdate(20,Febrary,1990)
    cout<<"The date of birth is : ";
    da.printdate;
    getch();
    }

You are attempting to perform a switch test on a char[50] array. In c++, the switch structure is designed to perform tests on the current state of a single integral expression (the most commonly used are 'int' and 'char').

Additionally, even if you were testing the correct data type, the syntax of the rest of your switch structure does not make sense and is syntactially incorrect. Here is an example:

//Cannot perform a switch on cstrings (why do you have string literals in char notation?)
case 1: 'January'    
//Here we have boolean logic..  but where is the if( ) ??!?
d>=1&&d<=31;    
//Even if your condition tested true..  what do we do now??!?  (You have NOTHING)
break;

Here is one example of a correct switch:

switch(m[0])
{
     case 'J':  //Do something useful
     break;
     case 'F':  //Dazzle us with your coding skills 
     break;
     case 'M':  //More c++ goodness goes here
     break;
     ...
     ....
     etc. etc.

Although the aforementioned scheme is not perfect (several months start with the same first letter and provisions should be made to account for such cases), at least it is a demonstration of a good working switch.

Another option would be to use if() else logic to perform tests on your cstrings:

if(strcmp(m, "January"))
{
     if( !(d >= 1 && d <= 31))
     {
          prompt_error_message();
     }
}
....
......
etc etc.

Learn more here.

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.