Input File Help-- How to read all lines in input file?

My program only reads the first line of the file that I open. For example, the first line reads "john smith 123-456-7890". The program asks for the first and last name and prints out the corresponding phone number. However, it only shows the phone number for the first name. If any of the other names on the file are inputted on the program, it shows "Sorry, this name is not in our database". Can someone fix this? Thank you so much!

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
50
51
52
53
 #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	string firstName;
	string lastName;
	string number;
	string firstNameInFile;
	string lastNameInFile;
	string numberInFile;
	string cont = "y";
	ifstream inData; 
	bool dataIsOk;
	inData.open("numberfile.dat"); //opening the file

	inData >> firstNameInFile >> lastNameInFile >> numberInFile;
	cout << "Enter a first and last name: "; //Asking user for the input
	cin >> firstName >> lastName;   //input the data

	//loop check
	while (cont == "y")
	{
		if( firstName == firstNameInFile && lastName == lastNameInFile )  //if input is true
		{
			cout << "Name: " << firstName << " " << lastName << endl << "Number: " << numberInFile << endl; //print
		}
		//if input is false
		else 
		{
			cout << "Sorry, that name is not in our database." << endl << "Make sure spelling is correct." << endl; 
		}

		cout << "Would you like to search for another name? y or n" << endl;  //user is asked if they would like to continue
		cin >> cont;

		//loop program if input is y
		if (cont == "y")
		{
			cout << "Enter a first and last name: "; //Asking user for the input
			cin >> firstName >> lastName ;   //input the data
		}
	}

	inData.close(); //close file


	return 0;
}
Last edited on
What is the error message?
What does your file looks like?
Last edited on
Since you only ever read one line from the file what do you expect? If you want to find some other name you'll need to read more than one line from the file using some kind of loop.
I don't know how to make a loop so that the program reads all the lines in the input file. What I mean by error message is that the program will say "sorry, that name is not in our database" for any name other than the first line in the file.

SO-- this is my input file
john smith 123-432-1238
jane doe 123-124-8574
bob bobbert 910-231-5433

when I run the program and continue to search for another name, this is what the output looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Enter a first and last name: john smith
Name: john smith
Number: 123-432-1238
Would you like to search for another name? y or n
y
Enter a first and last name: jane doe
Sorry, that name is not in our database.
Make sure spelling is correct.
Would you like to search for another name? y or n
y
Enter a first and last name: bob bobbert
Sorry, that name is not in our database.
Make sure spelling is correct.
Would you like to search for another name? y or n
Last edited on
Topic archived. No new replies allowed.