problem reading .txt files/ saving high scores

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <stdlib.h> /* Include srand and rand */
#include <stdio.h>  /* Include printf() */
#include <time.h>   /* Include time() */
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace 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;
        }
        else if ( 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!
.get() gets a single character, I'm not even sure how you are managing to assign that to a double without an error.

Use Game >> highscore, just like cin.
never mind that fixed it. Thanks a ton i've been trying to figure this out forever
Last edited on
Topic archived. No new replies allowed.