Мне нужно отсортировать структуру C ++ только с iostream и fstream, хотя, похоже, что-то идет не так (см. Ввод и вывод ниже).
void sortCarsByPrice(Cars carsArray[])
{
int index1;
int index2;
int index3;
int swap;
int yearTemp;
char makeTemp;
char modelTemp;
float priceTemp;
int availTemp;
do
{
swap = 0;
for (index1 = 0; index1 < 5; index1++)
{
if (carsArray[index1].price > carsArray[index1 + 1].price)
{
yearTemp = carsArray[index1].year;
carsArray[index1].year = carsArray[index1 + 1].year;
carsArray[index1 + 1].year = yearTemp;
for (index2 = 0; carsArray[index1].make[index2] != '\0' && carsArray[index1 + 1].make[index2] != '\0'; index2++)
{
makeTemp = carsArray[index1].make[index2];
carsArray[index1].make[index2] = carsArray[index1 + 1].make[index2];
carsArray[index1 + 1].make[index2] = makeTemp;
}
for (index3 = 0; carsArray[index1].model[index3] != '\0' && carsArray[index1 + 1].model[index3] != '\0'; index3++)
{
modelTemp = carsArray[index1].model[index3];
carsArray[index1].model[index3] = carsArray[index1 + 1].model[index3];
carsArray[index1 + 1].model[index3] = modelTemp;
}
priceTemp = carsArray[index1].price;
carsArray[index1].price = carsArray[index1 + 1].price;
carsArray[index1 + 1].price = priceTemp;
availTemp = carsArray[index1].available;
carsArray[index1].available = carsArray[index1 + 1].available;
carsArray[index1 + 1].available = availTemp;
swap += 1;
}
}
}
while (swap != 0);
cout << "SORTED!" << endl;
return;
}
input:
0 2014 Toyota Tacoma $115.12 0
1 2015 Ford Fusion $90.89 1
2 2009 Dodge Neon $45.25 0
3 2015 Ford F150 $112.83 1
4 2016 Subaru Outback $71.27 1
вывод: (после сортировки)
0 999984304 �yota Ծ0#on $-1.84172e+33 32566
1 2009 Dodg Neonma $45.25 0
2 2016 Subae Outb $71.27 1
3 2015 Ford Fusi $90.89 1
4 2015 Fordru F150U�� $112.83 1