Selectoin SOrt

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Selectoin SOrt

 
0
  #1
Apr 28th, 2005
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.


  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int MAXCARS = 12;
  7. float avgallons = 0;
  8. int numel = 0;
  9. float totalmiles, totalgallons;
  10.  
  11. struct Car
  12. {
  13. int number, miles;
  14. float gallons;
  15. float averagegallons;
  16. }Cars;
  17.  
  18. Car Cars[MAXCARS];
  19.  
  20.  
  21. void getdata(int, int, float);
  22. void processdata(int, float);
  23. void sort(float);
  24. void putdata(int, float);
  25.  
  26. ifstream HopeData;
  27. ofstream MikeData;
  28.  
  29. void main()
  30. {
  31.  
  32. HopeData.open("gdata.dat");
  33. MikeData.open("pdata");
  34.  
  35. if(HopeData.fail())
  36. {
  37. cout << "\n\nFile not successfully opened\n\n";
  38. }
  39. cout << "\n\nFile successfully opened\n\n";
  40.  
  41. MikeData << "\n\n Car Report" << endl;
  42. MikeData << "Number Average Per Car" << endl;
  43. cout << setiosflags(ios::showpoint);
  44. cout << setiosflags(ios::fixed);
  45. cout << setprecision(2);
  46.  
  47. getdata(Cars.number, Cars.miles, Cars.gallons);
  48. putdata(Cars.number, Cars.averagegallons);
  49.  
  50. MikeData << "\n\nAverage Gallons Used by All Cars is " << avgallons << endl;
  51. }
  52.  
  53. void getdata(int number, int miles, float gallons)
  54. {
  55. while(HopeData.peek() != EOF)
  56. {
  57. HopeData >> Cars.number >> Cars.miles >> Cars.gallons;
  58. HopeData.ignore(80,'\n');
  59.  
  60. processdata(Cars.miles, Cars.gallons);
  61. }
  62. }
  63.  
  64. void processdata(int miles, float gallons)
  65. {
  66. Cars.averagegallons = Cars.miles / Cars.gallons;
  67. totalmiles = totalmiles + Cars.miles;
  68. totalgallons = totalgallons + Cars.gallons;
  69. avgallons = totalmiles / totalgallons;
  70. numels++;
  71. sort(Cars.averagegallons);
  72. }
  73.  
  74. void sort(float averagegallons, int numels)
  75. {
  76. int i,j,min,minidx,temp,moves=0;
  77.  
  78. for(i=0;i<numels;i++)
  79. {
  80. min = Cars[i];
  81. minidx = i;
  82. for(j=i+1;j<numels;j++)
  83. {
  84. if(Cars[j].averagegallons<Cars[i].averagegallons)
  85. {
  86. min = Cars[j];
  87. minidx=j;
  88. }
  89. if(min<Cars[i].averagegallons)
  90. {
  91. temp=Cars[i];
  92. Cars[i]=min;
  93. Cars[minidx]=temp;
  94. }
  95. }
  96. }
  97.  
  98. }
  99.  
  100. void putdata(int number, float averagegallons)
  101. {
  102. while(HopeData.peek() != EOF)
  103. {
  104. MikeData << Cars[].number << "\t " << Cars[].averagegallons << endl;
  105. }
  106. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Selectoin SOrt

 
0
  #2
Apr 28th, 2005
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);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1519 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC