Okay here is some code I have made:

/*=============================================

Number Guessing Game
Uses the srand() function and loops.
James Duncan Bennet - james.bennet1@ntlworld.com
===========================================*/

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>

using namespace std;

int guess = 0;
int tries = 0;
string line;

int main()
{
srand(time(0));

int randomNumber = rand() % 50 + 1; // Generate a random number between 1 and 50

cout << endl << "The Number Guessing Game" << endl; // Display a welcome message for the user

ifstream infile ("hiscore.txt");   // Open hiscore file for reading

cout << "The highscore is: ";

while (! infile.eof() ) //Check the whole file
{
getline (infile,line);
cout << line ;
}
infile.close();

cout << endl;

do
{
cout << endl << "What is your guess? (1-50): ";
cin >> guess; // Put the user input in the variable "guess"

if (guess < randomNumber) // If the users guess is less than the random number
cout << "Too low. Try again!" << endl;

if (guess > randomNumber) // If the users guess is more than the random number
cout << "Too high. Try again!" << endl;

tries++; //Increment "tries" by 1 each loop

} while (guess != randomNumber); // Loop it while the users guess is not equal to the random number

cout << endl << "Ta-Dah! Thats the number I was looking for!" << endl;
cout << "It took you: " << tries << " attempts to guess the correct answer.\n" << endl; //Some feedback for the user

system("pause"); //Wait for any key to be pressed

ofstream outfile ("hiscore.txt"); //Open hiscore file for writing
outfile << tries << " Tries \n"; //Save hiscore
outfile.close();

return 0; //Return a successful execution
}

How can I make it only save the highscore if it is less than the existing one?

e.g 2 tries is better than 4

## All 4 Replies

Okay here is some code I have made:

/*=============================================

Number Guessing Game
Uses the srand() function and loops.
James Duncan Bennet - james.bennet1@ntlworld.com
===========================================*/

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>

using namespace std;

int guess = 0;
int tries = 0;
string line;

int main()
{
srand(time(0));

int randomNumber = rand() % 50 + 1; // Generate a random number between 1 and 50

cout << endl << "The Number Guessing Game" << endl; // Display a welcome message for the user

ifstream infile ("hiscore.txt");   // Open hiscore file for reading

cout << "The highscore is: ";

while (! infile.eof() ) //Check the whole file
{
getline (infile,line);
cout << line ;
}
infile.close();

cout << endl;

do
{
cout << endl << "What is your guess? (1-50): ";
cin >> guess; // Put the user input in the variable "guess"

if (guess < randomNumber) // If the users guess is less than the random number
cout << "Too low. Try again!" << endl;

if (guess > randomNumber) // If the users guess is more than the random number
cout << "Too high. Try again!" << endl;

tries++; //Increment "tries" by 1 each loop

} while (guess != randomNumber); // Loop it while the users guess is not equal to the random number

cout << endl << "Ta-Dah! Thats the number I was looking for!" << endl;
cout << "It took you: " << tries << " attempts to guess the correct answer.\n" << endl; //Some feedback for the user

system("pause"); //Wait for any key to be pressed

ofstream outfile ("hiscore.txt"); //Open hiscore file for writing
outfile << tries << " Tries \n"; //Save hiscore
outfile.close();

return 0; //Return a successful execution
}

How can I make it only save the highscore if it is less than the existing one?

e.g 2 tries is better than 4

when it comes time to save the high score read in the pre-existing scores and check them against the current score, if the current score is a better score then save, if not do not write out to file :)

cheers!

>How can I make it only save the highscore if it is less than the existing one?

Set up a variable at the start. Then update this variable if it meets the condition.

Other things, the following idiom for reading files is horribly flawed.

while (! infile.eof() ) //Check the whole file

while ( infile >> line)
{
//
}

After you retrieve data from your input file, convert your string, line, to an int. (Which can be done easily using a stringstream) then you can compare the two highscores, and only choose to overwrite the existing one if the new one is higher.