| | |
vector subscript out of range
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Solved Threads: 0
Hello, I'm almost done with this program, but when I want to sell an item (option 2) I get an error after I enter how many 'vector subscript out of range'. The program compiles correctly, but I cannot for the life of me figure this one out. Any Help?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> #include <fstream> #include <iomanip> using namespace std; void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice); void do_again(),usersChoice(char ans); void searchInventory(string searchInput); void printReport(); void printHeading(); void sellStock(string sellItem, int numSold); int showMenu(); int ans(); vector<int>itemID, pOrdered, pInStore, pSold; vector<double>manufPrice,sellingPrice; vector<string>itemName; int totalItems; double totalInv; unsigned int counter; int searchName; int sellName; int sellNum; char choice; string searchInput; string input; int noOfRows; int main() { ifstream incode; incode.open("itemInfo.txt"); if (!incode) { cout << "Can't open the input file" << endl; return 1; } getData(incode, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice); showMenu(); cin.get(); incode.close(); return 0; } void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice) { int sold = 0; string name; int id, ordered; double mPrice; double sPrice; char ch; while (infile >> id) { infile.get(ch); getline(infile, name); infile >> ordered >> mPrice >> sPrice; itemID.push_back(id); itemName.push_back(name); pOrdered.push_back(ordered); pInStore.push_back(ordered); pSold.push_back(sold); manufPrice.push_back(mPrice); sellingPrice.push_back(sPrice); } } void usersChoice(char ans) { int counter2 = 1; switch (choice)//roll thru options { case '1': //if they picked the number 1, its gonna go to this function, //thats pretty much how the whole thing works unsigned int counter; cout << "\n\n\t\tWhich item would you like to check the inventory for?\n\n"; for (counter = 0; counter < itemName.size(); counter++) { cout << "\t\t\t" << counter + 1 << ". " << itemName[counter] << "\n"; } cout << "\n\n\t\t\t Choice: "; cin >> searchName; //cout << "\n\n\t" << itemName[searchName - 1]; //input = ; searchInventory(itemName[searchName - 1]); break; case '2': // run sellStock function cout << "Which item would you like to sell?" << endl; for (counter = 0; counter < itemName.size(); counter++) { cout << "\t\t\t" << counter + 1 << ". " << itemName[counter] << "\n"; } cout << "\n\n\t\t\t Choice: "; cin >> sellName; cout << "\n\t\tHow many would you like to sell? "; cin >> numSold; sellStock(itemName[sellName - 1], numSold); break; case '3': // run print Report printHeading(); printReport(); break; case '4': // exit the program break; default://THEY'RE NOT FOLLOWING DIRECTIONS?!?! cout << "\t\tInvalid Input" << endl;//if directions aren't followed }//end switch }//end function int showMenu() { do { cout << "\n\n\t\tPlease choose from the following menu (1, 2, 3, or 4 to EXIT):\n"; cout << "\n\t\t (1) Check if an item is in inventory"; cout << "\n\t\t (2) Sell an item"; cout << "\n\t\t (3) Print a report\n\n"; cout << "\n\t\t (4) Exit\n\n\t\t\t Choice: "; cin >> choice; usersChoice(choice);//send that answer to usersChoice! }while(choice != '4'); return choice; } void searchInventory(string searchInput) { for (counter = 0; counter < itemName.size(); counter++) if(itemName[counter] == searchInput) cout << "\n\t\tThere are " << pInStore[counter] << " " << searchInput << "s in stock."; } void printHeading() { cout << "\n\n\t\t\t\tFriendly Hardware Store" << endl << endl; cout << "\tItemID\t" << "ItemName\t" << "pOrdered" << " pInStore" << " pSold" << " manufPrice" << " sellingPrice\n\n"; } void printReport () { unsigned int x; double totalInventory = 0.00; //Value of all available inventory int totalItems = 0; double itemInventory = 0.00; for (x = 0; x <= itemID.size() - 1; x++) { cout << "\t" << itemID[x] << "\t" << itemName[x] << "\t" << pOrdered[x] << "\t " << pInStore[x] << "\t " << pSold[x] << "\t " << "$" << manufPrice[x] << ".00" << "\t" << "$" << sellingPrice[x] << ".00\n\n"; itemInventory = static_cast<double>(pInStore[x]) * sellingPrice[x]; totalInventory = totalInventory + itemInventory; totalItems = totalItems + pInStore[x]; } cout << "Total Inventory: $" << totalInventory << endl; cout << "Total number of items in the store: " << totalItems << endl; } void sellStock(string sellItem, int numSold) { int totalItems = 0; double itemInventory = 0.00; double totalInventory = 0.00; for (counter = 0; counter < 6; counter++) if(sellItem == itemName[counter]) pInStore[counter] = pInStore[counter] - numSold; pSold[counter] = pSold[counter] + numSold; totalItems = totalItems + pInStore[counter]; totalItems = totalItems - numSold; totalInventory = totalInventory - (sellingPrice[counter] * numSold); printHeading(); printReport(); }
mostly this type of error comes when you are trying to access an index which doesnt exist in the vector. suppose you have only 5 elements in the vector and you try to fetch [5] or [6] element. in your sellstock method you are looping from 0-6, can you check if your vectors have enough elements? to make it better, you can get the 'size' of the vector and loop from 0-size.
thanks
-chandra
-chandra
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
It wrong here:
if there are less than 6 item in itemName, it will occur memory error.
c++ Syntax (Toggle Plain Text)
for (counter = 0; counter < 6; counter++) if(sellItem == itemName[counter])
if there are less than 6 item in itemName, it will occur memory error.
Last edited by littlestone; May 8th, 2008 at 2:01 am.
![]() |
Similar Threads
- Trouble with files, strings and vectors (C++)
- Why does vector get out of range? (C++)
- Non dereferencable iterator C++ (C++)
- Bound Checking (C++)
- List of Primes (C++)
- Vector of vectors & magic square issues (C++)
Other Threads in the C++ Forum
- Previous Thread: Equation?
- Next Thread: Trouble with strings
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





