so i made a basic guessing game and i am trying to save the best scores. it is writing them into the text file fine and when i check the text file it has the correct best score but when the program is asked to display the best score it displays a seemingly random number. Here is my code
#include <stdlib.h> /* Include srand and rand */
#include <stdio.h> /* Include printf() */
#include <time.h> /* Include time() */
#include <iostream>
#include <cstdlib>
#include <fstream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
srand (time(NULL));
int numberoftries = 0;
int secret;
double guess;
secret = rand() % 100 + 1;
ifstream Game;
Game.open ("highscore.txt");
double highscore = Game.get();
Game.close();
cout << "You're highscore is ";
cout << highscore;
cout << endl;
while ( guess != secret )
{
numberoftries = numberoftries + 1;
cout << "I'm thinking of a number between one and one hundred can you guess it?";
cin >> guess;
if ( guess < secret )
{
cout << "You guessed to low";
cout << endl;
}
elseif ( guess > secret )
{
cout << "You guessed to high";
cout << endl;
}
}
if (numberoftries < highscore)
{
highscore = numberoftries;
ofstream Game;
Game.open ("highscore.txt");
Game << highscore;
Game.close();
}
cout << "Congrats it took you ";
cout << numberoftries ;
cout <<" tries to guess it";
cout << endl;
cout << "You're high score is ";
cout << highscore;
cout << endl;
system("PAUSE");
return(0);
}
for example when i run this program it claims the previous best score is 57 but yet when i go to the text file it says the best score is 4. Please tell me any coding errors i may have made or any other advice as to how to fix this problem. Thanks!