addressbook

This is a discussion on addressbook within the C++ Programming forums, part of the General Programming Boards category; i have created a class ov date in address book ,, i want the the days and months to be ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    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;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Checking for valid dates is different from date arithmetic. Which one are you asking about?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    i just want to check if the date is valid for a specific month,,

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    yah exactly i want it to switch it to nxt month ,,,But its not working,,,

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Fatima Rizwan View Post
    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...

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    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,,

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    like the person who's born in february will be abv the person who born in march

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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...

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    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. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    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??

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    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;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function not working correctly
    By mackieinva in forum C Programming
    Replies: 0
    Last Post: 09-29-2007, 09:22 PM
  2. Loop Query
    By (TNT) in forum C# Programming
    Replies: 5
    Last Post: 02-13-2006, 06:16 PM
  3. Need help with my addressbook
    By Sputtilutti in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21