#include <iostream>
#include <stdlib.h>
usingnamespace std;
//---------------------------------Interface----------------------------------//
class guessNumber
{
public:
guessNumber(int guess)
{
Number = generate();
Guess = guess;
}
bool Comparator();
bool isBigger();
private:
int generate();
int Number;
int Guess;
};
//-------------------------------Implementation-------------------------------//
int guessNumber::generate()
{
srand ( time(NULL) );
return (rand()%20 + 1);
}
bool guessNumber::Comparator()
{
if (Number==Guess) returntrue;
elsereturnfalse;
}
bool guessNumber::isBigger()
{
if (Number>Guess)returntrue;
elsereturnfalse;
}
//----------------------------------Main--------------------------------------//
int main()
{
int g;
guessNumber GN; //ERROR LINE 48
cout<<"I'm thinking of a number between 1 and 20.\nYou have three guesses to find out the number."<<endl;
for (int guess=1;guess=3;guess++)
{
cout<<"Guess "<<guess<<": ";
cin>>g;
GN(g); //ERROR LINE 54
if (GN.Comparator())
{
cout<<"Yup! That's the number alright. Way to go!"<<endl;
cin.get();
return 0;
}
elseif(GN.isBigger())
{
cout<<"Nope. Think of a larger number."<<endl;
continue;
}
else
{
cout<<"Nope. Think of a smaller number."<<endl;
continue;
}
}
cout<<"HAHA! you couldn't guess my Number!"<<endl;
system ("pause");
return 0;
}
If you're wondering, the interface implementation headers are there cuz i find reading codes with classes in them a little confusing).
Anyway, I got the following error in line 48 and 54
no matching function for call to 'guessNumber::guessNumber()'
how do you fix that?
Second, how can this code be more efficient?
1. create a constructor guessNumber() that just calls Number = generate();
2. create a new method void guess( int myGuess ) { }
3. edit your main() appropriately
You fell into a trap because you have not given your class a good enough name (you tried using that name both as a constructor and as a function name).
Don't worry about efficiency yet - get your code right first.
Thansk alot - I got the code working fine.
One thing, though. Is it possible to get the number generator to generate a single random number and stick with it. Right now, it generates a different number for each try.