addressbook

Printable View

• 11-27-2009
Fatima Rizwan
addressbook
i have created a class ov date in address book ,, i want the the days and months to be checked ,,whether for a specific month the days are in limits are not
so i have done this thing in the print function,, here is the code

Code:

```        switch(month)         {         case 1:                 if (day>31)                 {                 day=day-31;                 month=month+1;} //so if the days are greater then 31 so i want it to switch //to next case,, but i dun know how to do this                 else                 cout<<"January-";               break;```
• 11-27-2009
Dino
Checking for valid dates is different from date arithmetic. Which one are you asking about?
• 11-27-2009
C_ntua
In a switch statement every case is examined, except if you break out with someway (like with the break statement)
Code:

```switch(month)         {         case 1:                 if (day>31)                 {                   day=day-31;                   month=month+1;                 } else {                                  cout<<"January-";                   break;                 }           case 2:                 ...```
So, if is day is >31 it will do "month+=1" and then it will go to case 2. Well, month == 2 so it wil also do case 2. If day is <=31 it will print "January" and then break, terminating the switch statement.
• 11-27-2009
Fatima Rizwan
i just want to check if the date is valid for a specific month,,
• 11-27-2009
Fatima Rizwan
yah exactly i want it to switch it to nxt month ,,,But its not working,,,
• 11-27-2009
C_ntua
Quote:

Originally Posted by Fatima Rizwan
yah exactly i want it to switch it to nxt month ,,,But its not working,,,

My code or your code? Seeing them again I actually think that you meant to write what I wrote but messed it up a bit. Your corrected code is below (with red)
Code:

```        switch(month)         {         case 1:                 if (day>31)                 {                 day=day-31;                 month=month+1;} //so if the days are greater then 31 so i want it to switch //to next case,, but i dun know how to do this                 else {                 cout<<"January-";                             break;}```
The way you had it it would always break. So it won't go on the next case...
• 11-28-2009
Fatima Rizwan
ohk thnks,,

i have created an Addressbook ,,in which i want to add the functionality that , according to user's will,, i will print the addressbook,, by their name ,, or by according to there Date of birth (month )
so any suggestions,, how to print according to birth month,,
• 11-28-2009
Fatima Rizwan
like the person who's born in february will be abv the person who born in march
• 11-28-2009
C_ntua
First of all, what if somebody enters month=1, day=50. Then you will print that he is actually "February 19th"? Isn't that a bad idea? You should simply print "Day: Error". Because what if the user misstyped, which is probably the case??
Of course, there are cases that you may want that functionality, just make sure this is your case.

So you want to sort them by name or by month.
If you represent monts by int then just print the one with the smallest. If you don't, then you can have a translation function that does
Code:

```int translateMonth(const char* monthName) {     if (strcmp(monthName, "February") == 0)         return 1;     else if (..)         .... }```
or you can simply add on your Date struct, if you have one:
Code:

```char* monthName; char* monthValue;```
If they are on the same date then you can also check the day as well.

But sorting by name is more difficult, it is strange that you are asking about the month...
• 11-28-2009
Fatima Rizwan
Code:

```class Date { public:   void setDate();   void printDate(); private:   int month;   int day;   int year;   int checkDay( int ) const; };   void Date::setDate()   {   int mn,yr,dy;   cout<<"Enter Month:";   cin>>mn;     if ( mn > 0 && mn <= 12 )  // validate the month     month = mn;     else     {                    // invalid month set to 1     month = 1;     cout << "Month " << mn << " invalid. Set to month 1.\n";     }   cout<<"Enter Year:";   cin>>yr;     if ( yr> 1970 && yr<=2009 )     year = yr;     else     {     year= 1990;     cout<< "Year "<< yr << " invalid. Set to year 1990.\n";     }   cout<<"Enter Day:";   cin>>dy;                // should validate yr   day = checkDay( dy );      // validate the day   cout << endl; }   void Date::printDate()   {         switch(month)         {         case 1:               cout<<"January ";               break;         case 2:               cout<<"February ";               break;         case 3:               cout<<"March ";               break;         case 4:               cout<<"April ";               break;         case 5:               cout<<"May ";               break;         case 6:               cout<<"June ";               break;         case 7:             cout<<"July ";             break;         case 8:             cout<<"August ";             break;         case 9:             cout<<"September ";             break;         case 10:             cout<<"October ";             break;         case 11:             cout<<"November ";             break;         case 12:             cout<<"December ";             break;     }//end switch   cout << day <<", "<< year << endl; }   int Date::checkDay( int testDay ) const   {   static const int daysPerMonth[ 13 ] =           { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };   if ( testDay > 0 && testDay <= daysPerMonth[ month ] )       return testDay;   if ( month == 2 && testDay == 29 && ( year % 400 == 0           || ( year % 4 == 0  && year % 100 != 0  ) ) )             return testDay;   cout << "Day " << testDay << " invalid. Set to day 1.\n";   return 1;  // leave object in consistent state if bad value  } // end function checkDay```
Here is the code for date,
• 11-28-2009
Fatima Rizwan
i take input from user for the month as int and then print the corresponding month name,, so tell me how to output them ?? when i have an array of 10 contacts???
and yeah ur ryt,, on their name basis hw to print ,, kindly guide me through it also,, plus
i want some more help,, May i post the code of the Addres book here??
• 11-28-2009
Fatima Rizwan
Code:

