0

I am trying to get the user to key in data and store them inside a vector so when -1 is being entered it will break the entire loop and start displaying the vector. However it is currently not working even if -1 has been entered the loop will still continue to go on.

void cargo::addCargo(vector<cargo> &userCargo)
{
    fstream file;
    file.open("cargo.txt", ios::out | ios::app);
    int i = 0;
    bool checker = true;
    cargo newCargo;
    while (checker == true)
    {
        
        cout << "Enter id" << endl;

        if (cin.peek() == '-1')
        {

            checker = false;
            cout << "Hello";

        
        }
        else
        {
            cin >> idCon;
            newCargo.setId(idCon);
        }
        
        cout << "Enter Destination Country" << endl;
        if (cin.peek() == '-1')
        {
            checker = false;
            
        }
        else
        {
            cin >> destCountryCon;
            newCargo.setDestCountry(destCountryCon);
        }
        
        cout << "Enter Time" << endl;
        if (cin.peek() == '-1')
        {
            checker = false;
        }
        else
        {
            cin >> timeCon;
            newCargo.setTime(timeCon);
            newCargo.setIndex(i + 1);
            userCargo.push_back(newCargo);
        }
    
        cout << endl;
        i++;
        
    }

    //loop to display the entire vector
    displayCargo(userCargo);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
GX-X
  • 115
  • 1
  • 4
  • 14
  • Why not use `break;`? The variable is only checked at the beginning of each iteration of the loop. Just because you set it to false doesn't make anything special happen until you reach the beginning of the loop again. – Retired Ninja Mar 28 '21 at 12:25
  • I tried using break but it still no difference – GX-X Mar 28 '21 at 12:27
  • What are you trying to check with `if (cin.peek() == '-1')`? Are you looking for EOF, or are you expecting the user to type `-`+`1`+``? – John Kugelman Mar 28 '21 at 12:32
  • I am expecting the user to press -1 to stop data entry if not the data entry will keep on going and going until the user press -1 to stop and it will display all the data he has entered – GX-X Mar 28 '21 at 12:33
  • That's not how to check for `-1`. What you should do is just run `cin >> destCountryCon` and `cin >> timeCon` unconditionally, then check if those variables contain `-1` or not. `cin.peek() == '-1'` doesn't do that. – John Kugelman Mar 28 '21 at 12:39
  • In other words, the issue here isn't about breaking out of while loops, it's that you're not checking for `-1` correctly. Make sense? – John Kugelman Mar 28 '21 at 12:43
  • Yeah is check for -1 and then break out the loop – GX-X Mar 28 '21 at 13:00

1 Answers1

0

You are code is not self-contained so I can't fix it for you. The pragmatic answer is break or continue with a flag like checker. peek() looks at the next character and when the user enters "-1" it will return '-' and not the multi-character constant '-1'. Alternative, user can press ctrl-d to trigger eof. If you want to use "-1" (who came up with that terrible ux?), read that as a string or int.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38