Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Is it okay to fix the terminate called after throwing an instance of 'std::invalid_argument' what(): stoi problem?
Next
Answered
Karina O'Kelly
  • 25
Karina O'Kelly
Asked: May 17, 20222022-05-17T14:13:32+00:00 2022-05-17T14:13:32+00:00In: cpp

Is it okay to fix the terminate called after throwing an instance of ‘std::invalid_argument’ what(): stoi problem?

  • 25

. Advertisement .

..3..

. Advertisement .

..4..

For the problem “terminate called after throwing an instance of ‘std::invalid_argument’ what(): stoi.” I tried to fix it, but it doesn’t work and returns the result I want. Here is my program:

#pragma once
 #include <string>
 #include <vector>
 #include "Task.h"
 #include <ostream>
 #include "ARAIG_Sensors.h"
 namespace m1
 {
  class Profile
  {
  ARAIG_Sensors araig;
  std::vector<Task> ToRun;
  std::vector<Task> Completed;
  std::string studentFN;
  std::string studentLN;
  std::string studentSN;
  std::string flightFN;
  std::string flightLN;
  std::string flightEN;
  std::ostream& os;
 
  struct calibration {
  int Max;
  int Min;
  };
 
  calibration cal;
 
  public:
  Profile(std::string fn, std::ostream& o, ARAIG_Sensors& a) : os(o)
  {
  araig = a;
 
  //parsing student
  std::ifstream infile(fn);
  std::string line;
  std::getline(infile, line);
  int f = line.find_first_of(",");
  studentFN = line.substr(0, f);
  line = line.substr(f + 1);
  f = line.find_first_of(",");
  studentLN = line.substr(0, f);
  studentSN = line.substr(f + 1);
 
  //parsing flight
  std::getline(infile, line);
  f = line.find_first_of(",");
  flightFN = line.substr(0, f);
  line = line.substr(f + 1);
  f = line.find_first_of(",");
  flightLN = line.substr(0, f);
  flightEN = line.substr(f + 1);
 
  //parsing calibration
  std::getline(infile, line);
  f = line.find_first_of(",");
  cal.Min = stoi(line.substr(0, f));
  std::cout << cal.Min << std::endl;
  line = line.substr(f + 1);
  cal.Max = stoi(line);
 
 
 
  std::list<Task> temp = araig.gettasks();
  while (std::getline(infile, line))
  {
  for (std::list<Task>::iterator i = temp.begin(); i != temp.end(); i++)
  {
  if ((*i).getName() == line)
  {
  ToRun.push_back(*i);
  break;
  }
 
 
 
  }
 
  }
 
  }
  void displayToRun()
  {
  for (std::vector<Task>::iterator i = ToRun.begin(); i != ToRun.end(); i++)
  {
  (*i).execute(os);
  }
  }
  void displayCompleted()
  {
  for (std::vector<Task>::iterator i = Completed.begin(); i != Completed.end(); i++)
  {
  (*i).execute(os);
  }
  }
  void displayNext()
  {
  std::vector<Task>::iterator i = ToRun.begin();
  (*i).execute(os);
  }
  void displayLast()
  {
  std::vector<Task>::iterator i = Completed.end();
  (*i).execute(os);
  }
  void Run()
  {
  //excute next Task and move Task to Completed
  std::vector<Task>::iterator i = ToRun.begin();
  Task t = *i;
  t.execute(os);
  ToRun.erase(i);
  Completed.push_back(t);
  }
  };
 
 }

and

"****@****:~> 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 [enabled by default]
 
 Profile.h:1:9: warning: #pragma once in main file [enabled by default]
 
 *****@****:~> a.out StimulationConfig.csv TaskConfiguration.csv SampleProfileConfig.csv SampleOutput.txt
 
 First : a.out
 Second : StimulationConfig.csv
 terminate called after throwing an instance of 'std::invalid_argument'
  what(): stoi
 Aborted

has occurred. I’ve checked the entire command line but still can’t find the mistake.

c++11
  • 2 2 Answers
  • 109 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-07T03:06:06+00:00Added an answer on June 7, 2022 at 3:06 am

    The cause: The error indicates that you provided stoi with a string that could not be converted to an integer.

    Detect the exception and check / print the string that created the issue.

    Solution:

    To solve the issue software is shutting down, you should use:

    try
    {
    .
    .
    .
    }
    catch (const std:: invalid_argument& e)
    {
    }

    block around this:

    while (getline...

    The findings that produce the exception can then be shown.

    • 7
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Manon Berthier
    2022-05-26T03:56:07+00:00Added an answer on May 26, 2022 at 3:56 am

    This means that std::stoi() is receiving bad input. It’s throwing a exception you are not catching.

    std::stoi, std::stol, std::stoll:

    There are exceptions

    • std::invalid_argument if no conversion could be done

    • std::out_of_range If the value converted would be outside the range of the result types or if std::strtol or std::strtoll is used, errno to ERANGE.


    std::terminate

    std::terminate() is called by the C++ runtime when exception handling fails for any of the following reasons:

    1) an exception is thrown and not caught (it is implementation-defined whether any stack unwinding is done in this case)

    Double-check the input string values you’re trying to convert into integers. They are not integers.

    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.