array sort

I'll say thanks in advance for any who take the time to glance at this for me.

I've a very basic data import from my file: test2.dat. The import appears to work fine and does display the data correctly, but upon sorting, I'm not getting anything but an output in the original order. I had my prof look at it and he didn't see anything obvious (then again, his eyesight is poor and he has a ton of students to assist). All I'm hoping for is a hint, even if it's just to say I coded it wrong.

Here's the code followed by the dat file contents:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int y,x,kount=0;
string item_name[100],item_color[100],temp_in,temp_ic;
double item_weight[100],temp_iw,total_weight=0,av_weight=0;

cout<<"Data retrieval program PD(2011)\n\n";
cout<<"This program will retrieve component data from the file test2.dat,\n";
cout<<"display the pre-sorted data and then display the data sorted by weight\n";
cout<<"from smallest to largest. The program will also display the sum of all\n";
cout<<"item weights and the calculation of average weight per item.\n\n";

fstream gruppo;
gruppo.open("test2.dat", ios::in);
gruppo>>item_weight[kount]>>item_color[kount]>>item_name[kount];

while(!gruppo.eof())
{
setprecision(3);
cout<<"Item"<<(kount + 1)<<": "<<setw(15)<<item_name[kount]<<"\t"<<setw(15)<<item_color[kount]<<setw(8)<<item_weight[kount]<<" grams"<<"\t\n";
total_weight=total_weight+item_weight[kount];
kount++;
gruppo>>item_weight[kount]>>item_color[kount]>> item_name[kount];
}

gruppo.close();

cout<<"\nSorting NEW gruppo data by item weight:\n\n";
for(y=0;y<kount-1;y++)
{
for(x=0;x<kount;x++);
{
if(item_weight[x]>item_weight[x+1])
{
temp_iw = item_weight[x];
item_weight[x] = item_weight[x+1];
item_weight[x+1] = temp_iw;

temp_in = item_name[x];
item_name[x] = item_name[x+1];
item_name[x+1] = temp_in;

temp_ic = item_color[x];
item_color[x] = item_color[x+1];
item_color[x+1] = temp_ic;
}
}
}

for(x=0;x<kount;x++)
{
cout<<setw(15)<<item_name[x]<<"\t"<< setw(15)<<item_color[x]<<"\t"<< setw(8)<<item_weight[x]<<" grams\n";
}

cout<<"\n\nTotal weight of gruppo: \t"<<total_weight<<" grams\n";
av_weight=total_weight/(kount+1);
cout<<"Average weight per component: \t"<<setprecision(3)<<av_weight <<" grams\n";

cout<<"\n\nThank you for using my data retrieval program!"<<"\n";
cout<<"Press ENTER to continue...";
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

return EXIT_SUCCESS;
}

/* dat file contents:
72 carbon f_derailleur
155 carbon r_derailleur
272 carbon brake_calipers
680 black_carbon crankset
29.25 natural bottom_bracket
98 red headset
248 titanium saddle
250 aluminum seatpost
260 carbon handlebar
130 black stem
52.5 black bottle_cages
248 black pedals
10.45 steel cables
88 yellow tubes
3.12 graphite brake_pads
11 speckled bar_tape
10 red computer
1675 anthracite frame
24 red bottles
590 red f_wheel
800 red r_wheel
235 grey f_tire
235 grey r_tire
117 titaium cassette
239 black chain
Last edited on
Topic archived. No new replies allowed.