writing array contents to file

Not exactly sure how to ask for help on this program. I've read in data from a file and displayed it to the screen as required. Made the calculations and now I'm supposed to write the data for "Trip Number" and "final cost" to 2-parallel arrays. But here is the kicker...I'm supposed to write it in reverse order. The output file has some crazy numbers in it. Can someone please help or point me in the right direction? Thank you..

Here is what I have thus far

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
 #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
int main()
{

//create two arrays
const int ARRAY_SIZE = 200; //array size of 100 elements
int nbrOfTrips[ARRAY_SIZE]; //array declaration of 100 elements
int counter; //For loop counter variable

double totalCost[ARRAY_SIZE]; //array declaration for cost of trips


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

//Variables
int tripNbr = 0;
double fuelCost = 0;
double fuelTotal = 0;
double wasteDisp = 0;
double misCost = 0;
double finalCost = 0.0;

cout<<"Welcome to Joe Snuffy 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);
	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;

	for(counter = 0; counter < ARRAY_SIZE; counter++) //loop to write data into array
	{
		nbrOfTrips[counter] = counter; // array 1
		totalCost[counter] = counter; // array 2
	}
	
	fileSave.open("TripCost.txt"); //open output file

	for(counter = 0; counter < ARRAY_SIZE; counter++) //use for loop to output to file
	{
		fileSave << nbrOfTrips[counter]<<totalCost[counter]<<endl;
	}
	fileSave.close();
}
system("Pause");
return 0; 
Last edited on
const int ARRAY_SIZE = 200; //array size of 100 elements
Here either the comment lies, or the declaration is wrong. Decide.

1
2
3
4
	for(counter = 0; counter < ARRAY_SIZE; counter++) //use for loop to output to file
	{
		fileSave << nbrOfTrips[counter]<<totalCost[counter]<<endl;
	}

This doesn't iterate the contents of the arrays in reverse, and you also do not put a whitespace between what you output so that may be why you're getting "crazy numbers".

1
2
3
4
5
6
	for(counter = 0; counter < ARRAY_SIZE; counter++) //use for loop to output to file
	{
		fileSave << nbrOfTrips[ARRAY_SIZE - 1 - counter];
		fileSave << '\t';
		fileSave << totalCost[ARRAY_SIZE -1 - counter]<<endl;
	}

Last edited on
My mistake...yes the array size will be up to 100. At least the instructions said "Assume the company has up to 100 trips." I should have given better instructions what the output would be as well. The input file looked like this.

50 //for trip number
50.00 //cost of gas
125.00 //cost of waste
250.00 //misc costs
75 //next trip number etc...etc..with the cost.

Now that I've read in the data, made the calculation to discount the fuel and output to screen I'm supposed to output to the file the:

trip number and final cost in reverse order so it would look something like this:

75 //trip number
550.00 //final cost
50 //trip number
450.00 //final cost of that trip.

I'm pretty lost on this exercise. Why can't professors take it slower and teach us the complex material? 7-days to do these, work and other classes is ridiculous. Everyone has to work now and school is not the same. Any help is greatly appreciated.

thanks,
Topic archived. No new replies allowed.