I'm trying to sort an array of objects and I keep running into a problem with this function. I'll post it so you can take a look. It passes an array of objects which has three variables, string itemCode, and int stockSold, int stockRemain. What I need to do is sort all the objects by the stockRemain variable. When the function runs it discards all of my stockSold and stockRemain data and just leaves the itemCode data.
void selectionSort(Item list[], int length )
{
int smallestPos;
Item temp;
// for every position in the array
for( int i=0; i<length; i++ )
{
// find the smallest element starting at that position
smallestPos = i;
for( int j=i+1; j<length; j++ )
{
if ( list[j].stockRemain < list[smallestPos].stockRemain )
{
// found a smaller one, remember it and keep going
smallestPos = j;
}
}
// see if we found something smaller than list[i]
if( list[i].stockRemain > list[smallestPos].stockRemain )
{
// we did find a smaller one, so swap with list[i]
temp.stockRemain = list[i].stockRemain;
list[i].stockRemain = list[smallestPos].stockRemain;
list[smallestPos].stockRemain = temp.stockRemain;
}
}
// done sorting the array
}
Take a look and see if you either need more of my code or if you can figure out what I need. Thanks.
What I mean is I have a function that goes through the object array and prints them out. When it prints there's nothing in the class members. I'm just gonna include all my code. I can't try to explain this without all my code.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace std;
#include "funcs.h"
int fileReader(string inventList, int i, Item inventory[])
{
ifstream inFile;
inFile.open(inventList);
int count;
// Loop through the input file.
while( !inFile.eof() )
{
inFile >> inventory[i].itemCode;
inFile >> inventory[i].stockSold;
inFile >> inventory[i].stockRemain;
cout << i << ": " << inventory[i].itemCode << endl;
cout << i << ": " << inventory[i].stockSold << endl;
cout << i << ": " << inventory[i].stockRemain << endl;
i++;
}
count = i;
// Close the input file
inFile.close();
return count;
}
void itemPrint(Item items[],int count)
{
cout << "Sorted Records: " << endl;
for(int i = 0; i<count;i++)
{
//cout << items[i].itemCode;
//cout << items[i].stockSold;
cout << items[i].stockRemain;
}
}
void selectionSort(Item list[], int length )
{
int smallestPos;
Item temp;
// for every position in the array
for( int i=0; i<length; i++ )
{
// find the smallest element starting at that position
smallestPos = i;
for( int j=i+1; j<length; j++ )
{
if ( list[j].stockRemain < list[smallestPos].stockRemain )
{
// found a smaller one, remember it and keep going
smallestPos = j;
}
}
// see if we found something smaller than list[i]
if( list[i].stockRemain > list[smallestPos].stockRemain )
{
// we did find a smaller one, so swap with list[i]
temp.stockRemain = list[i].stockRemain;
list[i].stockRemain = list[smallestPos].stockRemain;
list[smallestPos].stockRemain = temp.stockRemain;
}
}
// done sorting the array
for(int i = 0; i<length;i++)
{
cout << "Spot " << i << ": " << list[i].stockRemain << endl;
}
}
I realize I still have to swap the other members object data around. I just wanted to work on swapping one variable so that I know it can sort. Thats the sole mission I have right now.