PDA

View Full Version : how to get this to loop (c++)




DaKyd55
Apr 20, 2011, 08:57 PM
so im extremely rusty in c++ and i have this simple guessing game i want to run. how can i set up the do while loop to get this to keep looping until the number is guessed correctly?

here is my code:

#include <iostream>

int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Guess the number I am thinking of, fool\n";
int x;
std::cin >> x;
do
{
if (x == 5)
{
std::cout << "You Guessed it! Go eat a cookie.";
}
else if (x > 2 && x < 8)
{
std::cout << "ooohhh..... almost";
}
else
{
std::cout << "Not even close, fool!!!";
}
return 0;
}
while (x == x);
}



lee1210
Apr 20, 2011, 09:08 PM
You could put the return in the x == 5 block, bad. Or you could make a done variable that you set in that block, better.

-Lee

subsonix
Apr 20, 2011, 09:09 PM
The problem doesn't seem to be C++ specifically, move your question inside the loop and change the condition to "x != 5" since this is you correct answer.

jiminaus
Apr 20, 2011, 10:48 PM
And move the unguarded return 0 out of the do{}while.

DaKyd55
Apr 21, 2011, 01:13 PM
when i make the while loop with ( x != 5) it says x was not declared in this scope

subsonix
Apr 21, 2011, 01:17 PM
when i make the while loop with ( x != 5) it says x was not declared in this scope

The x variable needs to be declared outside of the loop.


int x;

do {
// question and answers here
} while( x != 5 );

chown33
Apr 21, 2011, 01:19 PM
when i make the while loop with ( x != 5) it says x was not declared in this scope

Post your current code.

Your original code had x declared outside the loop. If you moved it to be declared inside the loop, then it won't exist outside the range defined by the { } of the loop's body. You'd have to move the declaration so it can be used in the while () conditional part of the loop.

Phil A.
Apr 21, 2011, 01:23 PM
when i make the while loop with ( x != 5) it says x was not declared in this scope

Personally, I'd do it with a for loop like this


#include <iostream>

int main (int argc, char * const argv[]) {
// insert code here...

for (int x = 0;x != 5;)
{
std::cout << "Guess the number I am thinking of, fool\n";
std::cin >> x;

if (x == 5)
{
std::cout << "You Guessed it! Go eat a cookie.\n";
}
else if (x > 2 && x < 8)
{
std::cout << "ooohhh..... almost\n";
}
else
{
std::cout << "Not even close, fool!!!\n";
}

}
}


However, if you want to use a do / while look you could do this



#include <iostream>

int main (int argc, char * const argv[]) {
// insert code here...
int x = 0;
do
{
std::cout << "Guess the number I am thinking of, fool\n";
std::cin >> x;

if (x == 5)
{
std::cout << "You Guessed it! Go eat a cookie.\n";
}
else if (x > 2 && x < 8)
{
std::cout << "ooohhh..... almost\n";
}
else
{
std::cout << "Not even close, fool!!!\n";
}

} while (x != 5);
}

DaKyd55
Apr 21, 2011, 05:45 PM
thanks guys it worked!! ill keep this open in case me or someone else have any more questions

actually sorry guys i have another question. it now puts in a random number and has you guess it, telling you if you're too high, too low, or correct. once you are correct the loop breaks. how can i put it so that the loop starts over again with a new random number? and how can i make it so that after the person is correct it asks them if they want to play again and if they hit y it replays and if not it ends. i forget the kbhit thing i dont even know if that works in xcode

chown33
Apr 21, 2011, 07:18 PM
actually sorry guys i have another question. ...

To summarize: you wrote some new code that doesn't work, which you haven't posted, and you expect us to tell you where it's broken simply by analyzing your somewhat vague and haphazard description?

Post your code. We can't debug descriptions.

DaKyd55
Apr 21, 2011, 08:25 PM
To summarize: you wrote some new code that doesn't work, which you haven't posted, and you expect us to tell you where it's broken simply by analyzing your somewhat vague and haphazard description?

Post your code. We can't debug descriptions.

oh thats awkward... i forgot to put it in..... and btw theres no problem with it i was just wondering how to so something with it

#include <iostream>

int main (int argc, char * const argv[]) {
// sean is awesome
srand((unsigned int)time(0));
int x = (rand() % 10) + 10;
int y;
std::cout << "Guess the number I am thinking of, fool\n";
while (y != x )
{
std::cin >> y;
if (x == y)
{
std::cout << "You Guessed it! Go eat a cookie.";
}
else if (y > x)
{
std::cout << "too high brah";
}
else if (y < x)
{
std::cout << "too low brah";
}
}
return 0;
}

chown33
Apr 21, 2011, 09:10 PM
The way to make a loop that starts over with a new random number is to make a loop that starts over with a new random number. Welcome to Tautology Club!

I break it down as follows:
1. Make a loop.
2. Inside the loop, assign a new random number.
3. Play the "guess the number" part of the game.
4. After game ends, ask user to play again or stop (you already have code that does this).
5. End of loop.

When you actually take the time to break down your vague description into logical steps, you'll see that it breaks down like the above. Breaking things down into logical steps is the very heart of programming. If you don't know how to do it for simple problems like the guess a number game, then you need to pick an even simpler problem and practice breaking it down. You'll get nowhere if you can't do the break-downs yourself, and you won't learn how to do break-downs by having someone else hand it to you. You have to practice doing it yourself. A lot.

DaKyd55
Apr 21, 2011, 10:24 PM
The way to make a loop that starts over with a new random number is to make a loop that starts over with a new random number. Welcome to Tautology Club!

I break it down as follows:
1. Make a loop.
2. Inside the loop, assign a new random number.
3. Play the "guess the number" part of the game.
4. After game ends, ask user to play again or stop (you already have code that does this).
5. End of loop.

When you actually take the time to break down your vague description into logical steps, you'll see that it breaks down like the above. Breaking things down into logical steps is the very heart of programming. If you don't know how to do it for simple problems like the guess a number game, then you need to pick an even simpler problem and practice breaking it down. You'll get nowhere if you can't do the break-downs yourself, and you won't learn how to do break-downs by having someone else hand it to you. You have to practice doing it yourself. A lot.

thanks for the advice, this is the first time ive done this since summer when i took a week so i forgot some stuff, but im starting to get it back