vector subscript out of range

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 32
Reputation: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

vector subscript out of range

 
0
  #1
May 8th, 2008
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?

  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. #include <iomanip>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. 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);
  12. void do_again(),usersChoice(char ans);
  13. void searchInventory(string searchInput);
  14. void printReport();
  15. void printHeading();
  16. void sellStock(string sellItem, int numSold);
  17. int showMenu();
  18. int ans();
  19.  
  20. vector<int>itemID, pOrdered, pInStore, pSold;
  21. vector<double>manufPrice,sellingPrice;
  22. vector<string>itemName;
  23. int totalItems;
  24. double totalInv;
  25. unsigned int counter;
  26. int searchName;
  27. int sellName;
  28. int sellNum;
  29. char choice;
  30. string searchInput;
  31. string input;
  32. int noOfRows;
  33.  
  34.  
  35. int main()
  36. {
  37. ifstream incode;
  38.  
  39. incode.open("itemInfo.txt");
  40. if (!incode)
  41. {
  42. cout << "Can't open the input file" << endl;
  43. return 1;
  44. }
  45.  
  46. getData(incode, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
  47.  
  48. showMenu();
  49. cin.get();
  50.  
  51. incode.close();
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. 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)
  60. {
  61. int sold = 0;
  62. string name;
  63. int id, ordered;
  64. double mPrice;
  65. double sPrice;
  66. char ch;
  67.  
  68. while (infile >> id)
  69. {
  70. infile.get(ch);
  71.  
  72. getline(infile, name);
  73.  
  74. infile >> ordered >> mPrice >> sPrice;
  75.  
  76.  
  77. itemID.push_back(id);
  78. itemName.push_back(name);
  79. pOrdered.push_back(ordered);
  80. pInStore.push_back(ordered);
  81. pSold.push_back(sold);
  82. manufPrice.push_back(mPrice);
  83. sellingPrice.push_back(sPrice);
  84. }
  85. }
  86. void usersChoice(char ans)
  87. {
  88. int counter2 = 1;
  89.  
  90. switch (choice)//roll thru options
  91. {
  92. case '1': //if they picked the number 1, its gonna go to this function,
  93. //thats pretty much how the whole thing works
  94. unsigned int counter;
  95. cout << "\n\n\t\tWhich item would you like to check the inventory for?\n\n";
  96.  
  97. for (counter = 0; counter < itemName.size(); counter++)
  98. {
  99. cout << "\t\t\t" << counter + 1 << ". " << itemName[counter] << "\n";
  100.  
  101. }
  102. cout << "\n\n\t\t\t Choice: ";
  103.  
  104. cin >> searchName;
  105. //cout << "\n\n\t" << itemName[searchName - 1];
  106. //input = ;
  107. searchInventory(itemName[searchName - 1]);
  108. break;
  109. case '2':
  110. // run sellStock function
  111.  
  112. cout << "Which item would you like to sell?"
  113. << endl;
  114.  
  115. for (counter = 0; counter < itemName.size(); counter++)
  116. {
  117. cout << "\t\t\t" << counter + 1 << ". " << itemName[counter] << "\n";
  118.  
  119. }
  120. cout << "\n\n\t\t\t Choice: ";
  121. cin >> sellName;
  122.  
  123. cout << "\n\t\tHow many would you like to sell? ";
  124. cin >> numSold;
  125.  
  126. sellStock(itemName[sellName - 1], numSold);
  127.  
  128. break;
  129. case '3':
  130. // run print Report
  131. printHeading();
  132. printReport();
  133.  
  134. break;
  135. case '4':
  136. // exit the program
  137. break;
  138. default://THEY'RE NOT FOLLOWING DIRECTIONS?!?!
  139. cout << "\t\tInvalid Input"
  140. << endl;//if directions aren't followed
  141. }//end switch
  142. }//end function
  143.  
  144. int showMenu()
  145. {
  146. do
  147. {
  148. cout << "\n\n\t\tPlease choose from the following menu (1, 2, 3, or 4 to EXIT):\n";
  149. cout << "\n\t\t (1) Check if an item is in inventory";
  150. cout << "\n\t\t (2) Sell an item";
  151. cout << "\n\t\t (3) Print a report\n\n";
  152. cout << "\n\t\t (4) Exit\n\n\t\t\t Choice: ";
  153. cin >> choice;
  154. usersChoice(choice);//send that answer to usersChoice!
  155. }while(choice != '4');
  156.  
  157.  
  158.  
  159. return choice;
  160. }
  161.  
  162. void searchInventory(string searchInput)
  163. {
  164.  
  165. for (counter = 0; counter < itemName.size(); counter++)
  166. if(itemName[counter] == searchInput)
  167. cout << "\n\t\tThere are " << pInStore[counter] << " " << searchInput << "s in stock.";
  168.  
  169. }
  170.  
  171. void printHeading()
  172. {
  173. cout << "\n\n\t\t\t\tFriendly Hardware Store" << endl << endl;
  174. cout << "\tItemID\t" << "ItemName\t" << "pOrdered" << " pInStore" << " pSold" << " manufPrice" << " sellingPrice\n\n";
  175.  
  176. }
  177.  
  178.  
  179. void printReport ()
  180. {
  181. unsigned int x;
  182. double totalInventory = 0.00; //Value of all available inventory
  183. int totalItems = 0;
  184. double itemInventory = 0.00;
  185.  
  186. for (x = 0; x <= itemID.size() - 1; x++)
  187.  
  188. {
  189.  
  190. 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";
  191. itemInventory = static_cast<double>(pInStore[x]) * sellingPrice[x];
  192. totalInventory = totalInventory + itemInventory;
  193. totalItems = totalItems + pInStore[x];
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201. cout << "Total Inventory: $" << totalInventory << endl;
  202. cout << "Total number of items in the store: " << totalItems << endl;
  203.  
  204.  
  205. }
  206.  
  207. void sellStock(string sellItem, int numSold)
  208. {
  209. int totalItems = 0;
  210. double itemInventory = 0.00;
  211. double totalInventory = 0.00;
  212.  
  213. for (counter = 0; counter < 6; counter++)
  214.  
  215. if(sellItem == itemName[counter])
  216. pInStore[counter] = pInStore[counter] - numSold;
  217. pSold[counter] = pSold[counter] + numSold;
  218. totalItems = totalItems + pInStore[counter];
  219. totalItems = totalItems - numSold;
  220. totalInventory = totalInventory - (sellingPrice[counter] * numSold);
  221.  
  222.  
  223. printHeading();
  224. printReport();
  225. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: vector subscript out of range

 
0
  #2
May 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: vector subscript out of range

 
0
  #3
May 8th, 2008
It wrong here:

  1.  
  2. for (counter = 0; counter < 6; counter++)
  3.  
  4. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC