Novice programmer with parallel arrays

Novice programmer with great respect to all programmers. My hair has disappeared, and sometimes it seems to me that I am spared attempts to solve these problems. Any Anyhoot assignment allows me to read data from a TXT file that I made. Perform the calculation and display. Reading data has more variables than what I have to write to the output file. So I read the data, and now I need to read in tripNumber and FinalCost in two different arrays, and then write the data in the "Reverse" to the file. I have most of this, but I'm stuck in a couple of spots that should be clear in my code. Understand that everyone has their own problems, and this is not a sad story. I work 60 hours a week, and I'm trying to get a diploma. Thank you for any help or advice to facilitate understanding of this complex skill.

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

using namespace std;
int main()
{

//create two arrays
const int ARRAY_SIZE = 100; //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;
double fuelCost = 0;
double fuelTotal = 0;
double wasteDisp = 0;
double misCost = 0;

int counter = 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 My 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;

//Write trip number and final cost to the 2 parallel arrays...not sure how to
//to do this.


//open output file
fileSave.open("TripCost.txt");

//for loops to output data to file
for(counter = 0; counter < ARRAY_SIZE; counter++)
{
fileSave<< nbrOfTrip[counter]<<endl;
fileSave<< totalCost[counter]<<endl;
}
}
system("Pause");
return 0;
}
+4
1

. " while " .

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;

    //Write trip number and final cost to the 2 parallel arrays...not sure how to
    //to do this.

}

. , , .

-, . , , . "numberOfTrips".

int numberOfTrips = 0;
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;

    //Write trip number and final cost to the 2 parallel arrays
    nbrOfTrip[numberOfTrips] = tripNbr;
    totalCost[numberOfTrips] = finalCost;
    ++numberOfTrips;
}

, , , , . , 'numberOfTrips' .

//for loops to output data to file
for(counter = 0; counter < numberOfTrips; counter++)
{
    fileSave<< nbrOfTrip[counter]<<endl;
    fileSave<< totalCost[counter]<<endl;
}
+4

All Articles