Click here to Skip to main content
15,891,855 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i need to list out the items from the notepad in ascending or descending order.
how do i go about doing that. need some codes
ur help is appreciated

C++
int main(int argc, char *argv[]);
{
    std::ifstream output_file("stockdatabase.txt");
    std::string value;
    std::vector<std::string xmlns:std="#unknown"> getItemID;
    if (output_file)
    {
        while (!output_file.eof())
        {
            std::getline(output_file, value, '\n');
            getItemID.push_back(value);
            std::sort(getItemID.begin(), AscendingOrderSorter());
        }
        for (int x = 0; x < stockVectorTemp.size(); x++)
        {
            cout << "[" << x+1 << "]\t" << stockVectorTemp[x].getItemID()
            << "\t" << stockVectorTemp[x].getTotalQty()
            << "\t\t" << stockVectorTemp[x].getItemDesc() << endl;
        }
        for (int y =0; y>stockVectorTemp.size(); y--)
        {
            cout << "[" << y-1 << "]\t" << stockVectorTemp[y].getItemID()
            << "\t" << stockVectorTemp[y].getTotalQty()
            << "\t\t" << stockVectorTemp[y].getItemDesc() << endl;
        }
Posted
Updated 22-Oct-13 21:28pm
v3
Comments
Richard MacCutchan 21-Oct-13 4:05am    
Please don't post questions like this, no one is going to write your assignment for you.
Captain Price 22-Oct-13 5:32am    
Your Duplicate : http://www.codeproject.com/Questions/670436/need-help-with-codes-on-edit-stock Why are you asking the same question twice ?
Richard MacCutchan 23-Oct-13 3:29am    
Your code is incomplete.
Dark Darkling 23-Oct-13 4:36am    
No such overloading for std::sort. It's needed to add 'getItemID.end()' as the second parameter.
Richard MacCutchan 23-Oct-13 9:12am    
Sorry, what?

Just read all items from file to 'std::vector<std::string xmlns:std="#unknown">' and call 'std::sort' function to this vector.

P.S. You could use a predicate for 'std::sort' function to implement your own sorting logic.

Good luck!
 
Share this answer
 
Comments
Member 10334475 22-Oct-13 5:32am    
could u show me the coding? I get the concept but I duno how to begin
C++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>


struct AscendingOrderSorter
{
  bool operator()(const std::string& lhs, const std::string& rhs)
  {
    return lhs < rhs;
  }
};

struct DecsendingOrderSorter
{
  bool operator()(const std::string& lhs, const std::string& rhs)
  {
    return lhs > rhs;
  }
};

struct RowPrinter
{
  void operator()(const std::string& value)
  {
    std::cout << value << "\n";
  }
};

int main(int argc, char *argv[])
{
  std::ifstream input_file("D:\\123.txt");
  std::string value;
  std::vector<std::string> file_data;

  if (input_file)
  {
    while (!input_file.eof())
    {
      std::getline(input_file, value, '\n');
      file_data.push_back(value);
    }
  }
  std::cout << "Before sort: \n";
  std::sort(file_data.begin(), file_data.end(), AscendingOrderSorter());
  // now file_data is sorted.
  std::cout << "After sort: \n";
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());
}
 
Share this answer
 
v3
Comments
Dark Darkling 22-Oct-13 6:14am    
NOTES:
1. 'D:\\123.txt' is the input file, so you must change the path to your own.
2. Use AscendingOrderSorter or DecsendingOrderSorter functors for appropriate sorting.
Member 10334475 22-Oct-13 21:56pm    
I tried it. it has no error but doesn't output anything.
Dark Darkling 23-Oct-13 4:31am    
The Output is empty because you don't print anything.
For my example use improved version of code.
For your code you need to make some changes, specifically:
1. Don't call sort function inside loop (performance issue).
2. What are 'stockVectorTemp' variable and 'getItemID' function?
3. Maybe you need to add getItemID vector into stockVectorTemp?
Member 10334475 23-Oct-13 10:50am    
thanks. now it has the correct output.
but i have another problem.
the ascending and descending output is displayed on top of program.
it displays the output. then it runs 2 sets of mainmenu which i didnt call.
why is it happening
Dark Darkling 24-Oct-13 3:52am    
Could you please show us your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900