I have written a program that stores booking information in a text file in c++. So far I can search the booking number and check if that number exists in the text file and print “Record is found”.
Now when I try to print the line in which the booking information is stored , the program always prints the last line in the text file.
for example it says that “Record 123 is found” but it prints the record “999” which is the last in the txt file.
Can you help me solve this problem by showing me some codes , about how I can print a specific line from a file.
My code is like this :
if ((i = search.find(x)) != search.end())
{
cout << id << name << phone number << credit card number<<...endl;
}
this code allways prints the last line.
Without seeing your code, we are left to guess blindly, and typically we don’t have the patience for that (no offense, it’s just that typically our guesses are wrong).
That said, I’ve got a hunch that what you are doing is using locals to read in your data and store them in some sort of structure (I’m guessing a
std::map) and then when you search for the data you are finding it, but making the mistake of using the locals instead of the iterator.You should be using your iterator (which you label
xin your example) to output the data. For example, ifsearchis astd::map, then the data for the value is stored in thesecondpart of thestd::pairreturned fromfind():