Hi, im new here. I was trying to teach myself C++ before I have to take it in the spring. I came across this example, and I don't understand how it works
d_random.h is a random number generation program
this first part is called, header.h
#include <iostream>
#include "d_random.h"
using namespace std;
class playHiLow
{
public:
playHiLow()
{
randomNumber r;
cout<<" Try to guess the right number between 1-1000"<<endl;;
do {
target = r.random();
}
while(target > 1000);
count = 1;
}
int makeGuess() {
if(count > 10) {
cout<<" Sorry, you did not find the number ";
cout << " The correct number is: " <<target<<endl;
exit(1);
}
count++;
cout<<" make a guess: ";
int x;
cin>>x;
if(x == target)
return 0;
else
if(x < target)
return -1;
else
return 1;
}
void writeTarget() {
cout<<"The number is "<<target;
}
int count;
private:
long target;
};
______________________________________…
second part
#include <iostream>
#include "header.h"
using namespace std;
void main()
{
playHiLow game;
while(1)
{
if (game.count <= 10)
cout<<" Enter your guess # "<<game.count;
int x = game.makeGuess();
if(x == 0)
{
cout<<" Correct! ";
return;
}
if(x == -1)
{
cout<<"Yout guess is too low..."<<endl ;
if(game.count<10)
cout<<"Try again." ;
}
if(x == 1)
{
cout<<"Yout guess is too high..." <<endl;
if(game.count<10)
cout<<"Try again.";
}
}
}
/*
Try to guess the right number between 1-1000
Enter your guess # 1 make a guess: 510
Yout guess is too high...
Try again. Enter your guess # 2 make a guess: 210
Yout guess is too low...
Try again. Enter your guess # 3 make a guess: 225
Yout guess is too low...
Try again. Enter your guess # 4 make a guess: 380
Yout guess is too high...
Try again. Enter your guess # 5 make a guess: 376
Correct! Press any key to continue . . .