I am trying to sort an input file by a specific value (price), then print the sorted list to an output file.
Example input text (how it it formatted in my code to print to the screen. I would like to sort by price and save to an output file.
*Must use constructors* ?? All of my code right now is running using structs to store data variables from each text file.
I would like to use one simple function to print all of the sections to one file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void sequencedOrdersFile ()
{
ofstream outData;
outData.open("SequencedOrders.txt");
///Sort orders by price (high to low)
for ()
{
...
}
///Print to outfile
outData << ///sorted information for all items
///Close outfile
outData.close();
}
...And i need to change true to yes and false to no...
bool myCompare (book & a, book & b)
{ return a.price < b.price;
}
std::vector<book> bookinv;
// For each book
bookinv.push_back (myBook);
sort (bookinv.begin(), bookinv.end(); myCompare);
> AbstractionAnon: Store the books in std::vector
+1
This is not standard C++ (though it is a non-standard dialect known as gnu++)
1 2 3 4 5
ifstream inputB;
inputB.open("Book.txt");
int bookInventory;
inputB >> bookInventory; ///read first line to set array lengyh
book booksArray[bookInventory]; // *** error: bookInventory is not a constant
Use a vector by default when the size is not a constant known at compile-time.
We can sort that as easily as we could sort an array.
:) I don't understand why c-style arrays are taught before std::vectors, or c-style strings before std::string, or raw pointers...... Vectors are as easy to use (easier in some cases) than c-style arrays!