Parallel Arrays&Files as input/output

closed account (SwAfjE8b)
Hey everyone! So I've mostly figured out this program and everything compiles that I have so far but there are two parts that I am unable to figure out which is the ouput of fileSave (below)
TripCost.txt should look like this: //reverse order
222
580.00
111
340.00
and the second issue I am having is calculating and outputting the totals (below is what the output is suppose to look like)
Welcome to Jim Vandergriff’s Space Travel Company //your name
Trip No Fuel Waste Misc Discount Fuel Final Cost
111 100.00 200.00 50.00 90.00 340.00
222 200.00 300.00 100.00 180.00 580.00
Totals 300.00 500.00 150.00 270.00 920.00

//-------------------------------------------------------------
// Assignment 11
// Purpose: Using parallel arrays and files as input and output
//-------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;
int main()
{

const int ARRAY_SIZE = 200; // array size of 100 elements

ifstream fileIn; // create file object
ofstream fileSave; // create new output file
fileIn.open("TripInput.txt"); // read in file

// Variables to hold data from the file
int tripNbr = 0;
int num0 = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
double totalFuel = 0;
double totalWaste = 0;
double totalMisc = 0;
double totalDisc = 0;
double totalFC = 0;
double fuelCost = 0;
double fuelTotal = 0;
double wasteDisp = 0;
double misCost = 0;

int counter = 0;
int numberOfTrips = 0;

int nbrOfTrip[ARRAY_SIZE];
double totalCost[ARRAY_SIZE];

for(counter = 0; counter < ARRAY_SIZE; counter++)
{
nbrOfTrip[counter] = 0;
totalCost[counter] = 0;
}

cout << "Welcome to Mikayla Webber's Space Travel Company" << endl;
cout << endl;
cout << "Trip No" << setw(10) << "Fuel" << setw(10) << "Waste" << setw(10) << "Misc" << setw(15)
<< "Discount Fuel" << setw(15) << "Final Cost" << endl;

if(fileIn.fail()) // test to see if file opened
{
cout << "File did not open." << endl;
}

while(fileIn >> tripNbr >> fuelCost >> wasteDisp >> misCost) // while loop to read in data from file
{
fuelTotal = fuelCost - (fuelCost * .10);
double finalCost = fuelTotal + wasteDisp + misCost;
cout << tripNbr << setprecision(2) << fixed << setw(14) << fuelCost << setw(10) << wasteDisp
<< setw(10) << misCost << setw(15) << fuelTotal << setw(15) << finalCost << endl;
nbrOfTrip[numberOfTrips] = tripNbr;
totalCost[numberOfTrips] = finalCost;
++numberOfTrips;
totalFuel+=fuelTotal;
totalWaste+=wasteDisp;
totalMisc+=misCost;
totalDisc+=fuelTotal;
totalFC += finalCost;
}

fileSave.open("TripCost.txt");

for (counter = numberOfTrips - 1; counter >= 0; counter--) // for loops to output data to file
{
fileSave<< nbrOfTrip[counter] << endl;
fileSave<< totalCost[counter] << endl;
}
system ("PAUSE");
return 0;
}
[/code]
Topic archived. No new replies allowed.