0

I am trying to convert some strings from a file into integers and doubles using stoi() and stod(). However, I keep getting an error which terminated my program.

Here is a screenshot of what happens when I run the code I included below.

The variable ageString, which I am trying to stoi, is = 100.

As you can see in the screenshot, it actually converts 100 into an integer and prints it out. Cool, that's exactly what I want.

But for some reason it is giving me an error saying invalid_argument. How is 100 an invalid_argument for stoi()?

I already made sure to #include string and to use namespace standard.

I've tried running my code on code::blocks and Repl.it using c++ 11. The results are the same.

I've tried using stoi() on a random number string (not from my file) like 1234 and it still doesn't work.

My .txt file looks like this (single space):

TEST NAME
M
100
200
300
1
//This is the body of my function.
//I've added several cout statements just to help me debug.
//In my actual function I don't plan to have any couts.

std::ifstream inputFile;
inputFile.open(fileName);

std::string name, sex;

int age, activityLevel, calories;

double height, weight;

while(inputFile.good())
{
    using namespace std;
    string ageString, heightString, weightString, activityString;

    getline(inputFile, name, '\n');
    getline(inputFile, sex, '\n'); 
    getline(inputFile, ageString, '\n');
    getline(inputFile, heightString, '\n');
    getline(inputFile, weightString, '\n');
    getline(inputFile, activityString, '\n');

    cout << endl << name             //All of this prints fine,
         << endl << sex              //just like it's written in the file.
         << endl << ageString
         << endl << heightString
         << endl << weightString
         << endl << activityString;

    cout << endl << endl << stoi(ageString); //This converts and prints

    cout << endl << endl << "hello"; //This also prints

}

std::cout << "run this code"; //The program stops working here.
inputFile.close();          

I expect the code to end by printing run this code, but it actually ends by printing hello.

Error message:

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi"

Biffen
  • 5,354
  • 5
  • 27
  • 32
KandCpp
  • 1
  • 3
  • `Exceptions std::invalid_argument if no conversion could be performed` - it means that your input cannot be converted into int. You need to `try catch` exceptions when you use std::stoi – Raffallo May 24 '19 at 05:57
  • I'm trying to stoi() a string that = "100". Why can "100" not be converted to an integer? – KandCpp May 24 '19 at 06:01
  • If `stoi` takes empty string, this exception will be called. You should show the content of your input file. I assume you have there data only for one person. So your while loop is wrong, because it executes twice the code and `stoi` takes empty string. After each getline you should check the state of stream. If it is bad, terminate it. – rafix07 May 24 '19 at 06:01
  • You have the same issue as in [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – molbdnilo May 24 '19 at 06:02
  • @rafix07 yes the file only contains the information of one person. (TEST NAME,M,100,200,300,1,) This is what my file looks like, just replace the ',' with '\n'. – KandCpp May 24 '19 at 06:07
  • Please [edit] your question and show the varbatim input file and a [MCVE]. You could also put a `cout << "\n";` right before calling `stoi`, which will show you what exactly you're passing to `stoi`. – Jabberwocky May 24 '19 at 06:10
  • @Jabberwocky I added the `"\n";` and it printed a blank value. I guess it is running twice like the the people above said. – KandCpp May 24 '19 at 06:27
  • @KandCpp that answers your question, a blank value is an inavlid argument to stoi. – Jabberwocky May 24 '19 at 06:30
  • 2
    Possible duplicate of [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Alan Birtles May 24 '19 at 06:41

1 Answers1

0

I think your code was iterated again in while loop. To ensure that it only iterates 1 times as expectation, please add more cout to check it. For example: I think your code will print start reading input 2 times then terminate your program. If it is, you need to consider using inputFile.good()

while(inputFile.good())
{

    using namespace std;
    cout << "start reading input" << endl;
    string ageString, heightString, weightString, activityString;

    getline(inputFile, name, '\n');
    getline(inputFile, sex, '\n'); 
    getline(inputFile, ageString, '\n');
    getline(inputFile, heightString, '\n');
    getline(inputFile, weightString, '\n');
    getline(inputFile, activityString, '\n');

    cout << endl << name             //All of this prints fine,
         << endl << sex              //just like it's written in the file.
         << endl << ageString
         << endl << heightString
         << endl << weightString
         << endl << activityString;

    cout << endl << endl << stoi(ageString); //This converts and prints

    cout << endl << endl << "hello"; //This also prints

}
the boy
  • 187
  • 6
  • For better stability, it is necessary to use `try catch`, because you read the file, and next you're trying to convert some text into an int. When you get correct values everything is ok, but when you will read some bad things from text file the program will fails – Raffallo May 24 '19 at 07:41
  • yes, of course, we should use 'try catch' for this case, my purpose is for debugging and hope we found the root cause of the issue, as it should not have exception with that input. – the boy May 24 '19 at 07:44
  • but still, it should have `try catch` :) Having various data you will not check every time what went wrong, and for sure the user will not check, so it's worth it to get an appropriate message and handle the exception – Raffallo May 24 '19 at 08:26
  • @theboy Thanks for the response. So yeah the problem was that the loop was running a 2nd time. Earlier in my program when it wrote the file, I made the last line of the file `outputFile << "\n";` This extra empty line in the file was causing the issue. – KandCpp May 24 '19 at 19:33