Advertisement
koodeta

Stack Overflow copypasta post

May 8th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. **Debug Error**
  2.  
  3. Projects\Final Project\GrItem\Debug\GrItem.exe
  4.  
  5. R6010 - abort() has been called
  6.  
  7. I was going over this with a friend and it seems as though `getline()` is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files.
  8.  
  9. **Quick Code**
  10.  
  11. The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.
  12.  
  13. // Read regular price
  14. getline(nameFile, input, '$');
  15. vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
  16. // Read sale price
  17. getline(nameFile, input, '#');
  18. vectorList[count].salePrice = stof(input.c_str());
  19.  
  20. **Pastebin Links**
  21.  
  22. GrItem.cpp http://pastebin.com/uMxErkjQ
  23.  
  24. StoreInfo.h http://pastebin.com/j6Gyi3yV
  25.  
  26. **Full Code**
  27.  
  28. #include "stdafx.h"
  29. #include "StoreInfo.h"
  30. #include <stdio.h>
  31. #include <iostream>
  32. #include <fstream>
  33. #include <algorithm>
  34. #include <vector>
  35. #include <iomanip>
  36. #include <map>
  37.  
  38. using namespace std;
  39.  
  40. // Tax rate catagories
  41. const double alcoholTax = 0.08;
  42. const double foodTax = 0.05;
  43. const double genMerchandiseTax = 0.07;
  44. const double medicineTax = 0.04;
  45.  
  46. struct cost {
  47. double alcTax = 0.0, food = 0.0, genMerch = 0.0, meds = 0.0;// Total taxes collected for each tax bracket
  48. double totalTax = alcTax + food + genMerch + meds;// Total Taxes to be accessed later
  49.  
  50. // Variables used in the accessor functions at the bottom
  51. double costBeforeTax = 0.0, costAfterTax = 0.0, custSaving = 0.0, totRegPrice = 0.0, totSalePrice = 0.0;
  52. };
  53.  
  54. // Constructor
  55. GrItem::GrItem(string name, int quantity, float regPrice, float salePrice, bool onSale, TaxCategory taxCategory){
  56.  
  57. name = name;
  58. quantity = quantity;
  59. regPrice = regPrice;
  60. salePrice = salePrice;
  61. onSale = onSale;
  62. taxCategory = taxCategory;
  63.  
  64. };
  65.  
  66. // Default constructor
  67. GrItem::GrItem() {
  68. }
  69.  
  70. // Get the total cost before the tax
  71. void getTotBeforeTax(double regPrice, double salePrice, bool onSale){
  72. cost itemTotalCost;
  73. if (onSale == 1){
  74. itemTotalCost.costBeforeTax += salePrice;
  75. itemTotalCost.totSalePrice += salePrice;
  76. }
  77. else{
  78. itemTotalCost.costBeforeTax += regPrice;
  79. itemTotalCost.totRegPrice += regPrice;
  80. }
  81. }
  82.  
  83. // Get the total after tax
  84. void getTotAfterTax(float costBeforeTax, float totalTax){
  85. cost itemTotalCost;
  86. itemTotalCost.costAfterTax = costBeforeTax + totalTax;
  87. }
  88.  
  89. // Get the total amount of tax for each category
  90. void getTotTaxCategory(double regPrice, double salePrice, bool onSale, TaxCategory taxCategory){// These different values are determined by what enum tax category they are
  91. cost itemTotalCost;
  92. // Adds values to the total alcohol tax gathered
  93. if (taxCategory = TaxCategory::alcohol){
  94. if (onSale = true)
  95. itemTotalCost.alcTax += salePrice * alcoholTax;
  96. itemTotalCost.alcTax += regPrice * alcoholTax;
  97. }
  98.  
  99. if (taxCategory = TaxCategory::food){
  100. if (onSale = true)
  101. itemTotalCost.alcTax += salePrice * foodTax;
  102. itemTotalCost.alcTax += regPrice * foodTax;
  103. }
  104.  
  105. if (taxCategory = TaxCategory::genMerchandise){
  106. if (onSale = true)
  107. itemTotalCost.alcTax += salePrice * genMerchandiseTax;
  108. itemTotalCost.alcTax += regPrice * genMerchandiseTax;
  109. }
  110.  
  111. if (taxCategory = TaxCategory::medicine){
  112. if (onSale = true)
  113. itemTotalCost.alcTax += salePrice * medicineTax;
  114. itemTotalCost.alcTax += regPrice * medicineTax;
  115. }
  116. }
  117.  
  118. // Get customer savings (total of all differences between regular price and sale price for items that are currently on sale)
  119. void getCustSave(double totRegPrice, double totSalePrice, bool onSale){
  120. cost itemTotalCost;
  121. if (onSale == 1){
  122. itemTotalCost.custSaving = totRegPrice - totSalePrice;
  123. }
  124.  
  125. }
  126.  
  127. // Function that is called to sort by name
  128. bool sortByName(const GrListItem &lhs, const GrListItem &rhs){
  129. return lhs.name < rhs.name;
  130. }
  131.  
  132. // Function that is called to sort by quantity
  133. bool sortByQuantity(const GrListItem &lhs, const GrListItem &rhs){
  134. return lhs.quantity < rhs.quantity;
  135. }
  136.  
  137. // Function that is called to sort by regular price
  138. bool sortByRegPrice(const GrListItem &lhs, const GrListItem &rhs){
  139. return lhs.regPrice < rhs.regPrice;
  140. }
  141.  
  142. // Function that is called to sort by sale price
  143. bool sortBySalePrice(const GrListItem &lhs, const GrListItem &rhs){
  144. return lhs.salePrice < rhs.salePrice;
  145. }
  146.  
  147. const int listSize = 20;
  148. // Main method
  149. int main() {
  150.  
  151. string input;// Holds each line from the imported textfile temporarily
  152. string fileName;// Name of grocery list user wishes to use
  153. fstream nameFile;// File stream object
  154. GrListItem itemList[listSize];// Creates a list of objects. These objects are each item on the list and hold related information
  155. std::vector<GrListItem> vectorList(itemList, itemList + listSize);
  156. cost itemTotalCost;
  157.  
  158. // Requests data from user
  159. cout << "What is the name of the grocery list you wish to use? " << endl;
  160. getline(cin, fileName);// Retrieves filename from user and applies string to grListName
  161.  
  162. // Tests to see if file can be opened
  163. fstream testFile(fileName, ios::out);
  164. if (testFile.fail()){
  165. cout << "ERROR: Cannot open indicated file.\n";
  166. return 0;
  167. }
  168.  
  169. // Open data file
  170. nameFile.open(fileName, ios::in);
  171. // Read data and apply variables to an object
  172. if (nameFile){
  173. int count = 0;
  174. while (nameFile && count < listSize){
  175.  
  176. // Read the name
  177. getline(nameFile, input, '#');
  178. vectorList[count].name = input;// Assigns item name to the object inside itemList.name
  179.  
  180. // Read quantity
  181. getline(nameFile, input, '$');
  182. vectorList[count].quantity = atoi(input.c_str());// Casts string to an int
  183.  
  184. // Read regular price
  185. getline(nameFile, input, '$');
  186. vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
  187.  
  188. // Read sale price
  189. getline(nameFile, input, '#');
  190. vectorList[count].salePrice = stof(input.c_str());
  191.  
  192. // Read on sale bool
  193. getline(nameFile, input, '#');
  194. if (input == "Y")// If the item is on sale, the onSale var returns true
  195. vectorList[count].onSale = true;
  196. else
  197. vectorList[count].onSale = false;
  198.  
  199. // Read tax category
  200. getline(nameFile, input, '#');
  201. if (input == "alcohol") {
  202. vectorList[count].taxCategory = TaxCategory::alcohol;;
  203. }
  204. else if (input == "general merchanise") {
  205. vectorList[count].taxCategory = TaxCategory::genMerchandise;
  206. }
  207. else if (input == "food") {
  208. vectorList[count].taxCategory = TaxCategory::food;;
  209. }
  210. else if (input == "medicine") {
  211. vectorList[count].taxCategory = TaxCategory::medicine;;
  212. }
  213. else {
  214. std::cout << "BAD TAX CATEGORY" << std::endl;
  215. }
  216.  
  217. // These functions are called as many times as there are objects in the array.
  218. getTotBeforeTax(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale);
  219. getTotTaxCategory(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale, vectorList[count].taxCategory);
  220. getTotAfterTax(itemTotalCost.costBeforeTax, itemTotalCost.totalTax);
  221.  
  222. count++;
  223. }
  224. // Close file
  225. nameFile.close();
  226. }
  227. else
  228. cout << "ERROR: Cannot open file.\n";
  229.  
  230. // Sort array
  231. // OFFER USER TO CHOOSE HOW THEY WOULD LIKE TO SORT THEIR LIST!!! For extra points
  232. // Maybe
  233. std::sort(vectorList.begin(), vectorList.end(), sortByName);
  234.  
  235. // For loop that creates a receipt on the screen
  236. // Formatting may or may not be correct
  237. for (int i = 0; i != listSize; ++i){
  238. std::cout << vectorList[i].name << endl;// Print item name
  239. std::cout << std::setfill(' ') << std::setw(20);
  240. std::cout.fill(' ');
  241.  
  242. std::cout << std::setfill(' ') << std::setw(5);// Print item quantity
  243. std::cout << vectorList[i].quantity << endl;
  244. std::cout.fill(' ');
  245.  
  246. std::cout << std::setfill(' ') << std::setw(5);// Print regular price of item
  247. std::cout << vectorList[i].regPrice << endl;// Adjust preci
  248. std::cout.fill(' ');
  249.  
  250. std::cout << std::setfill(' ') << std::setw(5);// Print sale price of item
  251. std::cout << vectorList[i].salePrice << endl;
  252. std::cout.fill(' ');
  253.  
  254. if (vectorList[i].onSale == 1){
  255. std::cout << 'Y' << endl;// Print 'Y' if vectorList[i] is on sale
  256. std::cout.width(3);
  257. std::cout.fill(' ');
  258. }
  259. else {
  260. std::cout << 'N' << endl;// Print 'N' if vectorList[i] is not on sale
  261. std::cout.width(3);
  262. std::cout.fill(' ');
  263. }
  264.  
  265. std::cout << vectorList[i].taxCategory << endl;// Print tax category
  266. std::cout.width(20);
  267. std::cout.fill(' ');
  268. }
  269.  
  270. // Print details of purchase below list of bought items
  271.  
  272. // NOTE: THESE VALUES ARE STORED IN THE STRUCT ABOVE main()
  273. // Display total before tax
  274. // Display total after tax
  275. // Display customer Savings
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement