Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 20, 2011, 08:57 PM   #1
DaKyd55
macrumors member
 
Join Date: Jul 2010
how to get this to loop (c++)

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 kainjow; Apr 21, 2011 at 12:18 AM. Reason: code tags
DaKyd55 is offline   0 Reply With Quote
Old Apr 20, 2011, 09:08 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Apr 20, 2011, 09:09 PM   #3
subsonix
macrumors 68030
 
Join Date: Feb 2008
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.
subsonix is offline   0 Reply With Quote
Old Apr 20, 2011, 10:48 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
And move the unguarded return 0 out of the do{}while.
jiminaus is offline   0 Reply With Quote
Old Apr 21, 2011, 01:13 PM   #5
DaKyd55
Thread Starter
macrumors member
 
Join Date: Jul 2010
when i make the while loop with ( x != 5) it says x was not declared in this scope
DaKyd55 is offline   0 Reply With Quote
Old Apr 21, 2011, 01:17 PM   #6
subsonix
macrumors 68030
 
Join Date: Feb 2008
Quote:
Originally Posted by DaKyd55 View Post
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 );
subsonix is offline   0 Reply With Quote
Old Apr 21, 2011, 01:19 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by DaKyd55 View Post
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.
chown33 is offline   0 Reply With Quote
Old Apr 21, 2011, 01:23 PM   #8
Phil A.
macrumors 68040
 
Phil A.'s Avatar
 
Join Date: Apr 2006
Location: Telford, UK
Quote:
Originally Posted by DaKyd55 View Post
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);
}
__________________
Tell a man there are 300 billion stars in the universe and he'll believe you. Tell him a bench has wet paint on it and he'll have to touch it to be sure. ~Murphy's Law
Phil A. is offline   0 Reply With Quote
Old Apr 21, 2011, 05:45 PM   #9
DaKyd55
Thread Starter
macrumors member
 
Join Date: Jul 2010
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 kainjow; Apr 21, 2011 at 07:34 PM. Reason: merged posts
DaKyd55 is offline   0 Reply With Quote
Old Apr 21, 2011, 07:18 PM   #10
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by DaKyd55 View Post
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.
chown33 is offline   0 Reply With Quote
Old Apr 21, 2011, 08:25 PM   #11
DaKyd55
Thread Starter
macrumors member
 
Join Date: Jul 2010
Quote:
Originally Posted by chown33 View Post
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 kainjow; Apr 22, 2011 at 12:03 AM. Reason: code tags
DaKyd55 is offline   0 Reply With Quote
Old Apr 21, 2011, 09:10 PM   #12
chown33
macrumors 603
 
Join Date: Aug 2009
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.
chown33 is offline   0 Reply With Quote
Old Apr 21, 2011, 10:24 PM   #13
DaKyd55
Thread Starter
macrumors member
 
Join Date: Jul 2010
Quote:
Originally Posted by chown33 View Post
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
DaKyd55 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Recovery Loop | Cant get iPhone 5 6.1.3 out of loop Fatush iPhone Tips, Help and Troubleshooting 1 Sep 19, 2013 09:14 PM
iPod Touch: Reboot loop :( fiatpanda iOS 7 2 Jul 6, 2013 05:18 PM
iPhone: iPHone 4 stuck in DFU loop then boot loop then DFU loop! urbanmacUser Jailbreaks and iOS Hacks 1 Jun 7, 2013 04:30 PM
restart loop SiaChris MacBook 4 May 28, 2013 09:45 AM
iPhone 4 Stuck on restore loop, Help! I'm stuck on anger and frustration loop! vegas60 iPhone Tips, Help and Troubleshooting 3 Nov 8, 2012 08:49 AM

Forum Jump

All times are GMT -5. The time now is 05:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC