Thread: Quick Help, Kinda Lost here

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Quick Help, Kinda Lost here

    Okay well I know this is a no brainer to most of you but I am brand new to the language and programming so could I get some help . Basically I want to make a quick game where you have to guess a number within 5 tries.

    Its pretty crazy what happens when you execute my program i've created an endless loop of some kind.

    Code:
    #include <iostream.h>
    int main()
    	{
    
    		//declerations
    		int guess=0; //ammount of guesses
    		int UserNum=0; //the number the user guested
    		int GenNum=10; //the random number, 10 until i figure out how ro rand
    		
    				cout<<"Please guess a number between 1-100:  ";
    				cin>>UserNum;
    					while(guess!=5) //as long as guess isnt 5 keep going
    							if (UserNum==GenNum)
    								{
    									cout<<"You win!";
    									break;
    								}
    							else
    									cout<<"Please try again!";
    									cin>>UserNum;
    						guess++;
    					cout<<"You Lose";						
    		return 0;	
    	}
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're missing a lot of braces. Forexample in your else statement and in your while statement.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    Code:
    #include <iostream.h>
    int main()
    	{
    
    		//declerations
    		int guess=0; //ammount of guesses
    		int UserNum=0; //the number the user guested
    		int GenNum=10; //the random number, 10 until i figure out how ro rand
    		
    				cout<<"Please guess a number between 1-100:  ";
    				cin>>UserNum;
    					while(guess!=5) //as long as guess isnt 5 keep going
    						{	
    							if (UserNum==GenNum)
    								{
    									cout<<"You win!";
    									break;
    								}
    							else
    								{
    									cout<<"Please try again!"<<endl;
    									cin>>UserNum;
    									guess++;
    								}	
    						//i have messed these 2 braces up !!
    						}
    					cout<<"You Lose";						
    		return 0;	
    	}
    okay there is no more endless loop but it doesnt quit after 5 guesses it just keeps going
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Works for me. You also might try better formatting that makes the code readable. Maybe something like:
    Code:
    #include <iostream.h>
    int main()
    {
        //declerations
        int guess  = 0; //ammount of guesses
        int UserNum= 0; //the number the user guested
        int GenNum =10; //the random number, 10 until i figure out how ro rand
    	
        cout << "Please guess a number between 1-100:  ";
        cin >> UserNum;
        while(guess != 5) //as long as guess isnt 5 keep going
        {	
            if (UserNum == GenNum)
            {
                cout << "You win!";
                break;
            }
            else
            {
                cout<<"Please try again!"<<endl;
                cin>>UserNum;
                guess++;
            }	
            //i have messed these 2 braces up !!
        }
        cout << "You Lose";						
        return 0;	
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM