Hey all.. I'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)
Nope it prints out everything in the file or just the first line in general.. I want it to print only based on user input for example the system will ask user to input name and then it will display the line saved on the customer.txt holding that name, hope it's a bit more clear now :)
std::string search_customer(const std::string& name, constchar* 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"";
}
ETA: Considering I'm also new to programming.. it would be really helpful to tell me how can I call the method in main.. thank you and sorry for my silly questions :D