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.
The
=(assignment) operator is not the same as the==(equality) operator.The line :
first assigns
-1tocurrentuser, and then checks ifcurrentuserhas a non-zero value. This will always be the case (-1 != 0), so the loop will never end.You likely meant this instead :
which compares
currentuserto-1, and continues the loop as long as that comparison is true.