iofstream problem

closed account (EwCjE3v7)
Hello there, for some reason the file highestScore, has the value 0 and it dosnt even change when I get a higher score. The code is the last few lines. before the return 0;

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <fstream>
using std::cout; using std::endl; using std::cin;
using std::vector; using std::string;
using std::ifstream; using std::ofstream;

vector<string> load_countries ()
{
    vector<string> countries;
    ifstream  ifs ("countries.txt", ifstream::in);
    string temp;

    while (ifs.good()){
        ifs >> temp;
        countries.push_back (temp);
    }
    ifs.close ();
    return countries;
}

string game() // the main game
{
    vector<string> countries = load_countries();

    srand(time(NULL));
    int randomIndex = rand() % countries.size(); // choose a random country

    auto firstLetter = countries[randomIndex].cbegin(), lastLetter = countries[randomIndex].cend() -1; // iterators to first and last letter

    cout << "First letter is: " << *firstLetter << endl;
    cout << "Last letter is: " << *lastLetter << endl;

    return countries[randomIndex];
}

int main()
{
    unsigned points = 0, tries = 0, crntTries = 0, hints = 0, crntHints = 0, giveUps = 0;

    cout << "Points:" << points << "\t\tHints:" << hints << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries
            << ")\t\tGive ups:" << giveUps << "\n--------------------------------------------------------------------------------\n"
            << "I will give you the first and last letter of a country.\nYou have to guess the country. Points will be counted. \nYou can get a hint by typing \"hint\", or give up by typing \"give up\"\n" << endl;

    string country = game();
    string input;

    while (getline(cin, input)) {
        if (!input.empty()) {
            input[0] = toupper(input[0]);

            if (input == country) {
                cout << "\nCorrect!!!\n" << endl;

                ++points;
                ++tries;
                crntHints = 0;
                crntTries = 0;

                cout << "Points:" << points << "\t\tHints:" << hints << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries << ")\t\tGive ups:"
                        << giveUps << "\n--------------------------------------------------------------------------------\n" << endl;

                country = game();

            } else if(input == "Hint") {

                char letter;

                if (crntHints == 0) {
                    ++hints;
                    ++crntHints;

                    cout << string(8,'\n') << "Points:" << points << "\t\tHints:" << hints << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries
                            << ")\t\tGive ups:" << giveUps << "\n--------------------------------------------------------------------------------\n"
                            << "\nHere is your hint" << endl;

                    letter = country[1];
                    cout << "The second letter is: " << letter << endl;

                } else if (crntHints == 1) {
                    ++hints;
                    ++crntHints;

                    cout << string(8,'\n') << "Points:" << points << "\t\tHints:" << hints << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries
                            << ")\t\tGive ups:" << giveUps << "\n--------------------------------------------------------------------------------\n"
                            << "\nHere is your hint" << endl;

                    letter = country[2];
                    cout << "The third letter is: " << letter << endl;

                } else {
                    cout << "No more hints that all." << endl;
                }

            } else if (input == "Give up") {
                ++giveUps;
                crntHints = 0;
                crntTries = 0;

                cout << string(8,'\n') << "Points:" << points << "\t\tHints:" << hints << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries
                        << ")\t\tGive ups:" << giveUps << "\n--------------------------------------------------------------------------------\n"
                        << "The country was: " << country << "\nTry another one. \n" << endl;

                country = game();
            } else {
                ++tries;
                ++crntTries;

                cout << string(8,'\n') << "Incorrect!!! Try again.\n" << "Points:" << points << "\t\tHints:" << hints
                        << "(" << crntHints <<  ")\tTries:" << tries << "(" << crntTries << ")\t\tGive ups:"
                        << giveUps << "\n--------------------------------------------------------------------------------\n" << endl;
            }
        }
    }

    ifstream getScore("highestScore.txt", ifstream::in);

    int HS;
    getScore >> HS;

    if (points > HS) {
        ofstream newScore("highestScore.txt", ofstream::out);
        newScore << HS << endl;
    }

    return 0;
}
closed account (EwCjE3v7)
Oh lol, I`m assign the file its only value. line 126 should be newScore << points << endl;
Topic archived. No new replies allowed.