've been looking for so long trying to figure out how to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.
For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)
Here is the code, any help would be greatly.. GREATLY appreciated!
This is the write to text file method (100% working)
Code:
cout << "Customer Name: ";
cin >> name;
// ...
ofstream myfile("customer.txt", ios::app);
// ...
myfile << " ID: " << id << " Name: " << name << " NICN : " << nicn << " gender : " << gender << " Day : " << day
<< " Month : " << month << " Year : " << year << " Status : " << status << " Charges : " << charges << endl;
My search function..:
Code:
std::string search_customer(const std::string& name, const char* path = "customer.txt")
{
cout << "enter name: ";
getline(cin, name);
std::ifstream myfile(path);
std::string line;
while (getline(myfile, line))
{
std::istringstream stm(line); // create an input string stream which reads fronm the line
std::string token;
while (stm >> token && token != name_prefix); // read upto, and including, prefix
std::string str_name, str_suffix; // the next token is the name
if (stm >> str_name && str_name == name && stm >> str_suffix && str_suffix == name_sufffix)
return line;
}
std::cerr << "look up of name failed\n";
return "";