Can someone help me to make my program sort according to the average gallons used per car I keep geting 30 - 40 error when i try to compile it. Also
my files do exist.

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int MAXCARS = 12;
        float avgallons = 0;
		int numel = 0;
        float totalmiles, totalgallons;

struct Car
{
int number, miles;
float gallons;
float averagegallons;
}Cars;

Car Cars[MAXCARS];


void getdata(int, int, float);
void processdata(int, float);
void sort(float);
void putdata(int, float);

ifstream HopeData;
ofstream MikeData;

void main()
{

HopeData.open("gdata.dat");
MikeData.open("pdata");

if(HopeData.fail())
        {
        cout << "\n\nFile not successfully opened\n\n";
       }
cout << "\n\nFile successfully opened\n\n";

MikeData << "\n\n              Car Report" << endl;
MikeData << "Number       Average Per Car" << endl;
cout << setiosflags(ios::showpoint);
                cout << setiosflags(ios::fixed);
                cout << setprecision(2);

getdata(Cars.number, Cars.miles, Cars.gallons);
putdata(Cars.number, Cars.averagegallons);	

MikeData << "\n\nAverage Gallons Used by All Cars is " << avgallons << endl;
}

void getdata(int number, int miles, float gallons)
{
        while(HopeData.peek() != EOF)
        {
HopeData >> Cars.number >> Cars.miles >> Cars.gallons;
HopeData.ignore(80,'\n');

processdata(Cars.miles, Cars.gallons);
        }
}

void processdata(int miles, float gallons)
{
Cars.averagegallons = Cars.miles / Cars.gallons;
totalmiles = totalmiles + Cars.miles;
totalgallons = totalgallons + Cars.gallons;
avgallons = totalmiles / totalgallons;
numels++;
sort(Cars.averagegallons);
}

void sort(float averagegallons, int numels)
{
int i,j,min,minidx,temp,moves=0;

for(i=0;i<numels;i++)
  {
min = Cars[i];
minidx = i;
 	for(j=i+1;j<numels;j++)
  	{
		if(Cars[j].averagegallons<Cars[i].averagegallons)
		{
		min = Cars[j];
		minidx=j;
		}
		if(min<Cars[i].averagegallons)
		{
       temp=Cars[i];
       Cars[i]=min;
       Cars[minidx]=temp;
	   	}
	}
}

}

void putdata(int number, float averagegallons)
{
        while(HopeData.peek() != EOF)
        {
MikeData << Cars[].number << "\t     " << Cars[].averagegallons << endl;
        }
}

Only define Cars once.

struct Car
{
int number, miles;
float gallons;
float averagegallons;
}Cars;

Car Cars[MAXCARS];

Then figure out which way to use them.

getdata(Cars.number, Cars.miles, Cars.gallons);
putdata(Cars.number, Cars.averagegallons);
This article has been dead for over six months. Start a new discussion instead.