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