I've been scouring the forums and internet trying to find a solution to my problem but I can't seem to find anything that relates to me specifically. As a disclaimer, while this relates to homework, the question I'm asking isn't how to do it, it's how to fix a problem in what I already have done.
Anyway, this project is to get an indeterminate amount of values from a file, store them in variables, and output them in a "receipt" layout. I decided the best way to do this would be structs and vectors
struct Item {
string itemName;
double itemPrice;
bool taxable;
}
vector<Item> itemList;
Now that I have my struct and my vector set up in my program, I'm trying to read from the file using getlines.
int vectorNumber = 0;
while(!inFile.eof()){
itemList.resize(vectorNumber+1);
string tempDouble;
string tempBool;
getline(inFile, itemList[vectorNumber].itemName);
getline(inFile, tempDouble);
itemList[vectorNumber].itemPrice = stod(tempDouble);
getline(inFile, tempBool);
itemList[vectorNumber].taxable = tempBool == "Y" ? true : false;
vectorNumber++;
}
The reason I'm doing itemList.resize(vectorNumber + 1) is because I was getting a vector subscript out of range
on the first getLine. I figured it was because the vector was blank, but I don't know for sure. Anyway, after I added that line, the program was able to move on, but now on the line with the stod, I'm getting an Unhandled exception at [hexValue] in [exe]: std::invalid_argument at memory location [hexValue].
I added a temporary cout << tempDouble
before the faulty stod line to see if it was actually pulling a value, and it is. So my question is why am I getting this weird error?
If I comment out everything from the second getline to the ?: line, the program runs successfully, so it's just this one thing I need help fixing. What am I doing wrong? The value it's pulling from the file (I checked it using cout) is 2.49, with an endline character after it. I know getline discards the endline character, but I figured I'd just tell you.
Thank you!
EDIT: I've been asked to see a minimal version of the code that still gives me problems, so here we are:
// TestApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct Item {
string itemName;
double itemPrice;
};
vector<Item> itemList;
int main()
{
ifstream inFile;
inFile.open("HW3_Data.txt");
int vectorNumber = 0;
while (!inFile.eof()) {
itemList.resize(vectorNumber + 1);
string tempDouble;
string tempBool;
getline(inFile, itemList[vectorNumber].itemName);
getline(inFile, tempDouble);
itemList[vectorNumber].itemPrice = stod(tempDouble);
vectorNumber++;
}
}
Because I have a feeling this has to do with the file itself and not the program, here is the file:
Bread
2.49
Y
Milk
1.89
N
Eggs, dozen
0.97
N
Apples
4.75
Y
Bananas
1.69
Y
Peanut Butter
2.49
Y
I copied and pasted the file straight into here. There is another line underneath the Y in the last line of the file, that is not an extra return I added in.