SOLVED

SOLVED :)
Last edited on
The error is at line 127,
 
    selectionSort(sales, MAX_ITEMS, prod);


The number of items which were read from the file is not 100, it is 5.

Change MAX_ITEMS to LineNum
 
    selectionSort(sales, LineNum, prod);


Some of the other code seems over-complex. For example you might read from the file and get the number of lines like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int getMenuFromFile(ifstream& infile, prodType sales[])
{
    string strLine = "";
    int j = 0;

    while (getline(infile, strLine))
    {
        istringstream ss(strLine);
        if ( ss >> sales[j].prodNum
                >> sales[j].prodName
                >> sales[j].prodPrice
                >> sales[j].numSold)
        {
            sales[j].prod =  sales[j].prodPrice * sales[j].numSold;
            j++;
        }
    }

    return j;
}

As a bonus, that also populates the prod field, removing the need for a separate table.
For context, this is the original code:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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;
    double prod;
};

int getLineNum()
{
    int number_of_lines = 0;
    string line;
    ifstream myfile("sales.txt");

    while (getline(myfile, line))
        ++number_of_lines;
    cout << "Number of lines in text file: " << number_of_lines << endl;

    return number_of_lines;
}

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


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

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

        strProdName = "";
        strProdName = strProdName.append(strLine, LastEmptyPos - 20);        //Store product name
        stringstream(strProdName) >> sales[j].prodName;

        strProdPrice = "";
        strProdPrice = strProdPrice.append(strLine, LastEmptyPos - 8);    //Store product price
        stringstream(strProdPrice) >> sales[j].prodPrice;


        strNumSold = "";
        strNumSold = strNumSold.append(strLine, LastEmptyPos + 1, strLine.size() - LastEmptyPos); //Store number of units sold
        stringstream(strNumSold) >> sales[j].numSold;

        j++;
    }
}

void makeProd(prodType sales[], double prod[], int LineNum)
{
    for (int i = 0; i < LineNum; i++)
    {
        prod[i] = sales[i].prodPrice * sales[i].numSold;
    };
}

void selectionSort(prodType sales[], int LineNum, double prod[])
{
    int i, j, first;
    for (i = LineNum - 1; i > 0; i--)
    {
        first = 0;
        for (j = 1; j <= i; j++)
        {
            if (prod[j] > prod[i])
                first = j;
        }
        prodType temp = sales[first];
        sales[first] = sales[i];
        sales[i] = temp;
    }

}

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



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

    int LineNum = getLineNum();

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

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

    inFile.close();

    makeProd(sales, prod, LineNum);

    selectionSort(sales, MAX_ITEMS, prod);

    printStructMembers (sales);

    return 0;
}

and the input file:
1002 Hammer 23.65 203
1024 Nails 6.95 400
1276 Screwdriver 13.95 251
1385 Elec_Drill 45.69 132
1462 Air_Filter 7.95 500

though the original code doesn't make any sense at all with that file, it requires a fixed-width layout a bit like this:
  1002 Hammer       23.65  203
  1024 Nails         6.95  400
  1276 Screwdriver  13.95  251
  1385 Elec_Drill   45.69  132
  1462 Air_Filter    7.95  500

Wading through all that took some time and effort. A word of thanks might have been nice, rather than just deleting everything as though it never happened.
Last edited on
Topic archived. No new replies allowed.