• Register
0 votes
1.5k views

Problem :

I want to convert the string into an int by using stoi, however for some unknown reason it is giving me an error and giving me following message:

"libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion"

The myline which is dealing with stoi is present towards the bottom of the code.

Please find below my code :

 #include "LongDistanceCalls.h"
 #include <iostream>
 #include <string>
 #include <fstream>
 #include <sstream>
 #include <algorithm>
 #include <iterator>
using namespace std;  
string myline;
string mytemp = "";
string mybeginning_time;
void convertTimeintoInt(string mybeginning_time)
{
  for(char x : mybeginning_time)
  {
     if(x == ':')
            continue;
    else
       mytemp += x;
  }
}
int main()
{
  ifstream inFile;
  string myday;
  int myminutes;
  double mytotal_callpay;
  //opens the .txt file
  inFile.open("MyCallRecords.txt");
//if the .txt file is openable, then printed to command myline.
  if (inFile.is_open())
  {
     cout<<"Mydays Time Durations Cost"<<endl;
     while(getmyline(inFile,myline))
     {
         istringstream split(myline);//splits each string into 3 separate strings (myday, mybeginning_time, myminutes)
        while(split)
             {
               split >> myday;
               split >> mybeginning_time;
               split >> myminutes;
               **int time = stoi(mytemp);**
               time = time > 0 && time < 2400;
6 5 3
7,540 points

1 Answer

0 votes

Solution :

Please find below the solution for your error:

string mytemp = "";

int main()
{
    ...
    int time = stoi(mytemp);
    ...
}

Where do you change the value of mytemp. stoi is erroring out because the input value is an empty string. See here under the Exceptions entry at the bottom.

9 7 4
38,600 points

Related questions

0 votes
0 answers 304 views
304 views
Problem : I want to convert the string into an int by using stoi, however for some unknown reason it is giving me an error and giving me following message: "libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion" The myline which is ... ; split >> mybeginning_time; split >> myminutes; **int time = stoi(mytemp);** time = time > 0 && time < 2400;
asked Dec 5, 2019 AbhijitJ 38.6k points
1 vote
1 answer 1.9K views
1.9K views
Problem : My stoi function is crashing the program with the error message as below : "****@****:~> g++ -std=c++0x m1.cpp stimulation.h stims.h Task.h exoskeleton.h ARAIG_Sensors.h Profile.h ARAIG_Sensors.h:1:9: warning: #pragma once in main file [ ... know why is there the instance being passed to the method but it is still returning the above error. Can somebody help me in resolving the issue?
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 15 views
0 votes
1 answer 13 views
0 votes
1 answer 10 views