Issue with with structures and sorting

I am trying to multiply members of a structure in my selection sort. For some reason when I do that the program is outputting incorrectly.

The problem is coming from line 63 in the selection sort but I do not know why or how to correct it.

Here is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

const int MAX_ITEMS = 100;


struct prodType
{
	int prodNum;
	string prodName;
	double prodPrice;
	int numSold;
};

void getMenuFromFile(ifstream& infile, prodType sales[])
{
    string strLine="";
	string strProdNum ="";
	string strProdName = "";
	string strProdPrice = "";
    string strNumSold="";
    int LastEmptyPos;
    int j=0;

    while ( getline(infile,strLine) && (j < MAX_ITEMS))
    {
        for (int i=0; i < strLine.size(); i++)
            if (strLine[i]==' ') LastEmptyPos = i;

		strProdNum ="";
		strProdNum = strProdNum.append(strLine);						
		stringstream(strProdNum) >> sales[j].prodNum;

		strProdName ="";
		strProdName = strProdName.append(strLine,LastEmptyPos-20);		
		stringstream(strProdName) >> sales[j].prodName;
		
		strProdPrice ="";
		strProdPrice = strProdPrice.append(strLine, LastEmptyPos-8);	
		stringstream(strProdPrice) >> sales[j].prodPrice;
		

		strNumSold ="";
		strNumSold =strNumSold.append(strLine,LastEmptyPos+1,strLine.size()-LastEmptyPos); 
        stringstream (strNumSold) >> sales[j].numSold;

        j++;
    }
}

void selectionSort(prodType sales[], int MAX_ITEMS)
{
	int startScan,
		minIndex;
	for (startScan = 0; startScan < (MAX_ITEMS - 1); startScan++)
	{
		minIndex = startScan;
		for (int index = startScan; index < MAX_ITEMS; index++)
		{
			if (sales[index].numSold * sales[index].prodPrice > sales[minIndex].numSold * sales[minIndex].prodPrice)
				minIndex = index;
		}
		if (minIndex != startScan)
		{
			prodType temp = sales[minIndex];
			sales[minIndex] = sales[startScan];
			sales[startScan] = temp;
		}
	}
}


void printStructMembers(prodType sales[])
	{
		cout << "The top selling product is " << sales[0].prodName << " with total sales of $" << sales[0].prodPrice * sales[0].numSold << endl;
		cout << "The second top selling product is " << sales[1].prodName << " with total sales of $" << sales[1].prodPrice * sales[1].numSold << endl;
	}



int main()
{
	string strline;
	prodType sales[MAX_ITEMS];
	ifstream inFile("sales.txt");


	if (inFile.is_open())
	{
		getMenuFromFile(inFile, sales);
	}

	else
	{
		cout << "Cannot open the input file." << endl;
	}

    inFile.close();

	selectionSort(sales, MAX_ITEMS);

    printStructMembers (sales);

    return 0;
}
Last edited on
Ok,
You were probably getting garbage values, right?
The reason is that on line 18 you wrote:
 
void getMenuFromFile(ifstream& infile, prodType sales[])

And you were using the "array" that was passed to the function like any other array. What happens when you pass an array to a function is that in the function you get a pointer to the first element.
You also need to pass the function how many elements are in the array or else you won't know how many there are and overshoot the end of the array.
So, the solution lies in using a for loop inside the function and using pointer arithmetic to initialize a new array of prodTypes one by one. Assuming you add a parameter to the function called arlen for the number of elements, you would do it like this:
1
2
3
4
5
prodType prod[arlen];
for(int i = 0; i < arlen; i++)
{
    prod[i] = *(sales + i);
}


That should fix your problem.
Good luck!
Last edited on
I'm sorry but your suggestion was a little hard to follow. I'm not exactly sure where I should add this code. I have tried it but it says arlen must have a constant value.
Can you post the file sales.txt. I guess the problem is more in loading the data from the file. After the file has loaded print it on the screen to see if you got the correct values.

1
2
3
4
5
6
7
8
9
if (inFile.is_open())
{
  getMenuFromFile(inFile, sales);
}
else
{
  cout << "Cannot open the input file." << endl;
}
inFile.close(); 

if the file can't be opend then there is no point to continue the program. Use exit(EXIT_FAILURE) to finish, also if it is not open when then close it?

@theturk1234,
You also need to pass the function how many elements are in the array or else you won't know how many there are and overshoot the end of the array.

It's not neccessary here since he uses the global const MAX_ITEMS.
Topic archived. No new replies allowed.