-3

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?

Dat To
  • 45
  • 6
  • Based on a quick guess, I'd say that `userChoice` is not within bounds [1,3], and your program gets stuck in the do-while loop. I recommend you handle the case where userChoice is not an expected value, either by breaking out or asking the user for new input. Also, this is a perfect **case** for a switch-case selection statement rather than if-else if. – Mansoor Mar 28 '19 at 00:09
  • Be very careful mixing operator `>>` with calls to the many forms of `getline`. Keeping track of what one or the other leaves in the stream for the next sucker can get tricky. The classic example is [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user4581301 Mar 28 '19 at 00:16

1 Answers1

2

Nothing to do with string comparisons, the problem with your code is at no point do you set the variable userChoice.

Presumably you meant to have some code somewhere like

cin >> userChoice;

but you don't have anything like that, so the behaviour of your program is undefined.

You really should have had a compiler warning telling you that you are using an uninitialised variable. Pay attention to compiler warnings and fix any that you get.

john
  • 71,156
  • 4
  • 49
  • 68
  • I forgot to put the cin >> userChoice under my do-while looping. – Dat To Mar 28 '19 at 00:17
  • 1
    @DatTo You have little chance of getting useful help unless you can post accurate code. Cut and paste code from your editor into stack overflow. – john Mar 28 '19 at 00:19
  • @DatTo it's a good idea to make sure your example is correct and can be used to replicate the error before posting. Your edit just rendered a correct answer incorrect, and that's bad form. – user4581301 Mar 28 '19 at 00:19
  • I just edit my code. My code is very long and I cannot post the whole version here since it is against the rule. I don't know why it works in this replica, but in my original version, it does not work at all. – Dat To Mar 28 '19 at 00:21
  • @DatTo My recommendation is to back up your program and produce a [mcve] to isolate the bug and post the MCVE if you have to. Often creating an MCVE helps you find the bug yourself and makes the question unnecessary. This is why an MCVE is is requested at part of all "Why doesn't my code work?" type questions: it eliminates the question before it is asked. – user4581301 Mar 28 '19 at 00:40
  • Thanks guys, I just fixed my code. Simply put the cin.ignore() before the getline(cin, tail); will solve the problem. Thanks for your help. I still do not understand why it can cause a major bug like this? – Dat To Mar 28 '19 at 00:49
  • @DatTo Check up in the comments under the question. I dropped a link describing the problem a while ago. A recommendation, though. Put the `ignore` after the operation that leaves the newline in the stream. If you put the `ignore` before an operation, you'll discard data if you ever hit that operation without a newline in the stream. – user4581301 Mar 28 '19 at 01:09