Hello! So I am making a contact manager and I am using a text file to hold some information on the contacts the user inputs. I need to search this file and output the name, email(s), and phone number(s) of the contact. I understand how to write and read from/to a file and I can search a file for one line of code but I'm having difficulties searching this file and outputting several lines of code. Here is an example of what my text file looks like after some user input
John Doe
theblank@email.com
theblank@email.com
555-555-5555
So I need to have the user search this file by inputting the name John Doe and then the program should output the following three lines. The amount of emails and phone numbers the user inputs is tied to a vector so that is not a set amount. Therefore, the search must be able to flux between outputting just a few lines of code to many. For example, a contact could look like this.
John Doe
theblank@email.com
theblank@email.com
theblank@email.com
555-555-5555
555-555-5555
So I need to figure out how to write a search algorithm for a text file to search for a name and output the rest of the information about the contact. The amount of information can vary. I don't think any code is necessary here but if you want to take a look I can post it.
A blank line?
A name line following a phone number line?
A special character?
If you have control over the file format, you can make it whatever you find convenient. A blank line between records is very, very convenient.
Thereafter, all you need to do is read the file until you find the contact name, then print the name, then read and print lines until you read a blank line (or whatever separates records) and quit.
Sorry for not mentioning that. They are just separated by a blank line. That's what I was thinking but I'm not sure how to implement it.
So read the file while it's true that there are characters? How would I code the program to recognize a blank line? Should I put in a flag, possibly a tildy (~) or something between contacts?
Here's a slightly different way. It's more complex than Thomas1965's solution but it anticipates the need to read and write full records of a contact.
The idea is to create functions that read and write a single contact, and then test them by simply reading a set if contacts and writing them out again. Once you have this working, adding code to filter out some contacts is easy.
Interestingly enough, that's very similar to how I have my code set up dhayden. I actually already had the write/read stuff done but it's pretty similar to how you set it up. I really appreciate the reply though, I managed to clean up my code based on how you had it.