4

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.

2
  • 2
    change it to while(currentuser == -1) Note ==. If that's not it, you might consider actually describing what the problem is and asking a question. Commented Sep 25, 2011 at 14:10
  • Which chapter of your book clearly differentiates between the = operator and the == operator? Hmmm... Commented Feb 3, 2013 at 1:25

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

4

Try == -1 instead of = -1

Comments

3

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

1 Comment

I would argue that the loss in readability may not be offset by the extra syntax checking.
0

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;

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.