Try this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int RandomNumber (int x) {
srand((unsigned) time(0));
int randomNumber = (rand() % 88) + 11;
cout << randomNumber << endl;
int result = randomNumber % x;
cout << result << endl;
if(!result) return 1;
else return 0;
}
int main() {
int y, iWinner, iClient=0, iMachine=0;
while(iClient<5 && iMachine<5){
cout << "Type a number: ";
cin >> y;
cout << y << endl;
iWinner = RandomNumber (y);
if(iWinner) iClient++;
else iMachine++;
cout << "Client:" << iClient << "Machine:" << iMachine << endl;
}
if(iClient>=5) cout << "Client Wins!" << iClient << "to" << iMachine;
else cout << "Machine Wins" << iMachine << "to" << iClient;
return 0;
}
|
It may take some tweaking. You really don't need to make a class, unless, that was the point of the exercise. If you really do need to make a class, then, your code might look more like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
class RandGame {
private: int randomNumber, result;
public: int x, iClient, iMachine;
void RandGame(void); // "constructor" declaration (optional)
int RandomNumber (void); // function declaration
};
void RandGame::RandGame(void){ // constructor function (optional)
iClient = 0;
iMachine = 0;
return;
}
int RandGame::RandomNumber (void) {
srand((unsigned) time(0));
int randomNumber = (rand() % 88) + 11;
cout << randomNumber << endl;
int result = randomNumber % x;
cout << result << endl;
if(!result) return 1;
else return 0;
}
int main() {
int iWinner;
RandGame Rand; // create object from class
while(Rand.iClient<5 && Rand.iMachine<5){
cout << "Type a number: ";
cin >> Rand.x;
cout << Rand.x << endl;
iWinner = Rand.RandomNumber ();
if(iWinner) Rand.iClient++;
else Rand.iMachine++;
cout << "Client:" << Rand.iClient << "Machine:" << Rand.iMachine << endl;
}
if(Rand.iClient>=5) cout << "Client Wins!" << Rand.iClient << "to" << Rand.iMachine;
else cout << "Machine Wins" << Rand.iMachine << "to" << Rand.iClient;
return 0;
}
|
This is just a crude example. There are many different ways of doing this. The class is named, "RandGame". You use this name to declare your object (variable) in the same way you might use "int", "char", or "float". The "constructor" is automatically called, when your object is created (defined). A constructor always has the same name as the class. You don't necessarily need a constructor. But, this is a good example of its use---in this case, setting the scores to Zero. You call variables and functions in the class, in the same way that you would members of a "struct".
As I mentioned, there are may different ways to use a class in this example. For instance, you could put your "while" loop in a class function. Or, you could do away with your constructor, and simply initialize your variables in your main() function. You could make "iWinner" part of the class. Or, you could take "iClient" and "iMachine" out of the class, initialize them in main(), and do-away with the constructor. It all depends on ho creative you wish to be.
You could, if you wish, also add a "destructor" function. That would be defined by, "RandGame::~RandGame()". in the case of this example, you could use the destructor to display the final score.
Hope this helps.