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;
}
}