I am working on a project and I'm a beginner. The project involves a person, adult, kid and bogie class. In bogie class I have initialsed pointer of type person named adult as adult = new Adult [adultcount]
, where adultcount
is the number of adults user wants to add.
When I add only one adult in input, it works fine, it takes the input and assigns it in while assigning values in for loop.
But when I add more than one, it stops working and says exception throw at line Adults[i].setdata()
.
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
string Name;
int Age;
string Gender;
public:
Person()
{
Name = '\0';
Age = 0;
Gender = '\0';
}
Person(string Name, int Age, string Gender)
{
this->Name = Name;
this->Age = Age;
this->Gender = Gender;
}
virtual void setdata()
{
string name, gender;
int age;
cin.ignore();
cout << " enter Name :";
getline(cin, name, '\n');
this->Name = name;
cout << endl;
cout << " Enter gender ";
getline(cin, gender, '\n');
this->Gender = gender;
cout << endl;
cout << " Enter age ";
cin >> age;
Age = age;
cout << endl;
}
virtual void printinfo()
{
cout << " name " <<Name;
cout << " Gender " << Gender;
cout << " age : " << Age;
}
};
class Adult :public Person {
public:
string Occupation;
string Qualification;
string NIC;
Adult():Person()
{
Occupation = '\0';
Qualification = '\0';
NIC = '\0';
}
Adult(string Occupation, string Qualification, string NIC, string Name, int Age, string Gender) : Person(Name, Age, Gender)
{
this->Occupation = Occupation;
this->NIC = NIC;
this->Qualification = Qualification;
}
void setdata()
{
Person::setdata();
string occupation, qualification/*, name, gender,*/ ,cnic;
/* int age;*/
cin.ignore();
/* cout << " enter Name :" ;
getline(cin, name, '\n');
this->Name = name;
cout << endl;*/
cout << "enter occupation ";
getline(cin, occupation, '\n');
this->Occupation = occupation;
cout << endl;
/*cout << " Enter gender ";
getline(cin, gender, '\n');
this->Gender = gender;
cout << endl;*/
cout << " enter qualification ";
getline(cin, qualification, '\n');
this->Qualification = qualification;
cout << endl;
cout << " Enter cnic ";
getline(cin, cnic, '\n');
this->NIC = cnic;
cout << endl;
/* cout << " Enter age ";
cin >> age;
this->Age = age;
cout << endl;*/
}
void printinfo()
{
Person::printinfo();
/*cout << " name "<<Name << endl;*/
cout << " occupation " << Occupation << endl;
/*cout << " Gender " << Gender << endl;*/
cout << " Qualifcation " << Qualification << endl;
/*cout << " Age " << Age << endl;
cout << " Gender " << Gender << endl;*/
}
};
class kid :public Person {
public:
string B_form_number;
kid() : Person()
{
B_form_number = '\0';
}
kid(string B_form_number, string Name, int Age, string Gender) :Person(Name, Age, Gender)
{
this->B_form_number = B_form_number;
}
void setdata()
{
Person::setdata();
string bform;
cout << " Enter B Form number ";
/* getline(cin, bform, '\n')*/;
cin >> bform;
this->B_form_number = bform;
cout << endl;
}
void setbform()
{
string bform;
cout << " Enter B Form number ";
getline(cin, bform, '\n');
this->B_form_number = bform;
cout << endl;
}
void printinfo()
{
Person::printinfo();
cout << " b form number " << B_form_number;
}
};
class Bogie /*:public kid*/ {
int Bogie_ID;
Bogie* next;
Person* Adults;
Person* kids;
string familyName;
int adultcount;
int kidcount;
public:
Bogie(int id)
{
Bogie_ID = id;
next = nullptr;
Adults = nullptr;
kids = nullptr;
familyName = '\0';
adultcount = 0;
kidcount = 0;
}
void AddPassengers() // should add adults and kids information etc
{
string familyName;
cout << "Enter family name " << endl;
cin.ignore();
getline(cin, familyName, '\n');
this->familyName = familyName;
bool condition = false;
bool condition2 = false;
while (condition == false) // runs until valid input for adults
{
cout << " How many adults in the family " << endl;
cin >> adultcount;
if (adultcount >= 1 && adultcount <= 4)
{
condition = true;
}
else
{
cout << " Invalid input " << endl;
cout << " We have seats for 4 adults " << endl;
condition = false;
}
}
while (condition2 == false) //runs until valid input for kids
{
cout << " How many kids in the family " << endl;
cin >> kidcount;
if (kidcount >= 1 && kidcount <= 6)
{
condition2 = true;
}
else
{
cout << " Invalid input " << endl;
cout << " We have seats for 6 kids " << endl;
condition2 = false;
}
}
this->Adults = new Adult[adultcount];
for (int i = 0; i < adultcount; i++)
{
Adults[i].setdata();
}
}
}
};
int main()
{
Bogie a(1);
a.AddPassengers();
a.Print();
}
I believe you need to change Adults
from Person *
to Adult *
.
When the program tries to access Adults[1]
at
for (int i = 0; i < adultcount; i++)
{
Adults[i].setdata();
}
It jumps ahead sizeof(Person)
in the array, while the objects in the array are sizeof(Adult)
, which leads to the exception you're seeing.
so I found the answer the appropriate method to to it was declaring pointers named adult and kid in the members as Person ** Adults; Person **kids and then adults = new Person * [adultcount];
for (int i = 0; i < adultcount; i++)
{
adults[i] = new Adult;
adults[i]->setdata();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.