# Thread: My number guessing game

1. ## 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. 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. ## 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.

4. 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. 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.

6. > 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. 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. > 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