Why wont my function work here?

#include <iostream>
#include <istream>
#include <fstream>
#include <string>

using namespace std;

void FindName(ifstream& inFile);


int main()
{
char repeat = 'y';
ifstream inFile;
inFile.open("phone.txt");
if ( !inFile )
{
cout << "Cannot open input file." << endl;
return 1;
}


cout << "Do you want to search for a name? Enter yes or no." << endl;
cin >> repeat;
while (repeat == 'y')
{
FindName(inFile);
cout << "Do you want to search for a name? Enter yes or no." << endl;
cin >> repeat;
}

inFile.close();
system("PAUSE");
return 0;
}

void FindName(ifstream& inFile)
{
string firstName, lastName;
string firstNametxt, lastNametxt, numbertxt;
char nameFound = 'n';
//inFile.close(); dont know if this needs to be here?
inFile.open("phone.txt");

cout << "Enter the name of the person you would like to search for!" << endl
<<" Please capitalize both the first and last name. Names should be seperated by a space." << endl;
cin >> firstName >> lastName;

while (inFile &&(nameFound == 'n'))
{

inFile >> firstNametxt >> lastNametxt >> numbertxt;

if (firstName == firstNametxt && lastName == lastNametxt)
{
cout << "The person's number is: " << numbertxt << endl;
nameFound = 'y';
}
}
if (nameFound == 'n')
cout << firstName << "" << lastName << " was not found in directory." << endl;
}

Last edited on
What exactly isn't working? You might want to copy the contents of the text file here as well or else it's hard to see what could be going wrong.

Using code tags around your code [ Code] [ /Code] would be nice too.
It will compile however when I enter a name that should be in my txt file, it says "was not found in directory". I dont think the void function is working properly. I am trying to extract the phone number if there is a match.

John Black 307-665-3456
Jake Long 455-444-3456
Kate Little 406-665-5672
Mike Taylor 709-455-3454
Terry Shell 303-677-4544
I am a rookie to the utmost degree and this is my first posts about code here.
The only problem I see is when you pass the text file into the function you are opening it again when it is already opened (you opened it in main). Which is causing problems.

You could fix this by removing the comment from .close() but there wouldn't be a point to it. Delete both the open and close functions in the void function and it should all work correctly.
Thank you, That worked. You are my hero. Thank you very much, I have been working on that for a while.
One last question, when I type a name that is not in my directory I recieve the correct error message, however when I try to run another search with a name I know is in there it says its not in the directory. I am trying to figure out why but if you can catch it please let me know.
That is because the position in the file is not being reset after everything loop, so you will not be able to find anything before the current name you searched. There are a few ways you could handle this but you could just reset the file position to the beginning every loop using something like this.

1
2
inFile.clear();  //Removes any eof() flags
inFile.seekg(0,ios::beg); // Resets file to the beginning 
I follow the concept but am not sure where to place them. At the end of each loop. Could you give me an example because I have been wondering how to just reset a program to the beginnin if needed.
Just placing that at the beginning of your void function should work. It doesn't reset the program is just makes the position in the text file reset to the start.
Thanks again for your input, you have been a great help.
Topic archived. No new replies allowed.