c++ - Printing Multiple Strings to a File -


i working on phone book, , have far been able have user input info, save string, save string file, , search string using keyword. however, functional first contact inputted. when new contact inputted, overwrites previous string saved in file. how can make print next line, , develop list of contacts can searched through. appreciated. here code:

#include <iostream> #include <string> #include <ostream> #include <fstream>  using namespace std;  struct person {     string name;     string address;     string phonenumber;     string email; };  int main() { resetuserselection:     vector<person>contactlist;      int userselection = 0;      cout << "press 1 add contact" << endl;     cout << "press 2 search contact"<<endl;     cout << "what want do? ";     cin >> userselection;      if(userselection == 1)     {         person newperson;          cout << endl;         cin.ignore();         cout << "what name? " ;         getline(cin,newperson.name);          cout << "what address? " ;         getline(cin,newperson.address);          cout << "what phone number? " ;         getline(cin,newperson.phonenumber);          cout << "what email? ";         getline(cin,newperson.email);          cout << endl;          string fullcontact = "name: " + newperson.name + ", address: " +     newperson.address + ", phone number: " + newperson.phonenumber + ", email: " +     newperson.email + "\n";          cout <<fullcontact;          cout << endl;          ofstream myfile;         myfile.open ("contactlist.txt");         myfile << fullcontact;         myfile.close();         goto resetuserselection;          goto resetuserselection;     }     else     {         string search;          ifstream myfile;         myfile.open ("contactlist.txt");         cout << "who want search for?" << endl;         cin >> search;         cout << endl;          ifstream myfile;         myfile.open ("contactlist.txt");         string contact;         while(getline(myfile, contact))         {             if(contact.find(search) != string::npos)             {                 cout << search << "'s contact info" << endl << contact << endl << endl;             }         }         myfile.close();          goto resetuserselection;     } } 

when open file writing, set filepointer start of file default. should add append option when opening file.

see api of ofstream::open how this:

myfile.open ("contactlist.txt", ofstream::out | ofstream::app); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -