Thread: Is this a loop situation?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    Is this a loop situation?

    Hi guys, im new to any kind of coding and am loving it. Ive been writing a simple little number guessing game, and have a question. The question is in the code. Thanks

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    		
    int main() {
     
    	//SetConsoleTitle("The Guessing Game!");
    	int guess;                     
            int clue;
    	
    	
    	 cout << "Hello Internet\n";
    	 cout << "Today, we are going to play the Guessing Game!\n";
    	 cout << "\n";
    	 cout << "You are going to pick a number from 1-100 and if you need help\n" << "Id be  happy to give you clues!\n" << "\n";
    	 
    	 cout << "Pick a number: "; 
    	 cin >> guess;
    	 cout << "\n";
    	 cin.ignore();                       
      
    		if ( guess < 74 ) { 
                    cout << "Sorry, you guessed wrong!\n";
    		cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    		char clue;
    		cin >> clue;
    			if ( clue =='y' ) {
    			cout << "You guessed low, sorry! Try again!\n";
    			}
    			else {
    			// How do I send the user back up to cin >> guess at the earlier point in the code?
    			}
    
            }
    
            else if ( guess > 74 ) {            
            cout << "Sorry, you guessed wrong!\n";
    		cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    		char clue;
    		cin >> clue;
    	    }
    
            else {
            cout << "You won the game!\n";    
            }
    
      cin.get();
     
      return 0;
    }
    Please ignore alignment lol. Does not look like this is Editor.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    33
    I am better coder then you...I was attacked in anotjher fred, i wont offer help to anyone except elysia

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't mix spaces and tabs and... use a do...while loop.
    That should solve it all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Code:
     int max_nb_games = 100;
     bool stopgame = false;
     while( !stopgame && game_i < max_nb_games) {// !stopgame is equivalent to 'stopgame == false'
    
         cout << "Pick a number: "; 
         cin >> guess;
         cout << "\n";
         cin.ignore();                       
         
         if ( guess < 74 ) { 
    	 cout << "Sorry, you guessed wrong!\n";
    	 cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    	 char clue;
    	 cin >> clue;
    	 if ( clue =='y' ) {
    	     cout << "You guessed low, sorry! Try again!\n";
    	 }
    	 else {
    	     stopgame = true; // stops the game
    	 }
         }
    
         else if ( guess > 74 ) {            
    	 cout << "Sorry, you guessed wrong!\n";
    	 cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    	 char clue;
    	 cin >> clue;
         }
    
         else {
    	 cout << "You won the game!\n";
    	 stopgame = true; // stops the game	 
         }
    
         game_i++;
    
     } // end while
    The user input block is simply repeated 'max_nb_games' times until the bool is false.

    You should not declare 'clue' as an 'int' earlier in the game, since you re-declare it as a 'char' lateron.
    Note the beautiful indentation ;-)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MarkZWEERS View Post
    You should not declare 'clue' as an 'int' earlier in the game, since you re-declare it as a 'char' lateron.
    I don't think it's a bad thing, but a better idea might be to rename the "char clue" to something else, to resolve ambiguity.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    18
    lol, ive never used any loops yet, but i figured it out and is pretty simple. I used with my original code, but thanks for the example markzweers. I dont know a lot of the functions you used. Im still very new =)

    but my game is done!!!

    I guess the next fun thing to do with it, would be to have it randomly generate the number your supposed to guess, every time you start it up. Is that possible? I assume. How would you do something like that?

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Haha you might use this:

    http://cboard.cprogramming.com/showthread.php?t=103071

    But there exist functions to create pseudo-random numbers : rand() and srand()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM