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!
#include<iostream>
#include<fstream>
#include<string>
#include<deque>
#include<conio.h>
constchar 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
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.
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?
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.
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.
#include<iostream>
#include<fstream>
#include<string>
constchar 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: ___