I have a Plane's entity class such as:
class Plane{
private:
string tailNumber;
public:
void setTail(string tail);
string getTail();
}
and a Planes' collection class such as:
class Planes{
public:
void addPlane();
void printDetails();
void printAllPlanes();
private:
vector<Plane> currentPlane;
}
addPlane definition:
void Planes::addPlane(){
Plane a;
string temp;
cout << "Enter tail:";
getline(cin, temp);
a.setTail(temp);
currentPlane.push_back(a);
}
My printDetails definition:
void Planes::printDetails()
{
cout << "Enter Plane's Tail Number: ";
getline(cin, tail);
cin.ignore();
for (unsigned int i = 0; i < currentPlane.size(); i++)
{
if (currentPlane[i].getTailNumber() == tail)
{
//print tail number by calling accessor function}
}
else
{
cout << "Error.";
}
}
and my main class:
int main(){
Plane a;
int userChoice;
do{
cout << "1.Add Plane";
cout << "2.Print All Planes";
cout << "3.Print a plane";
cout << "4.Quit";
cin >> userChoice;
if (userChoice == 1)
a.addPlane();
else if (userChoice == 2)
a.printAllPlanes();
else if (userChoice == 3)
a.printDetails();
}while (userChoice != 4);
return 0;
}
I am successfully adding a new object and print all objects in my vector to display. The problem is if my tail number is: "TGA", then running currentPlane[0].getTail() return "TGA". However, when compare the user-input variable tail = "TGA" with currentPlane[0].getTail() = "TGA" yields an infinite-loop of do-while menu for some reason that I do not understand (because it is a simple string comparison?).
If I only enter integer value such as "12345", then it will jump to the else branch instead of infinite-looping. If I enter any alphanumeric value, then the infinite-looping will appear again. Can you help me, please?