the question for this program is :write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers in a file conatining a list of names and phone numbers. The user should be prompted to enter a first and last name, and the program then outputs the the corresponding number, or indicates that the name isn't in the directory.
for this program, i'm supposed to use functions to code the problem. The code works but it runs incorrectly. When I enter name, it asks me to repeat the process instead of giving me the number. when i type 'n', it gives me the number. Really appreciate your help! Thank you.
CODE
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
bool checkName(string, string, int, string, string);
bool cont(char);
int main()
{
ifstream inData;
inData.open("phone.dat");
if(!inData)
{
cout << "Invalid file. Please check it again" << endl;
return 1;
}
string firstName, lastName, first, last;
int number;
char yes = 'y';
cout << "Enter a first and last name: "<< endl;
cin >> first >> last;
while(cont(yes))
{ inData >> firstName >> lastName >> number;
while (inData)
{
checkName(firstName,lastName,number,first,last);
// inData >> firstName >> lastName >> number;
}
inData.close();
inData.open("phone.dat");
}
if (inData)
cout << number;
else
cout << "The name isn't in the directory" << endl;
return 0;
}
bool checkName(string firstName,
string lastName,
int number,
string first,
string last)
{
bool Mybool = true;
if ( first == firstName && last == lastName)
return 1;
else
return 0;
}
bool cont(char yes)
{
bool myBool = true;
cout << "Do you want to look up another number(y/n)?"<< endl;
cin >> yes;
if (yes == 'y')
return 1;
else return 0;
}