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

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

You should instead use:-

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.

A few other comments regarding your code -

Global variables are generally a bad thing... break the habit now while you're still in the early stages of learning. In this case, you'll not need to change anything by declaring them just inside the start of your main() block.


change your loop which runs while !EOF, since this will lead to reading past the end of the file. The EOF flag is only set after getline fails to read, so the final iteration of your loop is likely to behave unpredictably. Try this instead while( getline(infile, line) ) This causes the loop to run while the file still has data, and automatically terminates when there's no more data to read, preventing any chance of overrun.

This article has been dead for over six months. Start a new discussion instead.