Define a base class person that will contain universal information, including name, address, birth date, gender and identification (student, worker etc). Derive from this class the following classes:
Student
Worker
Student_worker

Write a program that asks user to input information (student, worker etc) and creates a list of persons. Give user choice to view list by gender and by identification( list of people who are students, list of people who are workers etc).

#include<iostream>
#include<string>
using namespace std;
enum Identification 
{Idstudent=0, Idworker=1, Idstudentworker=2};
class person
{
public:
int id_name;
void output (string _name, string _address, string _birthdate, char _gender);
person(){};
string name;
string address;
string birthdate;
char gender;
Identification id;
void input();
Identification getid() {return id;}
string getname() {return name;}
string getaddress() {return address;}
string getbirthdate() {return birthdate;}
char getgender() {return gender;}
Identification getid() const {return id;}
int getid_name() {return id_name;}
};
void person::output (string _name, string _address, string _birthdate, char _gender)
{
cout<< " name “<<_name<<” address “<<_address<<” birthdate “<<_birthdate<<” gender “<<_gender<<endl;
}
void person::input()
{
cout<< " enter the name, address, birthdate, gender " <<endl;
cin>>name>>address>>birthdate>>gender;
cout<< " what are they doing?? if student enter 0, if worker enter 1, if both enter 2 " <<endl;
cin>>id_name;
}
class student:public person{};
class worker:public person{};
class studentworker:public person{};
void main()
{
int allchoice;
char genderchoice;
int b[3];
char c[3];
int choice;
enum Identification id;
person abc[3];
for (int i=0; i<3; i++)
{
abc.input();
}
for (int i=0; i<3; i++)
{
abc.output (abc.getname(), abc.getaddress(), abc.getbirthdate(), abc.getgender());
}
cout<< " Do you want to search from gender or ID? “<<endl;
cout<< " gender enter '1' “<<endl;
cout<< " id enter '2' “<<endl;
cin>>allchoice;
if(allchoice==1)
{
cout<< " pls enter the group that you want to search. “<<endl;
cin>>genderchoice;
for (int i=0; i<3; i++)
{
c=abc.getgender();
if(c==genderchoice)
{
abc.output (abc.getname(), abc.getaddress(), abc.getbirthdate(), abc.getgender());
}
}
}
else
{
cout<< " pls enter the group that you want to search.if student enter 0, if worker enter 1, if both enter 2 " <<endl;
cin>>choice;
for (int i=0; i<3; i++)
{
b=abc.getid_name();
if(b==choice)
{
abc.output (abc.getname(), abc.getaddress(), abc.getbirthdate(), abc.getgender());
}
}
}
}

Recommended Answers

All 8 Replies

Not even going to bother.

First, format your code so we can read it.
Next, ask a question we can answer. That starts with details of the problem, not just posting the assignment and nothing else useful.

What's more You missed " at line 28. And read about using arrays, cause this:

person abc[3];
for (int i=0; i<3; i++)
{
abc.input();
}

is completly wrong.

Here's the corrected code.. Your problem mainly was that you hadn't put indexes while using your arrays.. rest is almost the same.. I almost did indent the code as otherwise it was impossible to understand.

#include<iostream>
#include<string>
using namespace std;

enum Identification 
{
	Idstudent, 
	Idworker, 
	Idstudentworker
};

class person
{

public:
	int id_name;
	string name;
	string address;
	string birthdate;
	char gender;
	Identification id;

	void input();
	void output (string _name, string _address, string _birthdate, char _gender);
	
	person(){}
	
	Identification getid() const {return id;}
	const string& getname() const {return name;}
	const string& getaddress() const {return address;}
	const string& getbirthdate() const {return birthdate;}
	char getgender() const {return gender;}
	int getid_name() const {return id_name;}
};

void person::output (string _name, string _address, string _birthdate, char _gender)
{
	cout << " name " << _name << " address " << _address << " birthdate "
		<< _birthdate << " gender " << _gender << endl;
}

void person::input()
{
	cout << " enter the name, address, birthdate, gender " << endl;
	cin >> name >> address >> birthdate >> gender;
	cout << " what are they doing?? if student enter 0, if worker enter 1,"
		" if both enter 2 " << endl;
	cin >> id_name;
}

class student:public person{};

class worker:public person{};

class studentworker:public person{};

void main()
{
	char allchoice;
	char genderchoice;
	int b[3];
	char c[3];
	int choice;
	//enum Identification id;
	person abc[3];

	// read values
	for (int i = 0; i < 3; i++)
	{
		abc[i].input();
	}

	// output values
	for (int i=0; i<3; i++)
	{
		abc[i].output (abc[i].getname(), abc[i].getaddress(), 
			abc[i].getbirthdate(), abc[i].getgender());
	}

	for (;;)
	{
		cout << endl;
		cout << " Enter a choice: " << endl;
		cout << " 1: To search with gender" << endl;
		cout << " 2: To search with id" << endl;
		cout << "Any other key to exit!" << endl;
		cin >> allchoice;
		if(allchoice == '1')
		{
			cout<< " pls enter the group(m/f) that you want to search. "<<endl;
			cin>>genderchoice;
			for (int i=0; i<3; i++)
			{
				c[i]=abc[i].getgender();
				if(c[i]==genderchoice)
				{
					abc[i].output (abc[i].getname(), abc[i].getaddress(), 
						abc[i].getbirthdate(), abc[i].getgender());
				}
			}
		}
		else if (allchoice == '2')
		{
			cout << " pls enter the group that you want to search.if student enter 0,"
				" if worker enter 1, if both enter 2 " <<endl;
			cin >> choice;
			for (int i=0; i<3; i++)
			{
				b[i] = abc[i].getid_name();
				if(b[i] == choice)
				{
					abc[i].output (abc[i].getname(), abc[i].getaddress(), 
						abc[i].getbirthdate(), abc[i].getgender());
				}
			}
		}
		else
		{
			cout << "Thanks for visiting!" << endl;
			break;
		}
	}
}
commented: Read the rules, we help people with their issues by guiding them through them, we don't do it for them. +0

