Working on a project from class I got it mostly right at this point but I'm having trouble with search function. I want to search my array (which is pre-loaded from a file) for a specific name and just output the name and whether it was found or not found.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct President
{
string first_name;
string last_name;
int begin_year;
int end_year;
string party_affil;
};
void loadData(President data[], const int SIZE);
int search(President data[], const int SIZE, string search_str);
void outputData(President data[], const int SIZE);
//**********************************************************************
int main()
{
const int SIZE = 17;
President data[SIZE];
string search_str;
loadData(data, SIZE);
search(data, SIZE, search_str = "Van Buren");
search(data, SIZE, search_str = "Polk");
outputData(data, SIZE);
return 0;
}
//**********************************************************************
void loadData(President data[], const int SIZE)
{
fstream inFile;
inFile.open("prez_data.txt");
for (int i = 0; i < SIZE; i++)
{
data[i].first_name = "";
data[i].last_name = "";
data[i].begin_year = 0;
data[i].end_year = 0;
data[i].party_affil = "";
}
for (int i = 0; i < SIZE; i++)
{
getline(inFile, data[i].first_name);
getline(inFile, data[i].last_name);
inFile >> data[i].begin_year;
inFile >> data[i].end_year;
inFile.ignore();
getline(inFile, data[i].party_affil);
}
for (int i = 0; i < SIZE; i++)
{
cout << data[i].first_name << endl;
cout << data[i].last_name << endl;
cout << data[i].begin_year << endl;
cout << data[i].end_year << endl;
cout << data[i].party_affil << endl;
cout << endl;
}
}
//**********************************************************************
int search(President data[], const int SIZE, string search_str)
{
bool location = false;
ofstream outFile;
outFile.open("a3.txt", ios::app);
outFile << "Search Results: " << endl;
for (int i = 0; i < SIZE; i++)
{
if (data[i].last_name == search_str)
{
location = true;
outFile << "President: " << search_str
<< " Found" << endl;
}
else
{
location = false;
outFile << "President: " << search_str
<< " Not Found" << endl;
}
}
outFile.close();
return location;
}
//**********************************************************************
void outputData(President data[], const int SIZE)
{
ofstream outFile;
outFile.open("a3.txt", ios::app);
outFile << "*********************************************" << endl;
outFile << "President Listing: " << endl;
outFile << endl;
for (int i = 0; i < SIZE; i++)
{
outFile << "First Name: " << data[i].first_name
<< " " << data[i].last_name << endl;
outFile << "Entered Office: " << data[i].begin_year << endl;
outFile << "Left Office: " << data[i].end_year << endl;
outFile << "Party Affiliation: " << data[i].party_affil << endl;
outFile << endl;
}
outFile.close();
}
|
example output I'm looking for is:
Search Results:
President: Van Buren Not Found
President: Polk Not Found
What I'm getting is:
Search Results:
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
President: Van Buren Not Found
Search Results:
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
President: Polk Not Found
I know it is something simple that I'm just blanking on. Any help would be greatly appreciated.