Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//function prototype
double computations(double, double, double, double, double, double);
int main()
{
//declare variables
string city;
double groceries, housing, utilities, transportation, health, misc, index;
int flag;
//max is set to 0 since calculated index will always be greater than 0
double max;
//min is set to 100
double min;
//variable names for max/min of city
string mincity, maxcity;
//declare input/output file names
ifstream inFile;
ofstream outputFile;
//open input file
inFile.open("costIndex.txt");
//open output file
outputFile.open("index.txt");
//open file; if fails exit program
if(inFile.fail())
{
cout << "File does not exist" << endl;
exit(100);
}
cout << setprecision(1) << fixed;
while (!inFile.eof())
{
//read whole line containing city location
getline(inFile, city);
//input values of cost index into separate variables
inFile >> groceries >> housing >> utilities >> transportation >> health >> misc;
//calculate the index cost for each city using computations function
index = computations(groceries, housing, utilities, transportation, health, misc);
//pause after every calculation
string tempstr; getline(inFile, tempstr);
//output result of city and calculated index to the output file
outputFile << left << setw(40) << city << setprecision(1) << fixed << right << setw(10) << index << endl;
if (flag = 0)
max = index;
min = index;
{
//calculate max index
if (index > max)
{
max = index;
maxcity = city;
}
//calculate min index
else if (index <= min)
{
min = index;
mincity = city;
}
flag = 1;
}
}
//output min and max index into the output file along with their city
outputFile << left << setw(40) << maxcity << right << setw(10) << max << endl;
outputFile << left << setw(40) << mincity << right << setw(10) << min << endl;
//close file - done
outputFile.close();
//output min and max index
cout << maxcity << endl;
cout << max << endl;
cout << mincity << endl;
cout << min << endl;
cout << "\nWon Kim\n" << "[email protected]\n" << "Lab #6";
}
double computations(double groc, double house, double util, double transp, double health, double misc)
{
/* Pre: groc = groceries cost
house - housing cost
util = utilities cost
transp = transportation cost
health = health cost
misc = miscellaneous goods & services cost
Post: calculation of composite index
Purpose: Calculate the composite index from the various cost for each name and state of a city
*/
double total;
//calculate total = index
total = 0.13 * groc + 0.29 * house + 0.10 * util + 0.12 * transp + 0.12 * health + 0.24 * misc;
return total;
}
So my professor recommended me to use flags to calculate the max and min. I initialized flag = 0 which is false. It runs the if statements initializing max/min to the value of the first index value, then comparing it to the previous when once the while loop cycles through. The max value I get is what I expect it to be but the min value is not. In fact the min value returns basically the last two lines of the input file I am grabbing the data from. I'm not sure why my minimum statement here is not working correctly. Anyone got any ideas?