0

03 Ribena 7 2.20

17 Coffee 76 1.50

24 Tea 21 1.10

39 Sprite 3 1.70

56 Cola 18 1.70

68 Orange 106 2.20

77 Lime 11 2.10

86 Grape 34 2.30

55 Chocolate 15 2.70

16 Frosty 20 2.20

65 Lemonade 21 1.90

55 Yoghurt 99 3.40

05 Punch 3 1.70

These records need to be sorted according to their ITEM NUMBER that is on the first column.

void record_initialize()
{
    vector<string> record;
    cout << "\n";
    record.push_back("03""\t\t""Ribena""\t\t""7""\t\t""2.20");
    record.push_back("17"  "\t\t""Coffee"  "\t\t""76"  "\t\t""1.50");
    record.push_back("24"  "\t\t""Tea"     "\t\t""21"   "\t\t""1.10");
    record.push_back("39"  "\t\t""Sprite"   "\t\t""3" "\t\t""1.70");
    record.push_back("56"   "\t\t""Cola"  "\t\t""18" "\t\t""1.70");
    record.push_back("68"  "\t\t""Orange"  "\t\t""106""\t\t""2.20");
    record.push_back("77"  "\t\t""Lime"    "\t\t""11" "\t\t""2.10");
    record.push_back("86"  "\t\t""Grape"     "\t\t""34" "\t\t""2.30");
    record.push_back("55" "\t\t" "Chocolate"   "\t""15" "\t\t""2.70");
    record.push_back("16"  "\t\t""Frosty"    "\t\t""20" "\t\t""2.20");
    record.push_back("55" "\t\t" "Lemonade"  "\t""21" "\t\t""1.90");
    record.push_back("55"  "\t\t""Yoghurt"   "\t\t""99" "\t\t""3.40");
    record.push_back("05"  "\t\t""Punch"     "\t\t""3"  "\t\t""1.70");
    cout << "\n";
    //sort(record.begin(), record.end());
    ofstream output_file("Drinks.txt");
    ostream_iterator<string> output_iterator(output_file, "\n");
    copy(record.begin(), record.end(), output_iterator);
}
void record_add()
{
    DrinkRecord d;
    cout << "\n";
    cout << "Please enter the item no.: ";
    cin >> d.no;
    cout << "\n";
    cout << "Please enter name of drink: ";
    cin >> d.name;
    cout << "\n";
    cout << "Please enter the quantity: ";
    cin >> d.quantity;
    cout << "\n";
    cout << "Please enter the unit price: ";
    cin >> d.price;
    cout << "\n";
    ofstream myfile;
    myfile.open("Drinks.txt", ios::app | ios::out);
    cout << "\n";
    myfile << d.no << "\t\t" << d.name << "\t\t" << d.quantity << "\t\t" << d.price << endl;
    myfile.close();
}

void record_view()
{

    cout << "\n";
    cout << "ItemNo" << "\t\t" << "ItemName" << "\t" << "Quantity" << "\t" << "Unit Price" << endl;
    cout << "\n";
    string getcontent;
    ifstream openfile("Drinks.txt");
    if (openfile.is_open())
    {
        while (!openfile.eof())
        {
            getline(openfile, getcontent);
            cout << getcontent << endl;
        }
    }
}

I have managed to do that part. But I am now having problems to sort after I add a new item to the drinks inventory. My lecturer told me to read the txt file into a vector, then sort the vector and then show the results. I cant seem to get the right steps. Any help would be much appreciated. I tried something like this.

void read_File() //Read the file into the vector function definition
{

    vector<string> logs;
    string line;


    cout << "Testing loading of file." << endl;
    ifstream myfile("Drinks.txt");
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            getline(myfile, line);
            logs.push_back(line);
            sort(line.begin(), line.end());

        }
        myfile.close();
    }
    else{
        cout << "Unable to open file." << endl;
    }

}
xXx
  • 1
  • 2
  • You are not sorting the vector. You are sorting the letters of each line. Also please don't loop on `eof()`: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik Nov 22 '14 at 21:28

2 Answers2

0

It seems that you're sorting after reading each line. Note that the call to sort is inside the loop. It means that on every iteration (after each line you read) you call sort. If I understood your assignment correctly, you need to sort all the lines (logs) in the file. If so, then it would mean that you'll need to call sort only once. And instead of passing it the line as a parameter, you probably need to pass it the logs vector as a parameter, because you want to sort the logs.

Ilya Kogan
  • 20,368
  • 15
  • 78
  • 134
0

You have to sort it once after reading the whole file.
Also, sort(line.begin(), line.end()); is going to sort the string in your vector based on string comparison rules, but, you need to sort based on your first column.
You need to write your own custom comparator which reads the first column from two strings getting compared.

Since this looks like a class question, I am going to let you write the below comparator function.
Refer the link below for more help.

sort(line.begin(), line.end(), mycolumncomparator);

// Fill the below function correctly
bool mycolumncomparator(const std::string& i, const std:string& j) 
{ 
    // Read the first column from both strings
    // covert them into int using atoi
    // return (int1 < int2);
}

Please refer here: http://www.cplusplus.com/reference/algorithm/sort/

Rush
  • 476
  • 2
  • 11