• Register
0 votes
2k 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

Please log in or register to answer this question.

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 372 views
372 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 2.6K views
2.6K 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 14 views
14 views
Problem : terminating app due to uncaught exception 'nsunknownkeyexception'
asked Feb 14 Shovo210 3.9k points
0 votes
1 answer 14 views
14 views
Problem: I'm trying to get std::function< > into a std::set< >. To do this, you need a function that compares the entered values. Since it std::functiondoesn't provide any comparison operator, I've thought about this: #include <functional> template< typename... ARGS > ... Note: The code is the minimum reproducible. I've kept what I think is important What am I doing wrong ? How do I solve it ?
asked Feb 7 sasha 13.2k points
0 votes
1 answer 22 views
0 votes
1 answer 6 views
6 views asked Feb 17 Akhil k 1.3k points
0 votes
1 answer 19 views
0 votes
1 answer 22 views
0 votes
1 answer 55 views
55 views
Problem : I want to compile my code to test my function to read and print the data file, but I get the compiling error that I don't understand as follows "Error: expected constructor, destructor, or type conversion before ';' token".
asked Jan 24, 2020 jwilliam 3.9k points
1 vote
1 answer 273 views
273 views
Problem : I am a Computer Science Student. I am trying to learn the C++ programming. I want to execute a very simple averaging code written by me but I always face below error: Terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: ... larger than 0? So I am confused because of above error. Can some expert in C++ guide me in fixing above error for me.
asked May 14, 2020 Martin K 6.6k points