Nick
Nick

Reputation: 9373

A simple C++ While loop not working

I have a simple while loop i'm trying to implement but for the life of me can't figure out what I'm missing. I have currentuser initialized at the top to -1

while(currentuser = -1){
    cout << "Enter user ID: ";
    cin >> id;
    currentuser = search(a, length, id);
}

My search function is this:

int search (User a[ ], int length, string userID){
    User u;
    string tempid;
    int templegnth; //I ignore length for now as I will use it later
    for(int i=0; i<50; i++){
        tempid = a[i].getID();
        templegnth = tempid.length();
        if((tempid == userID)){
            return i;
        }
    }
    return -1;


}

I know its something very simple but the answer escapes me right now.

Upvotes: 4

Views: 6894

Answers (6)

GmdGooodsoups
GmdGooodsoups

Reputation: 1

Whenever I use a while loop and a boolean, I try to make sure it only runs if it is above or is 50 but instead whenever I execute a cout with the while and int, the outcome is a literal mess, its like the while loop executes a lot more than it should. Eg.

int Buses;
int People;
cin >> People;
while(People >= 50){
Buses += 1;
People -= 50;
};
cout << Buses << endl;

and when I input 757, my result was 32782.

Even by subtracting the int value by 32767, the values sort of fix itself but not the higher numbers, 99 is 1, 101 is 2 but c++ says that 300 is 3 although it is meant to be 6.

(My solution I found: I had to declare the Person and Buses variable with a 0 because the value was different without the starting 0.)

This is the fixed code:

int Buses = 0;
int People = 0;
cin >> People;
while(People >= 50){
Buses += 1;
People -= 50;
};
cout << Buses << endl;

Upvotes: 0

Michael
Michael

Reputation: 146

You've got the answers but here is a tip on how to avoid it in the future. Always try to use

while(-1 == currentuser){
  std::cout << "Enter user ID: ";
  std::cin >> id;
  currentuser = search(a, length, id);
}

as this way

while(-1 = currentuser){
  ;
} 

will be thrown out by the compiler

Upvotes: 3

David Hammen
David Hammen

Reputation: 33136

Even with the = changed to ==, the loop still has problems.

while(currentuser == -1){
  std::cout << "Enter user ID: ";
  std::cin >> id;
  currentuser = search(a, length, id);
}

Typing an EOT (control-D on a Linux box, control-Z? on windows) will raise std::cin's end of file condition. The value of id won't be changed, and the lookup will presumably keep on returning -1. The result is an infinite loop with lots of spew to std::cout.

One way to fix this is to break out of the loop when the std::cin >> id; fails. For example, if (! (std::cin >> id)) break;

Upvotes: 0

thomasfedb
thomasfedb

Reputation: 5983

You need to change:

while(currentuser = -1){

to be:

while(currentuser == -1){

Currently you are assigning currentuser to -1 every time your loop runs, rather than checking if it is still assigned to that value.

Upvotes: 6

Sander De Dycker
Sander De Dycker

Reputation: 16243

The = (assignment) operator is not the same as the == (equality) operator.

The line :

while(currentuser = -1){

first assigns -1 to currentuser, and then checks if currentuser has a non-zero value. This will always be the case (-1 != 0), so the loop will never end.

You likely meant this instead :

while(currentuser == -1){

which compares currentuser to -1, and continues the loop as long as that comparison is true.

Upvotes: 7

Jean Logeart
Jean Logeart

Reputation: 53869

Try == -1 instead of = -1

Upvotes: 4

Related Questions