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
|
#include <iostream>
#include <iomanip>
#include <cstring>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;
int searchName(string searchName, const string NAME_AR[], const int AR_SIZE, bool &isFound);
void inputArrays(ifstream& iFile, string nameAr[], int idsAr[], float balanceAr[], const int AR_Size);
int main()
{
const int AR_SIZE = 10;
ifstream iFile;
ofstream oFile;
string inFileName;
string outFileName;
string sName;
string nameAr[AR_SIZE];
int idsAr[AR_SIZE];
float balanceAr[AR_SIZE];
int nameIndex;
bool isFound = false;
///
cout << "What input file would you like to use? ";
getline(cin, inFileName);
iFile.open(inFileName.c_str());
cout << "What output file would you like to use? ";
getline(cin, outFileName);
oFile.open(outFileName.c_str());
inputArrays(iFile, nameAr,idsAr,balanceAr, AR_SIZE);
oFile << setw(9)<< left << "ID #" << setw(25) << "NAME" << setw(11)
<< right << "BALANCE DUE" << endl;
oFile << setw(9)<< left << "----" << setw(25)
<< "--------------------" << setw(11)
<< right << "-----------" << endl;
do{
cout << "\nWho do you want to search for (enter done to exit): ";
getline(cin, sName);
if(sName!= "done")
{
nameIndex = searchName(sName, nameAr, AR_SIZE, isFound);
if (isFound)
{
oFile << setw(9)<< left << idsAr[nameIndex] << setw(25) <<
nameAr[nameIndex] << setw(1) << "$" << right <<
setw(10) << setprecision(2) << fixed <<
balanceAr[nameIndex] << endl;
}
}
}while(sName != "done");
oFile << setw(20) << "Average Balance Due: $" ;
cout << endl << "Thank you for using my program.";
return 0;
}
void inputArrays(ifstream& iFile, string nameAr[], int idsAr[], float balanceAr[], const int AR_SIZE)
{
for (int index = 0; iFile && index < AR_SIZE; index++)
{
getline(iFile,nameAr[index]);
iFile >> idsAr[index];
iFile >> balanceAr[index];
iFile.ignore(1000, '\n');
}
}
int searchName(string sName, const string NAME_AR[], const int AR_SIZE, bool &isFound)
{
int searchIndex = 0;
for (int index = 0; index < AR_SIZE; index++)
{
if (sName == NAME_AR[index])
{
searchIndex = index;
isFound = true;
}
}
if (isFound)
{
cout << "Found.\n";
}
else
{
cout << sName << " was not found.\n";
}
return searchIndex;
}
}
|