![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 108
| 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;
}
|
| Fatima Rizwan is offline | |
| | #2 |
| Registered User Join Date: Mar 2009
Posts: 108
| 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 |
| Fatima Rizwan is offline | |
| | #3 |
| Registered User Join Date: Aug 2006 Location: Liverpool UK
Posts: 553
| 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? |
| rogster001 is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 108
| 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!! |
| Fatima Rizwan is offline | |
| | #5 |
| The Beautiful C++ Utopia Join Date: Oct 2007
Posts: 16,515
| Indentation is lacking... If you wanted help, you really should have asked before hours left to the deadline.
__________________ WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk. Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Why did the Java creators shoot themselves in the foot? |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem building Quake source | Silvercord | Game Programming | 16 | 07-11-2010 09:13 AM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| NAQ: Everything you never wanted to know about CPP | evildave | C Programming | 21 | 12-12-2005 10:56 AM |
| Determining required flops to run C code | mollyann | C Programming | 5 | 03-30-2005 01:28 PM |
| is this required in a function body? | Unregistered | C++ Programming | 2 | 09-26-2001 03:20 PM |