Whats going on is an address book. There is a search function in the program that takes in a string for the phone number. The file is read and if the string exist, it file is reread and the characters infront of it are sent to output.... basically showing who the number belongs too... But.. I need help...

cout<<"Enter number: ";
getline(cin, q, '\n');
string line;
  ifstream AB ("AddressBook.txt");
  if (AB.is_open())
  {
    while (! AB.eof() )
    {
      getline (AB,line);
      if (line == q){
      cout << "Match Found!" << endl;
      cout << "Number Belongs To: "<<endl;
      cout<<endl;
      
      char ch;
      while (! AB.eof() )
      {
            ios_base::cur;
            AB.get(ch);
            cout<<ch;
            while (! AB.eof() )
            {
                  getline (AB,line);
                  if (line == q){break;}
            }
      }

Im sure its something simple, but I keep getting the first letter of the persons name in the txt of who the number belongs too.

I can't see how this code does anything. You have 3 nested while loops all exiting on the same condition. And if you read this you'll see your condition is wrong ( feof() is the same as .eof() )

Then in the second loop, you read a single character and output it. Other than that, no other output of note.

If you happen to find the input string in the file, why do you reread the file?

You need to consider the format of your input. I will presume something like

Avery Brown
129 Landis St.
Chapel Hill, NC 01234
555-1234
Emma Green
13 Rabbit Run Rd.
Chapel Hill, NC 01234
555-2933

Every four lines is a different record. So read one whole record at a time (that is, read four lines at a time [as per my example, or however many lines per record you are actually working with]).

So, some pseudo might be:

get phone number from user
do
  get record from file
  if eof then complain and quit
while phone number from file doesn't match phone number from user
print entire record

Also, make sure you get a good reference. There is no such function as is_open(). You can test success directly from the object:

ifstream AB( "AddressBook.txt" );

if (!AB) {
  cerr << "I couldn't open the file 'AddressBook.txt'." << endl;
  return EXIT_FAILURE;
  }

// file is fine, ask user for phone number and do while loop go here

AB.close();

// print record found or tell user that record was not found

Hope this helps.

Yeah that helps a lot.

The txt file is as follows..

7165555555
Jimmy Eatworld
555 Ocean Avenue
Miami
Florida
15216

6165555555
Johnny Dep
321 George Avenue
Columbus
Ohio
52615

So. the program takes in the phone number and searches the file for the number. Say 6165555555 is entered.

6165555555 is found.

now from that point on i just want to get the characters after it until the next phone number is read.... But i am trying your method

problem solved... thanks

If your problem has been solved...please mark this thread as being solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.