```#include<iostream.h> #include<conio.h> #include<string.h> class Date { public:   void setDate();   void printDate(); private:   int month;   int day;   int year;   int checkDay( int ) const; };   void Date::setDate()   {   int mn,yr,dy;   cout<<"Enter Month:";   cin>>mn;     if ( mn > 0 && mn <= 12 )  // validate the month     month = mn;     else     {                    // invalid month set to 1     month = 1;     cout << "Month " << mn << " invalid. Set to month 1.\n";     }   cout<<"Enter Year:";   cin>>yr;     if ( yr> 1970 && yr<=2009 )     year = yr;     else     {     year= 1990;     cout<< "Year "<< yr << " invalid. Set to year 1990.\n";     }   cout<<"Enter Day:";   cin>>dy;                // should validate yr   day = checkDay( dy );      // validate the day   cout << endl; }   void Date::printDate()   {         switch(month)         {         case 1:               cout<<"January ";               break;         case 2:               cout<<"February ";               break;         case 3:               cout<<"March ";               break;         case 4:               cout<<"April ";               break;         case 5:               cout<<"May ";               break;         case 6:               cout<<"June ";               break;         case 7:             cout<<"July ";             break;         case 8:             cout<<"August ";             break;         case 9:             cout<<"September ";             break;         case 10:             cout<<"October ";             break;         case 11:             cout<<"November ";             break;         case 12:             cout<<"December ";             break;     }//end switch   cout << day <<", "<< year << endl; }   int Date::checkDay( int testDay ) const   {   static const int daysPerMonth[ 13 ] =           { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };   if ( testDay > 0 && testDay <= daysPerMonth[ month ] )       return testDay;   if ( month == 2 && testDay == 29 && ( year % 400 == 0           || ( year % 4 == 0  && year % 100 != 0  ) ) )             return testDay;   cout << "Day " << testDay << " invalid. Set to day 1.\n";   return 1;  // leave object in consistent state if bad value  } // end function checkDay class Person { char Fname[15]; char Lname[15]; Date dob; public:   void setFname()   {   cout<<"Enter First Name:";   cin>>Fname;   }   char* getFname()   {   return Fname;   }   void setLname()   {   cout<<"Enter Last Name:";   cin>>Lname;   }   char* getLname()   {   return Lname;   }   void setdob()   {   cout<<"Enter Date of Birth:";   dob.setDate();   }   void printPerson()   {   cout<<Fname<<" "<<Lname<<endl;   cout<<"Date of Birth is:";   dob.printDate();   cout<<endl;   } }; class Address {  int housenum;  int streetnum;  char city[15];  char country[20]; public:  void sethousenum()  {  cout<<"Enter house No:";  cin>>housenum;  }  int gethousenum()  {  return housenum;  }  void setstreetnum()  {  cout<<"Enter Street No:";  cin>>streetnum;  }  int getstreetnum()  {  return streetnum;  }  void setcity()  {  cout<<"Enter City name:";  cin>>city;  }  char* getcity()  {  return city;  }  void setcountry()  {  cout<<"Enter Country name:";  cin>>country;  }  char* getcountry()  {  return country;  }  void printaddress()  {  cout<<"Street No.is:"<<getstreetnum()<<endl<<"House No.is:"<<gethousenum()<<endl;  cout<<"City:"<<city<<endl<<"Country:"<<country<<endl;  } }; class Contact { Person person; Address Haddress; Address Oaddress; unsigned long Pno; unsigned long Mno; public:  Contact()  {  Pno=0;  Mno=0;  }  void setpersonalinfo()  {  person.setFname();  person.setLname();  person.setdob();  }  void setHaddress()  {  cout<<"Enter Home Address"<<endl;  Haddress.sethousenum();  Haddress.setstreetnum();  Haddress.setcity();  Haddress.setcountry();  }  void setOaddress()  {  cout<<"Enter Office Address:"<<endl;  Oaddress.sethousenum();  Oaddress.setstreetnum();  }  void setPno()  {  cout<<"Enter Phone No:";  cin>>Pno;  }  int getPno()  {  return Pno;  }  void setMno()  {  cout<<"Enter Mobile No:";  cin>>Mno;  }  int getMno()  {  return Mno;  }  void printContact()  {  person.printPerson();  cout<<"Home Address";  Haddress.printaddress();  cout<<endl;  cout<<endl<<"Office Address";  Oaddress.printaddress();  cout<<endl;  cout<<"Phone Number is:"<<getPno()<<endl;  cout<<"Mobile Number is:"<<getMno()<<endl;  } }; class Addressbook { Contact contact[2]; public: void addcontact()  {         for (int j=0;j<2;j++)         {         contact[j].setpersonalinfo();         contact[j].setHaddress();         contact[j].setOaddress();         contact[j].setPno();         contact[j].setMno();         }  }  void print()  {         for (int j=0; j<2; j++)         {         contact[j].printContact();         cout<<endl;         }  } };   int main()   {         clrscr();         Addressbook addressbook;         addressbook.addcontact();         addressbook.print();         getch();         return 0;   }```