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 <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
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."<<endl;
return 0;
}

//Order of data from file
inFile1 >>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: " <<endl;
cin>> firstName >> lastName;

{
//inFile1 >> firstNamedoc >> lastNamedoc>>phoneNumberdoc;
if( firstName == firstNamedoc && lastName == lastNamedoc)
cout<< phoneNumberdoc <<endl;
else
cout<<"Number was not in directory."<<endl;
}
cout <<"Want to continue looking? 0 for no, 1 for yes. " <<endl;
cin >>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 <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
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: " <<endl;
cin>> firstName >> lastName;

phonedir();

cout <<"Want to continue looking? 0 for no, 1 for yes. " <<endl;
cin >>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."<<endl;
return 0;
}

//Order of data from file
Do
{
inFile1 >>firstNamedoc >> lastNamedoc >> phoneNumberdoc;

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

If (flag = 1)
cout<<"phone number is: "<< phoneNumberdoc <<endl;
else
//error message
cout<<"Could not find person in directory. "<<endl;
}

Recommended Answers

All 3 Replies

Member Avatar for MonsieurPointer

Please use code tags

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';
}
Member Avatar for MonsieurPointer

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.

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.