-
Selection Sort
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.
Code:
#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;
}
}
-
main()'s return type is int, not void.
Don't declare objects globally; do it locally and then pass them to functions as necessary.
Don't use EOF to control your input loops.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Instead, place the input function in the condition of the while loop:
Code:
while(myifstream >> x )
{
// do stuff with x
}
You'd make life a lot easier on yourself by using a std::vector rather than an array to hold your data.
Post your errors.
-
In addition to what joshdick said.
Code:
struct Car
{
int number, miles;
float gallons;
float averagegallons;
}Cars;
Car Cars[MAXCARS];
What is Cars? Is it an instance of a struct or is it an array of struct Car objects?
Code:
void processdata(
...
numels++;
...
}
There is no numels variable in scope anywhere at the time you get here. There is a global numel variable however.
Your method of passing values into pretty much all your functions is horribly wrong. You should be passing Car objects or your Car array and not ints and floats. You should also be passing them as references/pointers instead of by value. I'm at a loss as to where to begin my explaining.