//The following program I wrote is a guessing game
//program. The program chooses a random number, which the user should
//attempt to guess. Hints are given to the player instructing him/her
//that the guess is too high or too low. The program ends if either
//the correct number is guessed, or the user does not guess the number
//within 10 tries. If the user guesses the correct number, the
//program proceeds to reports
//a. the number of tries it took to guess the number.
//b. The incorrect guesses made by the user.
//Using structures I declared a data-type called struct PlayingPerson.
//This structure should keep track of a player's name and score.
//The score is defined to be the number of guesses the user needed to guess the correct answer.
//The struct PlayingPerson should be given an alternate name Player,
// using the typedef statement.
//A file called HighScores.dat should contain the name and
//score of the player with the lowest score.
//(Note: the lower the score, the better the player).
//The first thing your game should do is open this file and
//read in the name and score contained within it. Also, the
//current player's name should be read in from the keyboard.
//After the game is ended, the score of the current player
//is compared with that read in from the file.
// If the score of the current game is less than that read
//in from the HighScored.dat file, the current name and score
//are recorded in the HighScores.dat file, thereby replacing
//the old data in it. If this is the first time the program is run,
//the file HighScores.dat does not exists yet.
//If then your program attempts to open this file,
//it will fail. In this case the file should be created at
//the end of your program, and the current score and name recorded
//in it.
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
struct playingperson
{
char playersname[50];
int score;
};
typedef struct playingperson player;
void populate (player &populateplayingperson);
void print (player printplayingperson);
int main ()
{
player a;
populate(a);
print(a);
int guesses[10];
int y,x;
int zero =0;
srand((unsigned)time(NULL));
int mynumber=rand();
mynumber=(mynumber%100)+1;
cout<<"I've got a number from 1 to 100, can you guess it?"<<endl;
for(y=0;y<10;y++)
{
cout<<"Your Guess: ";
cin>>guesses[y];
zero++;
if (guesses[y] > mynumber)
{
if (zero == 10)
{
break;
}
cout<<"Guess Lower."<<endl<<endl<<"Guess again"<< endl;
}
else if (guesses[y] < mynumber)
{
if (zero == 10)
{
break;
}
cout<<"Guess Higher."<<endl<<endl<<"Guess again" << endl;
}
else
{
cout << "Congratulation YOU GOT IT!!" << endl;
break;
}
}
for(x=0;x<zero;x++)
cout<<"Guess "<<x+1<<" was: "<<guesses[x]<<endl;
cout<<"The correct answer is: "<<mynumber<<endl;
cout << "You guessed " << zero << " times." << endl;
return 0;
}
void populate(player &populateplayingperson)
{
cout<<"Players Name: "<<endl;
cin>>playingperson.playersname;
cout<<"Players number of guesses: "<<endl;
cin>>playingperson.score;
}
void print (player printplayingperson)
{
cout<<"Players Name: "<<player.playersname<<endl;
cout<<"Players number of guesses: "<<player.score<<endl;
}
I can not seem to get this to work correctly. Could someone steer me in the right direction. Any help would be appreciated.
Thanks