954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Phone Directory Lookup Problem-Please help!

Im having a hard time figuring out what is wrong with my program. I need to write a program that looks up a phone number in a file containing a list of names and phone numbers. The user is to input the first name and last name to look up, and the program should output the phone number or display an output message saying that it is not in the file (phone number directory). Then the program should ask the user if he or she wants to look up another name. Here is what I got the first time I tried to write it:

#include
#include
#include
#include
#include
using namespace std;

int main()
{
ifstream inFile1;
string firstName;
string lastName;
string phoneNumber;
string firstNamedoc;
string lastNamedoc;
string phoneNumberdoc;
int playagain;
//getline(cin, phoneNumberdoc);
//ifstream Infile1;

inFile1.open("TelephoneDirectory.txt");
//exit if file cannot be opened
if (!inFile1)
{
cout <<"Cannot open input file."<>firstNamedoc >> lastNamedoc >> phoneNumberdoc;
do
{
//Ask user to enter first and last name.
cout<<"Please enter the first and last name to look up, seperated by a space: " <> firstName >> lastName;

{
//inFile1 >> firstNamedoc >> lastNamedoc>>phoneNumberdoc;
if( firstName == firstNamedoc && lastName == lastNamedoc)
cout<< phoneNumberdoc <>playagain;
}
while (playagain == 1);


system("PAUSE");
return 0;

}

The above program only works if the first name on the file is the one searched, otherwise it displays that the name cannot be found.

I then tried to write it another way, but ths one dosent work either. It gives me an error for the void function. Any help would be greatly appreciated, as I am not completely lost and frustrated at this point.

#include
#include
#include
#include
#include
using namespace std;
void phonedir();

int main()
{
ifstream inFile1;
string firstName;
string lastName;
string phoneNumber;
string firstNamedoc;
string lastNamedoc;
string phoneNumberdoc;
int playagain;

do
{
//Ask user to enter first and last name.
cout<<"Please enter the first and last name to look up, seperated by a space: " <> firstName >> lastName;

phonedir();

cout <<"Want to continue looking? 0 for no, 1 for yes. " <>playagain;
}
while (playagain == 1);

}

Void phonedir(string firstName, string lastName)
{
Int flag = 0;
inFile1.open("TelephoneDirectory.txt");
//exit if file cannot be opened
if (!inFile1)
{
cout <<"Cannot open input file."<>firstNamedoc >> lastNamedoc >> phoneNumberdoc;

if((firstname == firstNamedoc) && (lastname == lastNamedoc))
{
Flag = 1;
break;
}
}
While(inFile1);

If (flag = 1)
cout<<"phone number is: "<< phoneNumberdoc <

pimpingreen13
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Please use code tags

MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

submiting as per my understanding as per my understanding. check out if it works for u.

cout<<"names\n";
cin>>firstName;
cin>>lastName; 


//Order of data from file

while(inFile1){
inFile1>>firstNamedoc >> lastNamedoc >> phoneNumberdoc;
//cout<<firstNamedoc<<"\t"<<lastNamedoc<<"\t"<<phoneNumberdoc<<"\n";
if ((firstName == firstNamedoc) && lastName == lastNamedoc)
  cout<<phoneNumberdoc<<"\n";

firstNamedoc='\0'; lastNamedoc='\0'; phoneNumberdoc='\0';
}
T.N.Sharma
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

My C++ is somewhat rusty, but I think the line

inFile1>>firstNamedoc >> lastNamedoc >> phoneNumberdoc;


only reads in the first line of the file. In order to solve your problem, you will have to loop through the file, store each line into a container (array / list / vector / map). You will then need to search through the container until the data the user has entered matches the criteria.

MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: