944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1636
  • C++ RSS
Apr 28th, 2005
0

Selectoin SOrt

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Apr 28th, 2005
0

Re: Selectoin SOrt

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);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Finding the mode value and its frequency!
Next Thread in C++ Forum Timeline: How to read end of line?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC