cant figure this out.

I cant get my program to load the data in my program when i load the game, it jsut says welcom to

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
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int treasury(float &M, string &PN, string CN);
void game(string &PN, string CN, float &M);

int main()
{
    int choice;
    float money = 500000;
    string Pname;
    string Cname;

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

    switch(choice)
    {
        case 1:
            {
                ofstream file("President.txt");

                cout << "Welcome please enter your name" << endl;
                cin >> Pname;
                cout << "\n";
                cin.sync();
                cout << "Thank you " << Pname << " now please enter your countries name" << endl;
                cin >> Cname;
                game(Pname, Cname, money);

                file << Pname << endl;
                file << Cname << endl;
                file << money << endl;
            }
            break;
        case 2:
            {
                ifstream file("President.txt");

                file >> money;
                file >> Pname;
                file >> Cname;

                game(Pname, Cname, money);
            }
            break;
    }
}

void game(string &PN, string CN, float &M)
{
    cout << "Welcome " << PN << " to " << CN << endl;
}

int treasury(float &M)
{
    cout << "NATIONAL TREASURY\n" << endl;

    cout << "Current money in treasury: " << M << endl;
}
Did u try getline instead ?
No, its not even reading from the file i dont think. Its supposed to load the stuff then set it all but id doesnt
And the file is definitely not empty...?
Contents look something like this:
1
2
3
joe
blo
9999
?
Other than explicitly closing the file after each case I cant think of what's wrong...though it's not in a loop...
Last edited on
so what do i do.
Nevermind i figured it out i outputted data like this

file << Pname << endl;
file << Cname << endl;
file << money << endl;

and was trying to read it in like this

file >> money;
file >> Pname;
file >> Cname;
Ok grrr i got a new problem. So the data is written correctly but not loaded correctly still. I load the game and it should say "welcome Chay Hawk to America" but instead it says welcome Chay to Hawk. ?? why is that? i think its reading the white space and skipping or something. how do i fix that?
Oh man, I've got to get my eyes checked out!!!
The order of 36, 37, 38 doesn't match 45, 46, 47. Namely, the types don't match. The first insertion operator on line 45 attempts to put the string (name) into the integer (money) and 'file' crashes there (gracefully).
Last edited on
Nevermind i figured out the other problem too.
Topic archived. No new replies allowed.