Why is this loop an infinite loop? int main () { int number = 0, // This variable contains the value from the function getNum() randNum = 0; // This variable contains the value from the the function getRand() do { while (number != randNum) { randNum = getRand(); number = getNum(); if (number == 1 && randNum == 2) cout << "The computer chose paper, you chose rock. You lose!\n"; if (number == 1 && randNum == 3) cout << "The computer chose scissors, you chose rock. You win!\n"; if (number == 2 && randNum == 1) cout << "The computer chose rock, you chose paper. You win!\n"; if (number == 2 && randNum == 3) cout << "The computer chose scissors, you chose paper. You lose!\n"; if (number == 3 && randNum == 1) cout << "The computer chose rock, you chose scissors. You lose!\n"; if (number == 3 && randNum == 2) cout << "The computer chose paper, you chose scissors. You win!\n"; } cout << "You tied with the computer!\n"; } { while (number < 4) This is just a code snippet obviously, if anybody would like to see more just let me know. Its for a console application.
Let's get rid of the non-pertinent code first: do { while (number != randNum) { randNum = getRand(); number = getNum(); } } while (number < 4) What are the return values ranges of getRand() and getNum()? Does getNum() ever return a number greater than 3, does getRand() ever return a number greater than 3? If not then you will loop forever. Your while loop will loop until randNum == number, and your do while will loop until number is greater than three.
I don't know c++ but I do see that you are missing a "counter" or something that advances the loop or exits the loop. Let me give you an example to explain what you are doing and why it'll never exit: a=2 b=2 Do While (a+b==4){ 'Do something }loop ^^see 2+2 will always be 4. You have to write this so that the code goes on to the next iteration of the variable(s)! Somewhere in your loop, tell the program how to iterate. For example, going through a recordset, the next iteration is the next record and is moved with something like "next". In your example, you have to find a way to get different values for your "number" variable. For example, add some value to your "number" variable at each loop pass. Otherwise, you are looping the same values and you'll never escape.
Thanks for responding to a post over a year old, and stating the ****ing obvious. There are other things one must learn prior to even learning C++ from a book.
Nope. Learning SQL for an unrelated reason. Putting everything on hold because I ship out for different military training again for a few months.
You could begin with YouTube videos. You can find excellent groups of videos tailored to beginners. I learned C++ years ago but mostly use Java now.