The program runs fine until it gets to the stoi function, then the program breaks and gives me this error " Microsoft C++ exception: std::invalid_argument at memory location 0x0030EE7C." I've looked at tutorials on using stoi and I'm not sure what I'm doing wrong. The flat file reads like this:
Organic 7 description light 4 description menthol 5 description. with each word or number on a new line.
struct ProdDescriptor { string name; string price; string descript; }; void getProds() // reads products off of the flat file { int array = 3; ProdDescriptor x[3]; ifstream ItemRead(FlatFileName); // object of the flat file string temp; if (ItemRead.is_open()) // opens flat file and reads { for (int i = 0; i < array; i++) { ProdSpecPrice[i] = 0; // initialize getline(ItemRead, x[i].name); getline(ItemRead, x[i].price); getline(ItemRead, x[i].descript); temp = x[i].price; ProdSpecPrice[i] = stoi(temp); ProdSpecName[i] = x[i].name; ProdSpecDescription[i] = x[i].descript; } 51 Answer
As from the reference documentation, std::stoi() must be expected to throw these exceptions:
Exceptions
std::invalid_argumentif no conversion could be performedstd::out_of_rangeif the converted value would fall out of the range of the result type or if the underlying function (std::strtolorstd::strtoll) setserrnotoERANGE.
Thus this exception depends on your actual input, which you're currently not disclosing from your question (unfortunately).
11