input/output problem

hey guys
i want to make a program that is used to enter the information of 11 players
in a structure array like that

struct players
{
std::string name;
std::string nationality;
std::height;
std::Weight;


};

and i used array like that
players arr[11];

and i filled it using for loop like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int c=0;
 for(int i = c ; i < 11 ;)
                {

                  cout<<"Please enter the name of the player no."<<i+1<<endl;
                  cin>>arr[i].name;
                  cout<<"Now please enter the nationality of "<<arr[i].name<<endl;
                  cin>>arr[i].nationality;
                  cout<<"Now please enter the height of "<<arr[i].name<<endl;
                  cin>>arr[i].height;
                  cout<<"Now please enter the weight of "<<arr[i].name<<endl;
                  cin>>arr[i].weight;
                  cout<<"Player is successfully added"<<endl<<endl;
                  c++;

                }
                  cout<<"You exceed the number of your products"<<endl<<endl;


i want to know how to use input/output files to save these data in txt file
and be able to view it again using another for loop
when i used this code within the for loop it saves only the last entered player's info
how can i make it saves all of them !?

1
2
3
4
5
6
7
8
ofstream myfile;
  myfile.open ("players.txt");
  myfile <<arr[i].name<<endl;
  myfile <<arr[i].nationality<<endl;
  myfile <<arr[i].height<<endl;
  myfile <<arr[i].weight<<endl;

  myfile.close();


ALSO as you can see i can't enter a name with space
how can i fix this ? using getline ? how please

many thanks
Last edited on
Open file only once before the loop. Each time you are opening you file you are discarding its content.

To read a line with spaces a getline is commongly used. However read this article on problem you will run into.
http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
thank you
Topic archived. No new replies allowed.