how to get this to loop (c++)

Discussion in 'Mac Programming' started by DaKyd55, Apr 20, 2011.

  1. DaKyd55, Apr 20, 2011
    Last edited by a moderator: Apr 20, 2011

    macrumors member

    Joined:
    Jul 3, 2010
    #1
    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);
    }
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    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.
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    And move the unguarded return 0 out of the do{}while.
     
  5. thread starter macrumors member

    Joined:
    Jul 3, 2010
    #5
    when i make the while loop with ( x != 5) it says x was not declared in this scope
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    The x variable needs to be declared outside of the loop.

    Code:
    int x;
    
    do {
        // question and answers here
    } while( x != 5 );
    
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. macrumors 601

    Phil A.

    Joined:
    Apr 2, 2006
    Location:
    Telford, UK
    #8
    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);
    }
    
     
  9. DaKyd55, Apr 21, 2011
    Last edited by a moderator: Apr 21, 2011

    thread starter macrumors member

    Joined:
    Jul 3, 2010
    #9
    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
     
  10. macrumors 603

    Joined:
    Aug 9, 2009
    #10
    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.
     
  11. DaKyd55, Apr 21, 2011
    Last edited by a moderator: Apr 21, 2011

    thread starter macrumors member

    Joined:
    Jul 3, 2010
    #11
    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;
    }
     
  12. macrumors 603

    Joined:
    Aug 9, 2009
    #12
    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.
     
  13. thread starter macrumors member

    Joined:
    Jul 3, 2010
    #13
    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
     

Share This Page