Help Required

This is a discussion on Help Required within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> #include<conio.h> #include<string.h> class Date { public: void setDate(); void printDate(); int getyear(); int getmonth(); int getday(); private: int ...

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

    Help Required

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    
    class Date
    {
    
    public:
      void setDate();
      void printDate();
      int getyear();
      int getmonth();
      int getday();
    
    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;
    
    }
    
    int Date::getyear()
    {
    return year;
    }
    int Date::getmonth()
    {
    return month;
    }
    int Date::getday()
    {
    return day;
    }
    
    
      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];
    
    
    public:
      Date dob;
    
      void setFname()
      {
      cout<<"Enter First Name:";
      cin>>Fname;
    
      }
    
      char* getFname()
      {
      return Fname;
      }
    
      void setLname()
      {
      cout<<"Enter Last Name:";
      cin>>Lname;
      cout<<endl;
      }
    
      char* getLname()
      {
      return Lname;
      }
    
      void setdob()
      {
      cout<<"Enter Date of Birth:";
      cout<<endl;
      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
    {
    
    Address Haddress;
    Address Oaddress;
    unsigned long Pno;
    unsigned long Mno;
    
    public:
    Person person;
     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();
     cout<<endl;
     }
    
     void setOaddress()
     {
     cout<<"Enter Office Address:"<<endl;
     Oaddress.sethousenum();
     Oaddress.setstreetnum();
     Oaddress.setcity();
     Oaddress.setcountry();
     }
    
     void setPno()
     {
     cout<<endl;
     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"<<endl;
     Haddress.printaddress();
     cout<<endl;
     cout<<endl<<"Office Address"<<endl;
     Oaddress.printaddress();
     cout<<endl;
     cout<<"Phone Number is:"<<getPno()<<endl;
     cout<<"Mobile Number is:"<<getMno()<<endl;
     cout<<endl;
    
     }
    
    };
    
    class Addressbook
    {
    public:
    Contact contact[3];
    void addcontact()
     {
    	for (int j=0;j<3;j++)
    	{
    	 contact[j].setpersonalinfo();
    	 contact[j].setHaddress();
    	 contact[j].setOaddress();
    	 contact[j].setPno();
    	 contact[j].setMno();
    	 clrscr(); }
    	 cout<<endl;
    	 cout<<endl;
     }
    
     void print()
     { clrscr();
    	for (int j=0; j<3; j++)
    	{
    	cout<<"CONTACT no is:"<<j+1<<endl;
    	contact[j].printContact();
    	cout<<endl;
    	}
    	cout<<endl;
     }
    
     void bynameprint()
     
    {
    
     int x;
    
     cout<<"\n\tContact by first name:";
     for(int i=0;i<3;i++)
      {
      cout<<"\n\tContact:"<<i<<"\n\n";
      x=strcmp(contact[i].person.getFname(),contact[i+1].person.getFname());
      if (x>0)
        { contact[i].printContact();}
      else
        { contact[i+1].printContact();}
      }
    
    }
    
     void sortcontactbydate()
     {
         for (int i=0; i<3; i++) // Sorting By Date
        {
    	for(int j=0;j<3;j++)
    	{
    	    int first = contact[i].person.dob.getyear();
    	    int second = contact[j].person.dob.getyear();
    	    if(first==second)
    	    {
    		int third = contact[i].person.dob.getmonth();
    		int fourth = contact[j].person.dob.getmonth();
    		if(third==fourth)
    		{
    		    int fifth = contact[i].person.dob.getday();
    		    int sixth = contact[j].person.dob.getday();
    		    if(fifth<sixth)
    		    {
    			Contact temp =contact[i];
    			contact[i]=contact[j];
    			contact[j]=temp;
    		    }
    
    		}
    		else if(third<fourth)
    		{
    		    Contact temp =contact[i];
    		    contact[i]=contact[j];
    		    contact[j]=temp;
    		}
    	    }
    	    else if(first<second)
    	    {
    		    Contact temp =contact[i];
    		    contact[i]=contact[j];
    		    contact[j]=temp;
    	    }
    	}
        }
    
     }
    
    
    
    };
    
    
      int main()
      {
    	clrscr();
    	int num;
    	int ans;
    	Addressbook addressbook;
    	cout<<"WELCOME TO THE ADDRESSBOOK";
    	cout<<endl;
    	cout<<"Press 1 to ADD contacts";
    	cin>>num;
    
    	if (num==1)
    	{
    	addressbook.addcontact();
    		{
    		clrscr();
    		cout<<"PRESS 1 TO PRINT CONTACTS BY DOB, 2 TO PRINT THEM BY NAME, 3 TO PRINT THEM AS THEY ARE";
    		cin>>ans;
    		if (ans==3)
    		{
    		addressbook.print();
    		}
    		if (ans==1)
    		{
    		addressbook.sortcontactbydate();
    		addressbook.print();
    		}
    		}
    		if (ans==2)
    		{
    		addressbook.bynameprint();
    		addressbook.print();
    		}
    
    	}
    	getch();
    	return 0;
      }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    want to print the address book by name,, but its not working,, can anybody help, i have to submit the assignment after 1 hr,, please help

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,407
    You have to submit in one hour? On a sunday? To save having to trawl through all that could you say why its not working? Ie what is the error? Does it compile? What is the output compared to what you want?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    yeah on sunday ,, i have to submit today,, and you know these are our EID holidays,, This assignment has spoiled my Eid and weekend, =(
    thnks for the concern ,, i managed to get the output lately,,
    THANKS
    EID MUBARAK!!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Indentation is lacking...
    If you wanted help, you really should have asked before hours left to the deadline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  4. Determining required flops to run C code
    By mollyann in forum C Programming
    Replies: 5
    Last Post: 03-30-2005, 12:28 PM
  5. is this required in a function body?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 03:20 PM

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