I am trying to write a phonebook program that uses a dat file to store the numbers (ex. in dat file--John Doe 5554445555). I have it to the point when the user keys "John" in, it will display the number. If the user keys in "John Doe", it will not work. I am entry level programming so the pointer will not move back to the top of the file unless the program is closed and restarted (which is fine for this assignment). I just need to to work with keying in John Doe....Please help if you can (Be sure to create a phone.dat file to run properly)! Thanks!
{
// Open file
ifstream phonebook;
string nameToLookUp;
string firstName;
string lastName;
string phoneNumber;
char runAgain;
firstName="";
lastName="";
runAgain = 'y';
phonebook.open("Phone.dat");
if ( !phonebook ) { //If input doesn't open...
//no
cout << "Can't open the phonebook file." << endl;
system("PAUSE");
return 1; //terminate the program
}
while (runAgain=='y'||runAgain=='Y') {
phonebook.close();
phonebook.open("Phone.dat");
cout<<"What Name to look up? ";
cin >> nameToLookUp;
while (firstName!=nameToLookUp&&lastName!=nameToLookUp&&!phonebook.eof()) {
phonebook >> firstName >> lastName >> phoneNumber;
}
if (!phonebook.eof())
cout << phoneNumber << endl;
else
cout << "Could not find the phone number" << endl;
cout << "Run again? ";
cin >> runAgain;
}
phonebook.close();
system("pause");
return (0);
}