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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
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;
int position = 0;
ofstream outFile;
outFile.open("a3.txt", ios::app);
cout << "Search Results: " << endl;
cout << endl;
outFile << "Search Results: " << endl;
outFile << endl;
for (int i = 0; i < SIZE; i++)
{
if (data[i].last_name == search_str)
{
location = true;
position = i;
}
}
if (location == true)
{
cout << "President: " << data[position].last_name
<< " Found" << endl;
cout << endl;
outFile << "President: " << data[position].last_name
<< " Found" << endl;
outFile << endl;
return 0;
}
else
{
cout << "President: " << search_str
<< " Not Found" << endl;
cout << endl;
outFile << "President: " << search_str
<< " Not Found" << endl;
outFile << endl;
return -1;
}
outFile.close();
}
//**********************************************************************
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 << "Name: " << setw(7) << right
<< data[i].first_name
<< " " << data[i].last_name << endl;
outFile << "Begin: " << setw(3) << right
<< data[i].begin_year << endl;
outFile << "End: " << setw(6) << right
<< data[i].end_year << endl;
outFile << "Party: " << setw(3) << right
<< data[i].party_affil << endl;
outFile << endl;
}
outFile.close();
}
|