Debug error when taking a string and casting it to a float with a file stream

Debug Error

Projects\Final Project\GrItem\Debug\GrItem.exe

R6010 - abort() has been called

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 because it is far to large for one post I shall, however, post the full code in the comments.

Quick Code

The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.

1
2
3
4
5
6
    // Read regular price
    			getline(nameFile, input, '$');
    			vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
    // Read sale price
    			getline(nameFile, input, '#');
    			vectorList[count].salePrice = stof(input.c_str());


Pastebin Links

GrItem.cpp http://pastebin.com/uMxErkjQ

StoreInfo.h http://pastebin.com/j6Gyi3yV

Here's the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "stdafx.h"
#include "StoreInfo.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iomanip>

using namespace std;

// Tax rate catagories
const double alcoholTax = 0.08;
const double foodTax = 0.05;
const double genMerchandiseTax = 0.07;
const double medicineTax = 0.04;

struct cost {
	double alcTax = 0.0, food = 0.0, genMerch = 0.0, meds = 0.0;// Total taxes collected for each tax bracket
	double totalTax = alcTax + food + genMerch + meds;// Total Taxes to be accessed later

	// Variables used in the accessor functions at the bottom
	double costBeforeTax = 0.0, costAfterTax = 0.0, custSaving = 0.0, totRegPrice = 0.0, totSalePrice = 0.0;
};

// Constructor
GrItem::GrItem(string name, int quantity, float regPrice, float salePrice, bool onSale, TaxCategory taxCategory){

	name = name;
	quantity = quantity;
	regPrice = regPrice;
	salePrice = salePrice;
	onSale = onSale;
	taxCategory = taxCategory;

};

// Default constructor
GrItem::GrItem() {
}

// Get the total cost before the tax
void getTotBeforeTax(double regPrice, double salePrice, bool onSale){
	cost itemTotalCost;
	if (onSale == 1){
		itemTotalCost.costBeforeTax += salePrice;
		itemTotalCost.totSalePrice += salePrice;
	}
	else{
		itemTotalCost.costBeforeTax += regPrice;
		itemTotalCost.totRegPrice += regPrice;
	}
}

// Get the total after tax
void getTotAfterTax(float costBeforeTax, float totalTax){
	cost itemTotalCost;
	itemTotalCost.costAfterTax = costBeforeTax + totalTax;
}

// Get the total amount of tax for each category
void getTotTaxCategory(double regPrice, double salePrice, bool onSale, TaxCategory taxCategory){// These different values are determined by what enum tax category they are
	cost itemTotalCost;
	// Adds values to the total alcohol tax gathered
	if (taxCategory = TaxCategory::alcohol){
		if (onSale = true)
			itemTotalCost.alcTax += salePrice * alcoholTax;
		itemTotalCost.alcTax += regPrice * alcoholTax;
	}

	if (taxCategory = TaxCategory::food){
		if (onSale = true)
			itemTotalCost.alcTax += salePrice * foodTax;
		itemTotalCost.alcTax += regPrice * foodTax;
	}

	if (taxCategory = TaxCategory::genMerchandise){
		if (onSale = true)
			itemTotalCost.alcTax += salePrice * genMerchandiseTax;
		itemTotalCost.alcTax += regPrice * genMerchandiseTax;
	}

	if (taxCategory = TaxCategory::medicine){
		if (onSale = true)
			itemTotalCost.alcTax += salePrice * medicineTax;
		itemTotalCost.alcTax += regPrice * medicineTax;
	}
}

// Get customer savings (total of all differences between regular price and sale price for items that are currently on sale)
void getCustSave(double totRegPrice, double totSalePrice, bool onSale){
	cost itemTotalCost;
	if (onSale == 1){
		itemTotalCost.custSaving = totRegPrice - totSalePrice;
	}

}

// Function that is called to sort by name
bool sortByName(const GrListItem &lhs, const GrListItem &rhs){
	return lhs.name < rhs.name;
}

// Function that is called to sort by quantity
bool sortByQuantity(const GrListItem &lhs, const GrListItem &rhs){
	return lhs.quantity < rhs.quantity;
}

// Function that is called to sort by regular price
bool sortByRegPrice(const GrListItem &lhs, const GrListItem &rhs){
	return lhs.regPrice < rhs.regPrice;
}

// Function that is called to sort by sale price
bool sortBySalePrice(const GrListItem &lhs, const GrListItem &rhs){
	return lhs.salePrice < rhs.salePrice;
}

