I am currently making a phonebook. Once the user has inputted their data, it saves all of the data as a single string (Name, address, phone number, email). I am very new to C++. Any help will be much appreciated. Thanks.
There are two major issues:
After the first contact is saved to the file, the proceeding contacts do not show up in the file
I would like to make it so that the user can search for the name, or one keyword that they inputted as part of their contact, and get the entire string containing ALL of their info. How can I do this?
#include <iostream>
#include <string>
#include <ostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
ResetUserSelection:
int userselection = 0;
cout << "Press 1 to Add Contact" << endl;
cout << "Press 2 to Search for Contact"<<endl;
cout << "What do you want to do? ";
cin >> userselection;
if(userselection == 1) {
person newPerson;
cout << endl << "What is your Name? ";
cin >> newPerson.Name;
cout << "What is your Address? " ;
cin >> newPerson.Address;
cout << "What is your Phone Number? " ;
cin >> newPerson.PhoneNumber;
cout << "What is your Email? ";
cin >> newPerson.Email;
cout << endl;
cout << newPerson.Name<<"'s contact info"<<endl;
cout << "Name : " << newPerson.Name <<endl;
cout << "Address : " << newPerson.Address <<endl;
cout << "Phone Number : " << newPerson.PhoneNumber <<endl;
cout << "Email : " << newPerson.Email <<endl;
cout << endl;
string fullContact = newPerson.Name + " " + newPerson.Address + " " + newPerson.PhoneNumber + " " + newPerson.Email;
ofstream myfile;
myfile.open ("contactlist.txt");
myfile << fullContact;
myfile.close();
goto ResetUserSelection;
}
else {
string search;
ifstream Myfile;
Myfile.open ("contactlist.txt");
cout << "Who do you want to search for?" << endl;
cin >> search;
//i do not know what to do here
goto ResetUserSelection;
}
}