beginner’s here.
Currently working on an exercise where I need to apply aggregation. The problem is my while loop isn’t working. It suppose to display back the menu option, after it has for the first time, and read input again and so on, but instead it exit the program.
`#include
#include
using namespace std;
class Address{
private:
string registrar;
string country;
public:
void set(string r, string c){
registrar = r;
country = c;
}
string getRegistrar(){
return registrar;
}
string getCountry(){
return country;
}
};
class Ship{
private:
string name;
string yearMade;
Address *address;
public:
void print(){
cout<<“Ship Name: “<< name <<endl;
cout<<“Year Built: “<< yearMade <<endl;
cout<<“Registered at: “<<endl<getRegistrar()<<“, “<getCountry()<<endl;
}
void read(){
string r, c;
cout<<“<<< Enter the information of the ship >>>”<<endl<<endl;
cout<<“Ship Name: “;
getline(cin, name);
cout<<“Year Built: “;
getline(cin, yearMade);
cout<<“The address the ship was registered:”<<endl;
cout<<“Registrar Office: “;
getline(cin, r);
cout<<“Country: “;
getline(cin, c);
address->set(r, c);
}
};
int main() {
Ship *ship = new Ship[100];
int option=0, total=0;
while(option != 3){
cout<<"======== MENU ========"<<endl;
cout<<"1. Add a ship"<<endl;
cout<<"2. Display ships"<<endl;
cout<<"3. Exit."<<endl<<endl;
cout<<"Choose an operation => ";
cin >> option;
cin.ignore();
if(option == 1){
ship[total].read();
total++;
}else if(option == 2){
cout<<"<<< Inventory of ships >>>"<<endl<<endl;
cout<<"Total ship: "<<total<<endl<<endl;
cout<<"==== Ship List ===="<<endl<<endl;
for(int i=0; i<total; i++){
ship[i].print();
}
}
}
return 0;
}`
Khairul Hakimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.