-
Looping.
Okay, first of all, I'm sorry if there was a post about this I did not see.
I am starting with the C++ language. I've been reading C++ for Dummies 4th edition for about 2 days now. I started making my own program using numbers, and everything works, except the looping. I cant make the program loop for some reason. It always terminates. Does anyone have a solution???
-
Post some code so we can see where the problem is and we'll be glad to help.
-
Ok, here goes:
//
//Number Game
//
#include <stdio.h>
#include <iostream.h>
int main()
{
//input loop count
int loopCount;
cout<<"Enter number of guesses you wish to make:";
cin>> loopCount;
//enter the guess
int guess;
cout<<"Enter your guess:";
cin>> guess;
if(guess<=42||>=44)
{
//Path 1
loopCount=loopCount--;
cout<<"That's incorrect. Try again.";
}
if(guess==44)
{
loopCount=loopCount--;
cout<<"That's correct. Press any key to exit.";
}
return 0;
}
That's it. I know, it's pretty stupid 'cause you can only play once. But, hey, it's a start.
-
>But, hey, it's a start.
We have to start somewhere, and getting one iteration to work before making the program loop many times is an excellent design strategy.
Try this out and see if you can work out exactly what it does and why.
Code:
#include <iostream.h>
int main()
{
//input loop count
int loopCount;
cout<<"Enter number of guesses you wish to make:";
cin>> loopCount;
cin.ignore();
//enter the guess
while ( loopCount-- > 0 ) {
int guess;
cout<<"Enter your guess:";
cin>> guess;
cin.ignore();
if(guess==44) {
cout<<"That's correct.\n";
break;
}
else
cout<<"That's incorrect.\n";
}
return 0;
}
-Prelude
-
I know what everything else does, except the command cin.ignore(). I have no idea what it does. One of the problems with my code was I didn't include while('condition'), right?
-
Yea, you didn't have a loop in your code. That's why it wasn't working.
-
>I know what everything else does, except the command cin.ignore().
Using cin can cause data to be left in the input stream. This causes problems with further calls to cin so there is a method called ignore() which reads and discards remaining data in the stream.
-Prelude
-
Try to remember that the program starts at the top of main and works its way down to the end of main. If you want any loops at all you have to use for, while or do..while structures.
-
Ok, so let me get this straight, the program uses the loopCount variable to loop everything after while(loopCount>0) statement?
Oh, yeah, I tried doing the while command after the code I just showed you all, but I put the condition as while(guess>=45||<=43). I guess that was the problem with the second try. Thanks a bunch.
-
it has to be like this:
if( guess >= 45 || guess <= 43 )
{
//do stuff
}
Everyone makes that mistake, I know I did - many, many times.
-
I feel so stupid. I have no idea why I didn't think of that.
-
-
One more problem. I got the program to loop fine and everything, but when it displays the message: That's correct, it just exits. Is there any way to make the program terminate by pushing a key?
-
> Is there any way to make the program terminate by pushing a key?
It depends.
Include conio.h if you have it, to get access to getch(), then just add getch() where you want your program to pause.
scanf could be done, but that would require hitting enter only.
> Klinerr1: lol.
...
-
So:
#include <iostream.h>
#include <conio.h>
int main()
{
//code
}
//wher I want it to pause
cout<<"That is correct. Press any key to exit.";
getch()
break;
}
return 0;
}
Or something like that. The getch() thing is right, right?
EDIT: I finally got it! Thanks everybody! Now, to move onto the next chapter.-_-