const int listSize = 20;
// Main method
int main() {

	string input;// Holds each line from the imported textfile temporarily
	string fileName;// Name of grocery list user wishes to use
	fstream nameFile;// File stream object
	GrListItem itemList[listSize];// Creates a list of objects. These objects are each item on the list and hold related information
	std::vector<GrListItem> vectorList(itemList, itemList + listSize);
	cost itemTotalCost;

	// Requests data from user
	cout << "What is the name of the grocery list you wish to use? " << endl;
	getline(cin, fileName);// Retrieves filename from user and applies string to grListName

	// Tests to see if file can be opened
	fstream testFile(fileName, ios::out);
	if (testFile.fail()){
		cout << "ERROR: Cannot open indicated file.\n";
		return 0;
	}

	// Open data file
	nameFile.open(fileName, ios::in);
	// Read data and apply variables to an object
	if (nameFile){
		int count = 0;
		while (nameFile && count < listSize){

			// Read the name
			getline(nameFile, input, '#');
			vectorList[count].name = input;// Assigns item name to the object inside itemList.name

			// Read quantity
			getline(nameFile, input, '$');
			vectorList[count].quantity = atoi(input.c_str());// Casts string to an int

			// Read regular price
			getline(nameFile, input, '$');
			vectorList[count].regPrice = stof(input.c_str());// Casts string to a float

			// Read sale price
			getline(nameFile, input, '#');
			vectorList[count].salePrice = stof(input.c_str());

			// Read on sale bool
			getline(nameFile, input, '#');
			if (input == "Y")// If the item is on sale, the onSale var returns true 
				vectorList[count].onSale = true;
			else
				vectorList[count].onSale = false;

			// Read tax category
			getline(nameFile, input, '#');
			if (input == "alcohol") {
				vectorList[count].taxCategory = TaxCategory::alcohol;;
			}
			else if (input == "general merchanise") {
				vectorList[count].taxCategory = TaxCategory::genMerchandise;
			}
			else if (input == "food") {
				vectorList[count].taxCategory = TaxCategory::food;;
			}
			else if (input == "medicine") {
				vectorList[count].taxCategory = TaxCategory::medicine;;
			}
			else {
				std::cout << "BAD TAX CATEGORY" << std::endl;
			}

			// These functions are called as many times as there are objects in the array. 
			getTotBeforeTax(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale);
			getTotTaxCategory(vectorList[count].regPrice, vectorList[count].salePrice, vectorList[count].onSale, vectorList[count].taxCategory);
			getTotAfterTax(itemTotalCost.costBeforeTax, itemTotalCost.totalTax);

			count++;
		}
		// Close file
		nameFile.close();
	}
	else
		cout << "ERROR: Cannot open file.\n";

	// Sort array 
	// OFFER USER TO CHOOSE HOW THEY WOULD LIKE TO SORT THEIR LIST!!! For extra points
	// Maybe
	std::sort(vectorList.begin(), vectorList.end(), sortByName);

	// For loop that creates a receipt on the screen
	// Formatting may or may not be correct
	for (int i = 0; i != listSize; ++i){
		std::cout << vectorList[i].name << endl;// Print item name
		std::cout << std::setfill(' ') << std::setw(20);
		std::cout.fill(' ');

		std::cout << std::setfill(' ') << std::setw(5);// Print item quantity
		std::cout << vectorList[i].quantity << endl;
		std::cout.fill(' ');

		std::cout << std::setfill(' ') << std::setw(5);// Print regular price of item
		std::cout << vectorList[i].regPrice << endl;// Adjust preci
		std::cout.fill(' ');

		std::cout << std::setfill(' ') << std::setw(5);// Print sale price of item
		std::cout << vectorList[i].salePrice << endl;
		std::cout.fill(' ');

		if (vectorList[i].onSale == 1){
			std::cout << 'Y' << endl;// Print 'Y' if vectorList[i] is on sale
			std::cout.width(3);
			std::cout.fill(' ');
		}
		else {
			std::cout << 'N' << endl;// Print 'N' if vectorList[i] is not on sale
			std::cout.width(3);
			std::cout.fill(' ');
		}

		std::cout << vectorList[i].taxCategory << endl;// Print tax category
		std::cout.width(20);
		std::cout.fill(' ');
	}

	// Print details of purchase below list of bought items

	// NOTE: THESE VALUES ARE STORED IN THE STRUCT ABOVE main()
	// Display total before tax
	// Display total after tax
	// Display customer Savings

}
Line 135 will either create a new file and open it or open and truncate an existing file.
Topic archived. No new replies allowed.