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>
usingnamespace 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;
}
Program worked for me, but needed a bit of logic to exit properly.
Make sure your data file is not .txt or something
dir name*.*
I like all your comments, so don't mind showing you my changes.
One thing I recommend is after your if/else statements always put in a {}.
It makes reading and fixing code much easier.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
// char chr; // Not needed
int main()
{
string first;
string last;
string number;
string firstfile;
string lastfile;
string numberfile;
char cont='y'; // changed from INT
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
while (cont=='y')
{
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;
if (cont=='y')
{
cout<<"Enter a first and last name."<<endl; //Asking user for the input
cin>>first>>last; //input the data
}
}
infile.close(); //close file
//cin>>chr;
return 0;
}
Ok I changed a few things but it still just returns Sorry that is not in our database. Would you like to search for another name? Y or N
It's not much different...Also I don't understand how this can work for you SameulAdams and not work for me? I am using a txt file what should I use instead?
Here is what I have.....
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
char chr;
int main()
{
string first;
string last;
string number;
string firstfile;
string lastfile;
string numberfile;
char 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
}
if(!infile)
{
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;
}