How to record different inputs in textfile

This program records a name in a text file, anybody can help me how to record multiple names in text file every time I run the program...tnx in advance

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
string name=" ";
ofstream outData("test.txt");

cout << "tie example: " << endl;
cout << "this is inserted into cout. " << endl;
cout<<"Name: ";
cin>>name;
outData << name << endl;

outData.close();
}
Last edited on
Hey man, just add a flag when you create your file.

use
ofstream outData("test.txt",ios::app);
instead of just:
ofstream outData("test.txt");

There are a selection to choose from but ios::app stands for append i think, and so will append your file next time you open it, rather than clearing the previous data.
@shaggy tnx much... i made it...next problem is that when i input two words only the first word is accepted in that same code...tnx in advance
Last edited on
Yeah, use getline() instead of cin cause, cin accepts characters until the first white space, but getiline accepts the line(until enter key is pressed)

Eg:
getline(cin,str);

Where str is a string.

Hope it HELPS!!!
Topic archived. No new replies allowed.