search for last name in text file, then output the whole line in the console.

Below is the code snippet that I need some help trying to figure out why my output is not what it should be. All commented out lines are lines used for other functions in my full program. I have trolled through these forums, as well as many others like CodeReview and StackExchange and nothing really seems to help my problem. By the way, this is a homework assignment. Anyway, the code below will compile and run. However when calling to the searchFile function and typing in the last name of the user I want to display their entire information(i.e Last name, First name, phone# and DOB) in the console, the program will always output <User> not found. I don't exactly know what is going on that is dropping us from the loop, and displaying the cout statement outside the while loop. Any help will be greatly appreciated!

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<fstream>
#include<string>
#include<deque>
#include<conio.h>

const char FILE_PATH[] = "DataFile.txt";

using std::cin;
using std::cout;
using std::ifstream;
using std::ofstream;
using std::ios;
using std::endl;
using std::string;
using std::fstream;
using std::deque;

//void DataRead(void);
void mainMenu(void);
//void editInfo(void);
//void addInfo(void);
void searchFile(); 
int main()
{

	mainMenu();


	return 0;
}
void mainMenu(void)
{
	int userChoice = 0;

	cout << "1. Find a person's information" << endl;
	cout << "2. Add a person to the database" << endl;
	cout << "3. Edit a person's information" << endl;
	cout << "4. Display all records to the screen" << endl;
	cout << "5. Quit" << endl;
	cout << "Choose Option: ";
	cin >> userChoice;

		switch (userChoice)
		{
		case 1:
			searchFile();
			break;

		case 2:
			//addInfo();
			break;

		case 3:
			//editInfo();
			break;

		case 4:
			//DataRead();
			break;

		case 5:
			break;
		
	}
}
void searchFile()
{
	ifstream dataIn(FILE_PATH, ios::in);

	string line;
	string word;

	cout << "Search by last name: ";
	cin >> word;

	dataIn.open("DataFile.txt");
	if (dataIn.is_open())
	{
		while (getline(dataIn, line))
		{
			if (line.find(word) != string::npos)
			{
				cout << "User found!" << endl;
				cout << "User credentials below: " << endl;
				cout << line << endl;
			}
		}
		cout << word << " not found" << endl;
	}
	mainMenu();
}



DATAFILE
1
2
3
4
5
Ammon Susan 503-840-9757 11-11-1953
Day Kristine 503-866-1420 06-03-1990
Hernandez Antonio 541-238-3623 07-26-1993
Walker Alex 541-880-3958 03-04-1991
Winsted Teigen 541-521-2336 04-19-1993
Last edited on
The line 89 is executed no matter what the lines 80-88 do.

Make a boolean flag and initialize it to false. If a line is found, then change the flag to true. That way you will know after the loop whether there was at least one matching line, or none.


PS. Remove line 91. You don't want that kind of recursion. Use a loop within mainMenu instead.
Last edited on
Thanks keskiverto! I went through and stepped into the program to see what's going on and have found that line is not stored with any information from the dataIn stream. So the while loop isn't even being initialized. It seems that the program isn't even calling getline, which correct me if I'm wrong, but that while loops is what is used to load the file into the line string and compare the values in it correct?
Last edited on
Okay, below is the appended function. I managed to get the function to finally get the output of the text file by appending the file to a temp string and moving it to the line string. However, now the program will output the whole text file, instead of just the line in the text file that I am searching for.

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
void searchFile()
{
	ifstream dataIn;//(FILE_PATH, ios::in);
	dataIn.open("DataFile.txt");

	string temp;
	string line;
	string word;

	cout << "Search by last name: ";
	cin >> word;

	if (dataIn.is_open())
	{
		while (!dataIn.eof())
		{
			getline(dataIn, temp);
			line.append(temp);
		}
		if (line.find(word) != string::npos)
		{

			cout << "User found!" << endl;
			cout << "User credentials below: " << endl;
			cout << line << endl;
		}
		else
		{
			cout << word << " not found" << endl;
		}

		dataIn.close();
	}
	mainMenu();
}
Finally got the code to work! I was having issues with the output when the last name was found. It would stay in the loop and output everything below the name, including the name. I fixed that by putting a break; in the while loop that exits when the name is found and will only put out that users info. Below is the working code as well as output in case anyone else is doing something similar and needs help:)

P.S. @keskiverto I am looking into that recursion issue you were talking about and implement that when I finish the last part of my program that I need to work on.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<fstream>
#include<string>

const char FILE_PATH[] = "DataFile.txt";

using std::cin;
using std::cout;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::string;

void DataRead(void);
void mainMenu(void);
void editInfo(void);
void addInfo(void);
void searchFile(); 

int main()
{

	mainMenu();


	return 0;
}
void mainMenu(void)
{
	int userChoice = 0;


	cout << "1. Find a person's information" << endl;
	cout << "2. Add a person to the database" << endl;
	cout << "3. Edit a person's information" << endl;
	cout << "4. Display all records to the screen" << endl;
	cout << "5. Quit" << endl;
	cout << "Choose Option: ";
	cin >> userChoice;

		switch (userChoice)
		{
		case 1:
			searchFile();
			break;

		case 2:
			//addInfo();
			break;

		case 3:
			//editInfo();
			break;

		case 4:
			//DataRead();
			break;

		case 5:
			break;
		
	}
}
void searchFile()
{
	ifstream dataIn;
	dataIn.open("DataFile.txt");

	string temp;
	string line;
	string word;

	cout << "Search by last name: ";
	cin >> word;

	if (dataIn.is_open())
	{
		while (getline(dataIn, temp))
		{
			line.append(temp);

			if (line.find(word) != string::npos)
			{

				cout << "\nUser found!" << endl;
				cout << "User credentials below: " << endl;
				cout << temp << endl;
				cout << endl;
				break;
			}
		}
		dataIn.close();
	}
	mainMenu();
}



Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1. Find a person's information
2. Add a person to the database
3. Edit a person's information
4. Display all records to the screen
5. Quit
Choose Option: 1
Search by last name: Ammon

User found!
User credentials below:
Ammon Susan 503-840-9757 11-11-1953

1. Find a person's information
2. Add a person to the database
3. Edit a person's information
4. Display all records to the screen
5. Quit
Choose Option: ___
Hate to tell you this but that is a bad way to do it.

What if you have 1000 people named Smith and you want to see them all ?

Topic archived. No new replies allowed.