Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DaKyd55

macrumors member
Original poster
Jul 3, 2010
81
0
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:

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);
}
 
Last edited by a moderator:

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 68040
Feb 2, 2008
3,551
79
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.
 

DaKyd55

macrumors member
Original poster
Jul 3, 2010
81
0
when i make the while loop with ( x != 5) it says x was not declared in this scope
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.

Code:
int x;

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

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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.

Moderator emeritus
Apr 2, 2006
5,799
3,094
Shropshire, UK
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

Code:
#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

Code:
#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

macrumors member
Original poster
Jul 3, 2010
81
0
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
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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

macrumors member
Original poster
Jul 3, 2010
81
0
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

Code:
#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;
}
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
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

macrumors member
Original poster
Jul 3, 2010
81
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.