Here's the corrected code.. Your problem mainly was that you hadn't put indexes while using your arrays.. rest is almost the same.. I almost did indent the code as otherwise it was impossible to understand.

#include<iostream>
#include<string>
using namespace std;

enum Identification 
{
	Idstudent, 
	Idworker, 
	Idstudentworker
};

class person
{

public:
	int id_name;
	string name;
	string address;
	string birthdate;
	char gender;
	Identification id;

	void input();
	void output (string _name, string _address, string _birthdate, char _gender);
	
	person(){}
	
	Identification getid() const {return id;}
	const string& getname() const {return name;}
	const string& getaddress() const {return address;}
	const string& getbirthdate() const {return birthdate;}
	char getgender() const {return gender;}
	int getid_name() const {return id_name;}
};

void person::output (string _name, string _address, string _birthdate, char _gender)
{
	cout << " name " << _name << " address " << _address << " birthdate "
		<< _birthdate << " gender " << _gender << endl;
}

void person::input()
{
	cout << " enter the name, address, birthdate, gender " << endl;
	cin >> name >> address >> birthdate >> gender;
	cout << " what are they doing?? if student enter 0, if worker enter 1,"
		" if both enter 2 " << endl;
	cin >> id_name;
}

class student:public person{};

class worker:public person{};

class studentworker:public person{};

void main()
{
	char allchoice;
	char genderchoice;
	int b[3];
	char c[3];
	int choice;
	//enum Identification id;
	person abc[3];

	// read values
	for (int i = 0; i < 3; i++)
	{
		abc[i].input();
	}

	// output values
	for (int i=0; i<3; i++)
	{
		abc[i].output (abc[i].getname(), abc[i].getaddress(), 
			abc[i].getbirthdate(), abc[i].getgender());
	}

	for (;;)
	{
		cout << endl;
		cout << " Enter a choice: " << endl;
		cout << " 1: To search with gender" << endl;
		cout << " 2: To search with id" << endl;
		cout << "Any other key to exit!" << endl;
		cin >> allchoice;
		if(allchoice == '1')
		{
			cout<< " pls enter the group(m/f) that you want to search. "<<endl;
			cin>>genderchoice;
			for (int i=0; i<3; i++)
			{
				c[i]=abc[i].getgender();
				if(c[i]==genderchoice)
				{
					abc[i].output (abc[i].getname(), abc[i].getaddress(), 
						abc[i].getbirthdate(), abc[i].getgender());
				}
			}
		}
		else if (allchoice == '2')
		{
			cout << " pls enter the group that you want to search.if student enter 0,"
				" if worker enter 1, if both enter 2 " <<endl;
			cin >> choice;
			for (int i=0; i<3; i++)
			{
				b[i] = abc[i].getid_name();
				if(b[i] == choice)
				{
					abc[i].output (abc[i].getname(), abc[i].getaddress(), 
						abc[i].getbirthdate(), abc[i].getgender());
				}
			}
		}
		else
		{
			cout << "Thanks for visiting!" << endl;
			break;
		}
	}
}

it worked but its not functionaing as it supposed to like what i have been ask2 do

What problem are you facing now! How is it behaving, and how should it? Elaborate please!

What problem are you facing now! How is it behaving, and how should it? Elaborate please!

it supposed to ask for identification # as well but its only asking name,age,gender,address ..
and also when i choose 2 for search by id it wont work

I don't understand why it's not working for those cases.. when i had tested after modifying the code, it was doing all those things..
I see two possibilities:
1. You're not giving it the input properly.. When asked for name, address, dob and sex, type four entries.. e.g. type:
Nitin New_Delhi Oct1986 m<press Enter>
Next, it'll ask for id, so enter appropriately 0, 1 or 2.. and press Enter
2. You've changed something in the code, which I'd not suggest you to do.

Or you may try this:

enum Identification 
{
	Idstudent = 0, 
	Idworker = 1, 
	Idstudentworker = 2
};

In either case, I think you can sit down and fix somethings on your own.. You can mark this thread solved if you think that was sufficient.

it worked but its not functionaing as it supposed to like what i have been ask2 do

What problem are you facing now! How is it behaving, and how should it? Elaborate please!

I don't understand why it's not working for those cases.. when i had tested after modifying the code, it was doing all those things..
I see two possibilities:
1. You're not giving it the input properly.. When asked for name, address, dob and sex, type four entries.. e.g. type:
Nitin New_Delhi Oct1986 m<press Enter>
Next, it'll ask for id, so enter appropriately 0, 1 or 2.. and press Enter
2. You've changed something in the code, which I'd not suggest you to do.

Or you may try this:

enum Identification 
{
	Idstudent = 0, 
	Idworker = 1, 
	Idstudentworker = 2
};

In either case, I think you can sit down and fix somethings on your own.. You can mark this thread solved if you think that was sufficient.

thhanx you very much brother.. i really appreciated your help

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.