C++ program Telephone Directory from a file

Posted by Stacy Doyle on Stack Overflow See other posts from Stack Overflow or by Stacy Doyle
Published on 2013-11-03T21:50:41Z Indexed on 2013/11/03 21:54 UTC
Read the original article Hit count: 271

Filed under:

I am writing a program for a phone directory. The user inputs a name and the program searches the file and either outputs the number or an error because the persons name is not in the file. The program should also ask the user if they would like to continue using the program and look up another number. So far runs and asks for the name and then prints the error message that I put in place saying that the name is not in the database. I am guessing that I must not really be having my program look in the file but not sure what to do also don't know how to get the program to run again if the user chooses to continue.

#include <iostream>  
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
char chr;


int main()
{
string first;
string last;
string number;
string firstfile;
string lastfile;
string numberfile;
int cont;
ifstream infile; 
infile.open("name and numbers.dat"); //opening the file

infile>>firstfile>>lastfile>>numberfile;

    cout<<"Enter a first and last name."<<endl; //Asking user for the input

cin>>first>>last;   //input the data
{
if(first==firstfile && last==lastfile)  //if the entered information matches the information in the file

    cout<<first<<" "<<last<<"'s number is "<<numberfile<<endl; //this is printed
else 
    cout<<"Sorry that is not in our database."<<endl;   //if the information doesn't match this is printed
}
cout<<"Would you like to search for another name? Y or N"<<endl;  //user is asked if they would like to continue
cin>>cont;

infile.close(); //close file


cin>>chr;
return 0;
}

© Stack Overflow or respective owner

Related posts about c++