0

Okay so I have this project I am working on right now. The program reads in a file, that could have any number of lines to it, and each item in the file is a different type, like so:

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

The first number is the product number that is a type double, the second is a type string, then float of price of each and then int of numbers sold. The program reads these in then sorts them and outputs the highest selling and lowest selling items.

I have been working on this for a week and this is what I have

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <algorithm>

using namespace std;

struct Sales
{
  string prodName;
  double proNum;
  string price;
  string sold;
};

void sortArray(struct database, int);   //Function prototypes
void sortString(string[], int);
void showArray(struct database[], int);
bool sales_sorter(Sales const& lhs, Sales const& rhs);

int main()
{
  ifstream fin;

  fin.open("sales.txt");

  if (fin.fail())
   {
      cout << "Failed to open file" << endl;
   }


  vector<Sales> database(5);
  string line;

  int i = 0;
  while (!fin.eof())
  {
    for (int j = 0; j < 5; j++) 
    {
        if (j == 0) // name
        {
            fin >>  database[i].proNum;
        }
        else if (j == 1) // 
        {
            fin >> database[i].prodName;
        }
        else if (j == 2)
        {
            fin >> database[i].price;
        }
        else if (j == 3)
        {
            fin >> database[i].sold;
        }
    }
    i++; //move to next item
  }
}

std::sort(sales.begin(), sales.end(), &sales_sorter);

cout << &sales_sorter;


/* for (int x = 0; x < 5; x++)   //Just used to make sure the array is working 
{
    cout << database[x].proNum << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].prodName << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].price << endl;
}

for (int x = 0; x < 5; x++)
{
    cout << database[x].sold << endl;
} */
system("pause");


   return 0;
}

void sortArray(double database[], int)
{
  bool swap;
  int temp;

  do
  {
    swap = false;
    for (int count = 0; count < (5 - 1); count++)
    {
        if (database[count] > database[count + 1])
        {
            temp = database[count];
            database[count] = database[count + 1];
            database[count + 1] = temp;
            swap = true;
        }
    }
  } while (swap);
}


void showArray(double database[], int)
{
  for (int count = 0; count < 5; count++)
    cout << database[count] << " ";
  cout << endl;
}

bool sales_sorter(Sales const& lhs, Sales const& rhs)
{
   if (lhs.prodName != rhs.prodName)
    return lhs.prodName < rhs.prodName;
   if (lhs.proNum != rhs.proNum)
    return lhs.proNum < rhs.proNum;
   if (lhs.price != rhs.price)
    return lhs.price < rhs.price;
   return lhs.sold < rhs.sold;
 }

Now I got the sorting idea from this thread but my sorting bool is throwing this error: "Error expected a ";" but there is no where to put it without breaking things, can someone please help me figure out how to sort this. I have gone through so many different threads but all of these items need to be sorted and I can't seem to find anything about data structures that point to an array!

Destry
  • 228
  • 1
  • 12

1 Answers1

3

There are at least a couple of problems with your code. First, you cannot define functions within other functions so you should take your sales_sorter function out of main. Second, you have a leak - you allocate your database but you never deallocate it. I'd replace that with either std::array or std::vector. For example instead of

 Sales *database = new Sales[5];

you will have

 std::array<Sales, 5> database;

or

 std::vector<Sales> database(5);

Then you need to pass your container to the sort function:

 std::sort(database.begin(), database.end(), &sales_sorter);

This should get you started.

Rostislav
  • 3,857
  • 18
  • 30
  • Hey thanks for your help! This helped alot, however I am getting an error that says `'sales_sorter': undeclared identifier`. Do you know why it might say this? I have updated my code above to reflect what you have said – Destry Oct 10 '15 at 16:58
  • @DestryAmiott Well, you didn't forward declare it by adding the prototype before main. – Rostislav Oct 10 '15 at 17:06
  • Thanks I didn't know you could declare a bool prototype like you could with `void`. Thank you for your help, I will mark this as correct, but I am still getting errors so I will try to figure it out. It is giving me an odd number that isn't in the file, but thank you for your help – Destry Oct 10 '15 at 17:31