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 140 141 142
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
double indexCalc(string, double, double, double, double, double, double);
//prototypes for functions that determine lowest and highest indexes.
double showHigh(double, double&);
double showLow(double, double&);
void output(string, string, double, double);
int main()
{
string line, lowest, highest;
double groceries, housing, utilities, transportation, health, miscellaneous, index, high = 0, low = 99999;
ifstream inFile;
ofstream outFile;
//File open test
inFile.open("costIndex.txt");
outFile.open ("index.txt");
if (inFile.fail())
{ cout << "No such file";
system("pause");
exit(100);
}
while (!inFile.eof())
{
//input from text file
getline (inFile, line);
inFile >> groceries;
inFile >> housing;
inFile >> utilities;
inFile >> transportation;
inFile >> health;
inFile >> miscellaneous;
inFile.ignore();
index = indexCalc(line, groceries, housing, utilities, transportation, health, miscellaneous);
if (index > high){
high = showHigh(index, high);
highest = line;
}
if (index < high && index < low)
{
low = showLow(index, low);
lowest = line;
}
outFile << setprecision(1) << fixed << "City name: " << line << endl;
outFile << "Groceries: " << groceries << endl;
outFile << "Housing: " << housing << endl;
outFile << "Utilities: " << utilities << endl;
outFile << "Transportation: " << transportation << endl;
outFile << "Health: " << health << endl;
outFile << "Miscellaneous: " << miscellaneous << endl;
outFile << "Index: " << fixed << index << endl;
}
outFile << setprecision(1) << fixed << "Town with the highest index: " << highest << endl;
outFile << "Highest index: " << high << endl;
outFile << "Town with the lowest index: " << lowest << endl;
outFile << "Lowest index: " << low << endl;
cout << setprecision(1) << fixed << "Town with the highest index: " << highest << endl;
cout << "Highest index: " << high << endl;
cout << "Town with the lowest index: " << lowest << endl;
cout << "Lowest index: " << low << endl;
inFile.close();
outFile.close();
return 0;
}
/*Pre:
Post : N/A
Purpose: Calculate city indexes.
*/
double indexCalc(string line, double groceries, double housing, double utilities, double transportation, double health, double miscellaneous)
{
double ind;
ind = (groceries * 0.13) + (housing * 0.29) + (utilities * 0.10) + (transportation * 0.12) + (health * 0.12) + (miscellaneous * 0.24);
return ind;
}
/*Pre: high reference.
Post: New High.
Purpose: Find the latest highest city index.
*/
double showHigh(double ind, double &high)
{
double newHigh;
if (ind > newHigh)
{
newHigh = ind;
return newHigh;
}
}
/*Pre: low reference.
Post: New low.
Purpose: Find the latest lowest city index.
*/
double showLow( double dex, double &low)
{
double newLow;
if (dex > newLow)
{
newLow = dex;
return newLow;
}
cout << "Bryan Kobashi" << endl;
}
|