c++ - R6010 Error When Using; std::stoi -


i have been getting error r6010 - abort() has been called. std::stoi() call , have find out why, ill start showing code @ hand;

    std::string str;      (int = 0; < numberofvalues; i++)     {         str = cmemblock[i];         if (cmemblock[i] == '\n' || cmemblock[i] == '\r')         {             cout << cmemblock[i] << endl;         }         else         {             int number = std::stoi(str);             cout << number;         }     } 

the aim of program @ stage read .txt file , display output in int format, error occurs when .txt file has more 1 line of data.

i should note cmemblock text file stored in chararrayptr format.

what need guidance how can represent lines of data in int format.

the std::stoi() expects see single numeric input in std::string argument passed, e.g.:

int x = std::stoi("12345"); 

no characters, line endings, multiple lines, etc. expected. can use std::istringstream initialized str alternatively:

std::string str = &(cmemblock[0]); std::istringstream iss(str); int number; if(!(iss >> number)) {     // print error ... } 

to read more values subsequently (delimited whitespace or '\n') can make loop:

std::vector<int> numbers; int number; while(iss >> number) {     // collect inputs     numbers.push_back(number); }  if(!iss.eof()) {     // input format error occurred ... } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -