Im a bit confused

Pages: 123
But it doesnt load the name though. when i do load.

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
132
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <time.h>
#include <random>

using namespace std;

void start(int &S, string &N);
void save(int &S, string &N);
string RanF();
void load(int &S, string &N);

int main()
{
    int score = 0;
    string name;
    string choice;

    cout << "1) New" << endl;
    cout << "2) Load" << endl;
    cin >> choice;
    cin.sync();

    if(choice == "1")
    {
        cout << "Enter your name" << endl;
        getline(cin, name);
        start(score, name);
    }
    else if(choice == "2")
    {
        load(score, name);
    }
    return 0;
}


void start(int &S, string &N)
{
    string choice;

    cout << "Ok " << N << " Enter the sentances exactly as you see them to earn points\n" << endl;

    do
    {
        string val = RanF();
        cout << val << endl;
        getline(cin, choice);

        if(choice == val)
        {
            S += 10;
            cout << "Current Score: " << S << endl;
        }

        else if(choice == "quit")
        {
            save(S,N);
            break;
        }
    }while(choice != "quit");
}


void save(int &S, string &N)
{
    ofstream file;
    file.open("file.txt", ios::app);

    time_t current = time(0);

    file << ctime(&current) << endl;
    file << "";
    file << "Score: " << S << endl;
    file << "Name: " << N << endl;
    file << "" << endl;

    file.close();
}

void load(int &S, string &N)
{
    ifstream file;
    file.open("file.txt");

    file >> S;
    file >> N;

    file.close();

    start(S,N);
}


string RanF()
{
    int TIME;
     string sent;

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 6;

    switch(TIME)
    {
        case 0:
            sent = "Grand Theft Auto San Andreas";
            return sent;
        case 1:
            sent = "GTA IV";
            return sent;
        case 2:
            sent = "Dead Island";
            return sent;
        case 3:
            sent = "Minecraft";
            return sent;
        case 4:
            sent = "Borderlands";
            return sent;
        case 5:
            sent = "Crackdown 2";
            return sent;
        default:
            cout << "Error" << endl;
    }
}


bump
closed account (o3hC5Di1)
Let's look at your two functions for save and load:

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
void save(int &S, string &N)
{
    ofstream file;
    file.open("file.txt", ios::app);

    time_t current = time(0);

    file << ctime(&current) << endl;
    file << "";
    file << "Score: " << S << endl;
    file << "Name: " << N << endl;
    file << "" << endl;

    file.close();
}

void load(int &S, string &N)
{
    ifstream file;
    file.open("file.txt");

    file >> S;
    file >> N;

    file.close();

    start(S,N);
}


You save the following format:

1
2
3
4
5
    file << ctime(&current) << endl;
    file << "";
    file << "Score: " << S << endl;
    file << "Name: " << N << endl;
    file << "" << endl;


And you read it as such:

1
2
    file >> S;
    file >> N;


What you are reading would equal to a file content of:

score name


In other words, S and N divided by a space.
Either change the format you save to to store it in that way, or change the format of how you extract the information from the file - the former is probably the easiest option as you would just do:

1
2
3
4
5
6
7
8
9
void save(int &S, string &N)
{
    ofstream file;
    file.open("file.txt", ios::app);

    file << S << " " << N;

    file.close();
}


All the best,
NwN
Topic archived. No new replies allowed.
Pages: 123