Question:
C++ how do I fix this infinite while loop?
Ryan
2012-07-08 19:17:51 UTC
I would like to stop the loop after the if/else function and restart the loop. Right now it continuously loops the if or else result. Here is the fragment in question.

while (keyNumber !=0){
indexOfKey(table, size, keyNumber);

int index = indexOfKey(table, size, keyNumber);

if (index == keyNumber){
cout<< "The integer entered is in the array"<< endl;
}

else {
cout<<"The integer entered is not in the array"<< endl;
}
}
Five answers:
Jeff
2012-07-08 19:24:24 UTC
I don't see you changing the keyNumber so what is it supposed to do except loop forever...?
husoski
2012-07-09 04:01:22 UTC
---- Editorial:

On the contrary, Jon, getting out of loops early--without using goto--is exactly the original purpose of break. The use in switch() is simply keyword economy. A testament to this is the existence of "continue", which has no meaning in a switch block. The idea behind break, continue and return is that that they are all "limited forward goto" operations tied to the structure of the program rather than arbitrarily-placed labels. Just like with if/else.

PS: I approve of not using break or continue (or return!) mindlessly, and I'm aware of structured programming ideas going back to before my time, from Dijkstra, Wirth, Hoare and others.

PPS: I wasn't one of the TD issuers.

---- End OpEd Mode



It's hard to tell what's going on in this program. You call indexOfKey() twice at the top of the loop, only capturing the return value on the second call. That's weird enough to deserve a comment or two.



You don't know keyNumber getting set to anything before, or during, the loop. It could be set as a reference argument in indexOfKey(), but without seeing at least a prototype how is anyone going to guess that.



Take a long think about this. When you enter that loop, you will loop forever if nothing sets keyNumber to zero. Maybe you need some additional condition, along the lines of what Jon was suggesting,



Just from what I see, it looks like you are getting key numbers from some source, accidentally left an extra indexOfKey call in the loop, and forgot to get the next key number (with 0 indicating that there aren't any more) at the bottom of the loop. If it's from console input, maybe add:



cout << "Enter next key, or 0 to end: ";

cin >> keyNumber;



...just before the closing brace, and things might be better. If you're stepping through a list of keys in an array or collection, then a for() loop might be more appropriate. From a file, don't prompt, just:



keyin>>keyNumber;

if (!keyin) // stop on eof or error

{ keyNumber = 0; }



...or something like that, instead.
j A L
2012-07-09 03:17:49 UTC
Never use a break statement in anything other than a switch statement, that is not what its intended for and bad programming. Instead of your condition in the while, try to use a boolean, and in the else statement after the cout just change the value of boolean to break out. Like the first guy said, as its written, it will either be an infinite loop or just break out, there is no updating keyNumber, so the while is pretty useless as the code stands.



not sure why the first statement here was necessary either.



indexOfKey(table, size, keyNumber);



int index = indexOfKey(table, size, keyNumber);
James Bond
2012-07-09 05:05:13 UTC
Sorry. I did not get you. Do you want to comeout from while once the inner if condition becomes true?

while (keyNumber !=0){

indexOfKey(table, size, keyNumber);



int index = indexOfKey(table, size, keyNumber);



if (index == keyNumber){

cout<< "The integer entered is in the array"<< endl;

break; <------------------------------------------- Does this help you?

}



else {

cout<<"The integer entered is not in the array"<< endl;

}

}
King Boy
2012-07-09 02:30:50 UTC
Use break keyword to exit or stop the loop whenever some condition is met.

In your code, I think the "break" statement should be placed when the keyNumber is found in the array.

If you find it hard to figure out, just relax and go over it again, here is a great toy for you to play http://thickasians.co


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...