1

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;
        }
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
RIDDLEisVoltron
  • 31
  • 1
  • 1
  • 5
  • What's the actual content of `temp` when you call `stoi()`. Could you present your observations from debugging here please! – πάντα ῥεῖ Aug 10 '15 at 20:44
  • Yeah, you need to check the value for `temp`. The `stoi` will throw `invalid_argument` [if no conversion could be performed](http://en.cppreference.com/w/cpp/string/basic_string/stol). Does `getline` remove `\n`? I think so.. but since you are using windows (im making assumption here), I'm not sure if `getline` removes `\r`. – wendelbsilva Aug 10 '15 at 20:47
  • @πάντα ῥεῖ The number 7. – RIDDLEisVoltron Aug 10 '15 at 20:47
  • @RIDDLEisVoltron Are you on Windows? Is your file using CRLF (or you are not sure)? Is the size of temp (`temp.length()`) equals to 2 but should have been 1 ? If you answered true to these questions, check [this thread](http://stackoverflow.com/questions/8960055/getline-to-string-copies-newline-as-well). – wendelbsilva Aug 10 '15 at 20:59
  • yes, windows 7, 64 bit, visual studios 2013. I'm reading in a .txt file. I haven't assigned a length to temp, is that necessary? – RIDDLEisVoltron Aug 10 '15 at 21:03

1 Answers1

4

As from the reference documentation, std::stoi() must be expected to throw these exceptions:

Exceptions

std::invalid_argument if no conversion could be performed std::out_of_range if the converted value would fall out of the range of the result type or if the underlying function (std::strtol or std::strtoll) sets errno to ERANGE.

Thus this exception depends on your actual input, which you're currently not disclosing from your question (unfortunately).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • The input should be the number 7, I'm beginning to think the error might be with the .txt format somehow. Is there something i should be looking at to make sure the flat file is reading in correctly? – RIDDLEisVoltron Aug 10 '15 at 20:59
  • I take that back. I manually assigned temp = 7; and it still broke. – RIDDLEisVoltron Aug 10 '15 at 21:01
  • 1
    @RIDDLEisVoltron: Unfortunately, that assignment doesn't do at all what you expect. Try `temp = 55;`, and see if that converts. – Benjamin Lindley Aug 10 '15 at 21:05
  • I manually assigned temp to 55 (temp = 55;) and It didn't break but, when i cout << temp it gives me 7. what the heck? – RIDDLEisVoltron Aug 10 '15 at 21:11
  • 3
    @RIDDLEisVoltron: `std::string` has an assignment operator that takes a `char`, and sets the string to hold that single `char` as its value. 55 is the ASCII code for the character '7'. So `temp = 55;` is equivalent to `temp = "7";` -- When you did `temp = 7;` you were assigning some non-printing character (the bell character) to `temp`. – Benjamin Lindley Aug 10 '15 at 21:16
  • @ Benjamin Lindley: Any Suggestions? Is there some sort of in-between conversion process that needs to take place? or should i be trying to use something besides stoi to convert a single string number into an int? – RIDDLEisVoltron Aug 10 '15 at 21:21
  • @RIDDLEisVoltron First inspect what your input really is with a debugger as mentioned! – πάντα ῥεῖ Aug 10 '15 at 21:22
  • @RIDDLEisVoltron: The takeaway here is that `stoi` is working fine. Apparently, your string does not contain what you think it does. I cannot reproduce your problem. – Benjamin Lindley Aug 10 '15 at 21:22
  • Nevermind. I feel stupid. I just rewrote the .txt file make extra careful to take out all spaces and now it all works fine. Thanks everyone. – RIDDLEisVoltron Aug 10 '15 at 21:28
  • @πάνταῥεῖ I think you mean "not disclosing", or possibly "concealing" or "withholding". – Alan Stokes Aug 10 '15 at 22:01
  • 1
    @AlanStokes Of course I meant **not disclosing**, THX for pointing out the typo! – πάντα ῥεῖ Aug 10 '15 at 22:06