hi I wanna make a text file in my program and I want to insert 2 names and 2 numbers to it but I have a problem that when I add a new data the perivious one is gone. please it's an emergency
#include <iostream>
#include <fstream>
#include<string>
usingnamespace std;
int main () {
int x,j,k;
do{
string a,b;//a,b are names
cin>>a>>b;
cin>>j>>k>>x;//j,k are numbers
//x=1 is to add data x=0 to show data x!=2 is to end the program
if(x==1){
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << a<<j<<endl;
myfile << b<<k<<endl;
myfile.close();
}
else cout << "Unable to open file";
}
if(x==0){
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
}
}while(x!=2);
return 0;
}