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.
Can anyone help?
This is my code:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
int fileSort();
int main()
{
fileSort();
// menu for user
const string MENU = "\nPlease select your inventory search option:"
"\n A - To search by product number"
"\n B - To display a list of the entire product line."
"\n Enter your selection ===> ";
// program banner
cout << "\nThis program queries the inventory list of Rinky Dooflingy Co."
<< "\nfor the price by item number or it displays the entire inventory"
<< "\nlist.\n"
<< MENU;
//read in inventory file
ifstream readInFile("InventorySort.txt");
assert(readInFile.is_open());
// vector and temporary variable declarations
vector<double> productNumber;
vector<double> productPrice;
double number;
double price;
// read data in from file to two temporary variable holders
while (readInFile >> number >> price)
{
// populate vectors with new variable data
productNumber.push_back(number);
productPrice.push_back(price);
}
// request menu option from user
char userChoice;
cin >> userChoice;
// display data as user requested
switch (userChoice)
{
// prompt user for item number
case 'A':
case 'a':
cout << "\nEnter the product number: ";
// get item number
double pNumber;
cin >> pNumber;
// search through productNumber data for item number match
for (int i = 0; i < productNumber.size(); i++)
if(pNumber == productNumber[i])
// display data if a match is found
cout << "\n\t" << "Item: " << setprecision(0) << productNumber[i]
<< " Price: $" << right << fixed << setprecision(2)
<< setw(8) << productPrice[i] << "\n";
break;
// display entire inventory list
case 'B':
case 'b':
cout << "\nCurrent Inventory and Prices:\n\n";
for (int i = 0; i < productNumber.size(); i++)
cout << "\t" << "Item: " << setprecision(0) << productNumber[i]
<< " Price: $" << right << fixed << setprecision(2)
<< setw(8) << productPrice[i] << "\n";
break;
default:
cout << "\nInvalid selection.";
}
readInFile.close();
cout << "\n";
return 0;
}
int fileSort()
{
ifstream inStream("Inventory.txt");
assert(inStream.is_open());
vector<string> inventoryVector;
string inventoryLine;
for (;;)
{
getline(inStream, inventoryLine);
if (inStream.eof()) break;
inventoryVector.push_back(inventoryLine);
}
inStream.close();
sort(inventoryVector.begin(), inventoryVector.end());
ofstream outStream("InventorySort.txt");
assert(outStream.is_open());
for (int i = 0; i < inventoryVector.size(); i++)
outStream << inventoryVector[i] << endl;
outStream.close();
return 0;
}
This is the instream text file "Inventory.txt":
Code:
101 45.00
303 3.00
707 102.00
302 13.45
555 7.97
999 500.00
345 76.99
325 3.33
769 22.45
111 2.76
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!