Hello,
This is has become a nightmare. I wrote a program that used two fixed-size arrays to read information from a text file that had a product number first and a price second. It was to use a linear search to find the product number the user entered from the keyboard to display that product number and corresponding price, (option a) or display the entire list (option b). Everything worked fine.
Then I got ambitious and decided I wanted to use vectors instead of arrays so they could grow as inventory does without changing code. That is where I am suffering. I am confusing myself when I work through the algorithm. It seems that everything should work the same with just minor alterations, but that isn't the case, or so it has become with the mess I have created.
Any help on telling me where I am going wrong would be huge. I just may go back to the fixed-size array and call it quits. I am running out of time for this class.
Code:#include <iostream> #include <fstream> #include <string> #include <cassert> #include <vector> using namespace std; int main() { // menu for user const string MENU = "\nPlease select your inventory search option:" "\n For a search by product number select A" "\n For a list of the entire product line select B." "\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("Inventory.txt"); assert(readInFile.is_open()); // item number from inventory list vector<int> productNumber; vector<double> productPrice; int n = 0; while (readInFile >> productNumber[n] >> productPrice[n]) { if (readInFile.eof()) break; n++; } for(;;) { char userChoice; cin >> userChoice; switch (userChoice) { case 'A': case 'a': cout << "\nEnter the product number: "; int pNumber; cin >> pNumber; for (int i = 0; i < productNumber.size(); i++) { int itemNum = productNumber[i]; if(itemNum == pNumber) cout << productNumber[i] << productPrice[i] << "\n"; } break; case 'B': case 'b': cout << "\nCurrent Inventory and Prices:\n\n"; for (int i = 0; i < productNumber.size(); i++) cout << "\t" << productNumber[i]<< productPrice[i] << "\n"; break; default: cout << "\nInvalid selection. Please try again." << MENU; } break; } readInFile.close(); cout << "\n"; return 0; }