save name-text rpg

Ok, I found a good save system that works, but I can't save a custom name. I tried using getline to ask the user a name, but no matter how I arrange the strings and variables, i always get errors.Here's the code, can anyone figure it out?
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

typedef struct
{

  int strength;
  int health;
  int max_health;
  int name;
  int x;
  string s1;

  


  // ...
} PlayerData;

int main()
{
    string x;
    cout<<"What is your name?"<<endl;
    s1 (cin, x);
    
  // initialize
  PlayerData player;
  player.strength=10;
  player.health=98;
  player.max_health=100;
  string s1;
  s1 = "x";


  // save
  ofstream fout("data.txt");

  fout << "strength " << player.strength << "\n";
  fout << "health " << player.health << "\n";
  fout << "max_health " << player.max_health << "\n";
  fout << "name "<<s1<<"\n";


  fout.close();

  // clear
  player.strength = player.health = player.max_health = 0 ;
  // read
  string line;
  ifstream fin("data.txt");
  if (fin.is_open())
  {
    while (fin.good())
    {
      getline(fin,line);
      stringstream ss(line);
      string fieldname;
      int value;
      ss >> fieldname >> value;
      if (fieldname == "strength")
        player.strength = value;
      if (fieldname == "health")
        player.health = value;
      if (fieldname == "max_health")
        player.max_health = value;
      if (fieldname == "s1")
         s1 = value;

    }
  }


  // display
  cout << "strength=" << player.strength << "\n";
  cout << "health=" << player.health << "\n";
  cout << "max_health=" << player.max_health << "\n";
  cout << "name is "<<s1<< "\n";
 
  
  system("pause");
  return 0;
}
1
2
3
fout << "name "<<s1<<"\n";
//...
if (fieldname == "s1")


See the problem?
Topic archived. No new replies allowed.