C++ telephone directory getting number from a file I need to add a loop but not sure which one

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
#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;
}
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace 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.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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;
	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;
}
Topic archived. No new replies allowed.