My number guessing game

This is a discussion on My number guessing game within the C++ Programming forums, part of the General Programming Boards category; Hey people. I am just starting to actually program a bit more rather than just read about c++ and have ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    My number guessing game

    Hey people.

    I am just starting to actually program a bit more rather than just read about c++ and have come up with a very basic number guessing game. I am having problems with writing effective functions and would be interested to know if a more experienced programmer would structure this problem differently.

    Also is there always a way to make a program object oriented no matter how simple it is. So for example if i were to make this OO would I declare a class GuessGame and just put the funcions and variables inside the class.
    Should I focus on procedural Programming first for a while or just get my hands dirty with classes from the moment i understand them .

    Any constructive feedback would be great . Thanks

    Code:
    #include<iostream>
    #include<stdlib.h>		// for random numbers
    #include<time.h>
    using namespace std;
    
    int ValidGuess(void);	
    int RandNum(void);
    void CompareNumbers(int NumToGuess,int Guess);
    
    int main(void)
    {	
    	srand((unsigned)time(NULL));
    
    	
    	int NumToGuess=RandNum();
    	int Guess=ValidGuess();	
        CompareNumbers(NumToGuess,Guess);
    
    	return(0);
    }
    
    // Makes sure the user enters a valid guess between 1-5
    int ValidGuess(void)		
    {
    	
    	int number;
    	do
    	{
    		char input[10];
    		
    
    		cout << "Enter a number" << endl;
    		cin.getline(input,9); //this will allow for a 9 digit number,which is bigger than an int anyways, so this should be fine
    		number = atoi(input); //Converts a digit string into a number
    				
    	}
    	while((number<1)||(number>5));
    
    	return number;
    }
    
    // This function produces the random number that the user has to guess
    int RandNum()
    {
    
    
    	int RndNum=rand()%5+1;
    	
    	return RndNum;
    }
    
    // This function compares the number produced by the computer and the number you guessed
    void CompareNumbers(int NumToGuess,int Guess)
    {
    	if(NumToGuess==Guess)
    	{
    		cout << "Ye Ha" << endl;
    		cout << "You guessed " << NumToGuess << endl;
    		cout << "Which is correct!" << endl;
    	}
    
    	else 
    	{
    		cout << "Unlucky Sunshine" << endl;
    		cout << "The number you were looking for was " << NumToGuess << endl;
    	}
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Also how come on some threads like mine here. You have to scroll across to read the sentence . I always find this annoying . Is it because of the long comment in my code?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    My Way

    I'm not experianced, (only a year in C++) but I vary rarely use functions other than "main()" unless I have to.

    But this is from a 14 year old redneck, so don't go on that.
    I have arms?

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    I'm not experianced, (only a year in C++) but I vary rarely use functions other than "main()" unless I have to.

    But this is from a 14 year old redneck, so don't go on that.
    I assume you also use void main();. You read a crappy book for C++, or maybe read some online tutorial. You aren't very advanced and you have a REALLY tough time debugging. Your programs are cluttered and you get lost sometimes because your main function is so long. Call me Cleo .

    USE FUNCTIONS. THEY'RE CLEANER.

    It's ok, I forgive you, no one expects much from rednecks.

  5. #5
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    (itld puts on flame proof suit)
    it is my understanding that "using namespace std" polutes the entire name space with every function in the code and this use should be avoided.

    BTW im a damn redneck, in fact i am sitting within 200 feet of 5 horses as i speak. so there!

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Is it because of the long comment in my code?
    It's probably due to your use of real tab characters when you indent your code

    This seems to excessively indent your code when posted on the message board.

    I think you have your tab stops set to 4 spaces, so it looks OK for you. This line
    > CompareNumbers(NumToGuess,Guess);
    is indented differently in your post, and just has 4 spaces

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Also is there always a way to make a program object oriented no matter how simple it is. So for example if i were to make this OO would I declare a class GuessGame and just put the funcions and variables inside the class.
    Should I focus on procedural Programming first for a while or just get my hands dirty with classes from the moment i understand them .

    Thanks.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Also is there always a way to make a program object oriented no matter how simple it is.
    Like a single class with a single method.

    Yeah, I'd say its possible, but "hello world" would be a bit more complicated using a hello class, than simply printing "hello world".


    > would I declare a class GuessGame and just put the funcions and variables inside the class
    Sounds about right

    As for what to learn, just go with whatever you feel comfortable with.

    If you're only going to use C++, then go for the class/OO approach.

    If you're going to use C as well, then the procedural programming aspects are a good start.

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, 11: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, 01:48 AM
  3. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 04:05 AM
  4. Number guessing.
    By Lunatic Magnet in forum C Programming
    Replies: 5
    Last Post: 04-07-2006, 01:43 AM
  5. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM

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