Help!!! Number guessing game

This is a discussion on Help!!! Number guessing game within the C++ Programming forums, part of the General Programming Boards category; i'm not asking for someone to write the program for me because i already did that. the problem is that ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Help!!! Number guessing game

    i'm not asking for someone to write the program for me because i already did that. the problem is that i can't get it to generate a random number. i could put in a number and have people guess but i want it to create a new number on its own every time it is started up.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you familiar with rand()? I believe there is a FAQ on how to use it. If you've already tried it but it doesn't work then post your effort here and details on how it's not working.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    was playing around with the rand() but my program just froze!!!! but i think i remembered what i did so i'll send you it in a pm as soon as i write it over again.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    just realized that i don't know how to send a private message

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Don't send a private message. Post the code and problems here. You might not even be allowed to send a PM until you've been around a while.

    If you don't want to post the exact code for some reason, come up with a small example that does the same thing. Look at the FAQ and try to implement the examples there, then move it into your code. At the very least you can post the line of code that uses rand().

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    i think that i just sent it to you

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    my first program, kinda sloppy. i know how to add a loop so i'm not worried about that
    using Borland C++ 4.5

    Code:
    //Asst. 1
    //Guessing game
    
    #include <iostream.h> // allows input and output to be displayed.
    
    int main()
    {
    	int x;
    	cout << "Do you think that you are able to compete with Nostradamus?\n";
    	cout << "1 for yes, 0 for no\n";
    	cin >> x;
    	if( x == 0 ){
    		cout << "Too bad, thought you were special.\n";
    	}
    	else{
    	cout << "Really!? Then let's see what you got.\n";
    	cout << "I'm thinking of a number between 1 and 100.\n";
    	cout << "Think you can guess it with ten tries?\n";
    	}
    	int y;
    
    	cin >> y;
    	if( y == 24){
    	cout << "Wow, you must be Nostradamus reincarnated.\n";
    	}
    	else
    		if (y < 24-11){
    		cout << "You're way off, don't bother trying again\n";
    		}
    		else
    			if (y > 21+11){
    			cout << "What were you thinking?\n";
    			}
    			else
    				if (y >= 24-10){
    				cout << "You're warming up\n";
    				}
    				else
    					if (y <= 24+10){
    					cout << "You're warming up\n";
    					}
    					else
    						if (y <= 24+5){
    						cout << "You're burning up\n";
    						}
    						else
    							if (y >= 24-5){
    							cout << "You're burning up\n";
    							}
    
    	 return 0;
    
    
    }

  8. #8
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Code:
    cin >> y;
    	if( y == 24){
    this makes no sense. if i answer "y" as in "yes", i think i can guess your number; the program stops.

  9. #9
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    nevermind, it's just that your prompt is a little confusing.

  10. #10
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    you will however need a loop so the user can continue guessing.

    something like:
    Code:
    while(guess != myNumber)
    {
        cin >> guess;
    
        if(guess == myNumber)
        {
            cout << "You got it!" << endl;
        }
    
        else
        {
            cout << "keep guessing..." << endl;
        }
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    This:
    Code:
    	if( y == 24){
    	cout << "Wow, you must be Nostradamus reincarnated.\n";
    	}
    	else
    		if (y < 24-11){
    		cout << "You're way off, don't bother trying again\n";
    		}
    		else
    			if (y > 21+11){
    			cout << "What were you thinking?\n";
    			}
    			else
    				if (y >= 24-10){
    				cout << "You're warming up\n";
    				}
    				else
    					if (y <= 24+10){
    					cout << "You're warming up\n";
    					}
    					else
    						if (y <= 24+5){
    						cout << "You're burning up\n";
    						}
    						else
    							if (y >= 24-5){
    							cout << "You're burning up\n";
    							}
    Is more conventionally written as:
    Code:
    if (y == 24) {
        cout << "Wow, you must be Nostradamus reincarnated.\n";
    }
    else if (y < 24-11){
        cout << "You're way off, don't bother trying again\n";
    }
    else if (y > 21+11){
        cout << "What were you thinking?\n";
    }
    else if (y >= 24-10){
        cout << "You're warming up\n";
    }
    else if (y <= 24+10){
        cout << "You're warming up\n";
    }
    else if (y <= 24+5){
        cout << "You're burning up\n";
    }
    else if (y >= 24-5){
        cout << "You're burning up\n";
    }
    It looks like you just need to add:
    Code:
    srand(time(0));
    int x = rand() &#37; 100; // An example for an integer in the range [0, 100)
    Then change your literal 24 to x.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    i'm trying these right now, thanx a lot.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    You guys mean like this?
    Code:
    //Asst. 1
    //Guessing game
    
    #include <iostream.h> // allows input and output to be displayed.
    
    int main()
    {
    	int x;
    	cout << "Do you think that you are able to compete with Nostradamus?\n";
    	cout << "1 for yes, 0 for no\n";
    	cin >> x;
    	if( x == 0 ){
    		cout << "Too bad, thought you were special.\n";
    	}
    	else{
    	cout << "Really!? Then let's see what you got.\n";
    	cout << "I'm thinking of a number between 1 and 100.\n";
    	cout << "Think you can guess it with ten tries?\n";
    	}
    	srand(time(0));
    	int g = rand() &#37; 100; // An example for an integer in the range [0, 100)
    	int y
    	while(y != g){
    	cin >> y;
    
    	if( y == g){
    	cout << "Wow, you must be Nostradamus reincarnated." <<endl ;
    	}
    	else if (y < g-11){
    		cout << "You're way off, don't bother trying again." << endl;
    		}
    	else if (y > g+11){
    		cout << "What were you thinking?" << endl;
    		}
    	else if (y > g-10){
    		cout << "You're warming up" << end1;
    		}
    	else if (y < g+10){
    		cout << "You're warming up" << end1;
    		}
    	else if (y <= g+5){
    		cout << "You're burning up" << end1;
    		}
    	else if (y >= g-5){
    		cout << "You're burning up" end1;
    		}
    	}
    
    	 return 0;

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That's pretty close. You're missing a semi-colon, though, and you'll want to initialize y to a number that can't be the correct number (like -1 maybe). Also, as you probably know from your research on rand() you have to #include <stdlib.h> and <time.h> for the rand(), srand() and time() functions.

    Did you try running it?

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    yea, and got three errors. it's probably because i didn't do #include <stdlib.h> and <time.h> for the rand(), srand() and time() functions.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  4. Number guessing.
    By Lunatic Magnet in forum C Programming
    Replies: 5
    Last Post: 04-07-2006, 12:43 AM
  5. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21