Using Vectors (cont) - Sort Problem

This is a discussion on Using Vectors (cont) - Sort Problem within the C++ Programming forums, part of the General Programming Boards category; Hello again,
This is a continuation of the vector vs fixed-size array issue because I couldn't let it alone.
I ...

Using Vectors (cont) - Sort Problem

Hello again,
This is a continuation of the vector vs fixed-size array issue because I couldn't let it alone.

I got the code to work as it should using vectors instead of fixed-size arrays, but I didn't like the product numbers displayed out of order, for selection 'B.' So, I decided to try a sort. After thinking about it for a while, I figured out a way but there is an MDE error that I am getting and don't know how to fix it. I should say I am using Visual Studio 2003.

The sort happens and the display is correct, but the new text file isn't populated until I hit the yes button on the pop-up box.

I have the file sort function call at the very top of main() and the function outside of main at the end. I tried a similar sort all by itself in another project and everything worked fine, so I think that it is because it is inside of main and doesn't close before being used in main(). But, I am not sure.

Once the program ends. A Microsoft Development Environment box pops up and refering to the InventorySort.txt file, says:
"This file has been modified outside of the source editor. Do you want to reload it."

The file has been sorted, it just hasn't been written to the text file - the file is blank unless I say yes. What is happening, why, and how do I fix it?

Once the program ends. A Microsoft Development Environment box pops up and refering to the InventorySort.txt file, says:
"This file has been modified outside of the source editor. Do you want to reload it."

The file has been sorted, it just hasn't been written to the text file - the file is blank unless I say yes. What is happening, why, and how do I fix it?

Thanks for your input. You all have been great!

That's not a problem, it appears that you have the input file (InventorySort.txt) open in your IDE. When you modify a file outside of the control of the IDE, any file that you have open within the IDE, you'll get that message. It's just the IDE's way of letting you know that some other process has altered one of the files you have open and letting you make a decision on whether or not you want that file to be updated to match the current version. You can "fix it" by not having a window open for that file in your IDE.

A big problem is that your fileSort function does the wrong thing to your file. It reads the existing file, product number and price, but then after the sort, it writes the information with only the product number but no price. Even if you do this sort, it is going to be difficult to keep the price vector sorted in a way that keeps the correct price associated with the correct product number. Think about it, you sort the product number but not the prices, so that what was in productNumber[0] might now be in productNumber[4] (for example) but what about productPrice[0]? Does is get moved to productPrice[4] as well?

There are different things you can do:

1) Instead of storing your file information in 2 different vector<double>'s, you can store a struct that contains the product number and price into a single vector. For this to work, your program is also going to need a custom sorting function object that can be passed to the sort function since the default (operator<) is no longer going to work, or you'll need to overload the less-than operator. Now, when the vector is sorted, the price and product number would get sorted together.

That code also demonstrates operator overloading of operator< (to take care of the sorting requirements) and operator>> to be able to read a product object from the file, and operator<< to write the product object to a file. You can do without those last two if you do the reading and writing a bit differently.

2) You can try a different container and skip the sortFile function altogether. Just read the data into a map<int,double> container instead of a vector. Information in a map is automatically sorted according to the key, which you would want to be the product number in this case.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens