0

I am trying to represent a variable in the form of a string to a integer, I have done so using;

atoi(str.c_str()) 

The string is originally obtained from a text file and stored into a;

CharArrayPtr cmemblock;

Which is then represented as a string;

    string str;

    for(int i = 0; i < numberofvalues; i++)
    {   
        str = cmemblock[i];
        int number = atoi(str.c_str());
        cout << number;


    }

If I was to change the 'cout' to print str;

str = cmemblock[i];
int number = atoi(str.c_str());
cout << str;

The number show correctly as stored in the text file

However, I require the output to be an integer so that I could represent it in a loop to search for a value stored in a array. So this is where 'number' comes into play, which is the reason why I am asking for your help, when;

cout << number;

Whenever a new line is read it is represented as '0' how would I go about removing this? If your require my full code it is in several different .cpp files and to prevent anyone copying my work I can only email it you, im sure you have already guessed it is part of a University Assignment.

Using Member Adosi code I came up with this;

         std::string str;

    for(int i = 0; i < numberofvalues; i++)
    {   

        str = cmemblock[i];
        std::stol(str);
        int number = std::stoi(str);
        cout << number;


    }

I get an error R6010. Have I done this wrong?

3 Answers3

3

std::stoi(str)

Use this instead of atoi

C++11 has this and a few other functions such as std::stol() for longs, std::stof() for floats, etc.

http://en.cppreference.com/w/cpp/string/basic_string/stol

Omar Himada
  • 2,540
  • 1
  • 14
  • 31
  • I have tried using this method I get the following error; R6010 - abort() has been called – NoobProgrammer Mar 21 '14 at 14:04
  • @NoobProgrammer Are you compiling with a C++11 enabled compiler? – const_ref Mar 21 '14 at 14:09
  • Should have checked that apologies, no im using visual studio 2010, ill use the methods listed below. Thank you for your help! – NoobProgrammer Mar 21 '14 at 14:14
  • Is it possible to add in a C++11 compiler to visual studio 2010? – NoobProgrammer Mar 21 '14 at 14:31
  • @NoobProgrammer Its probably easier to simply upgrade and use VS2012 or 2013. Nathans suggestion will work on VS2010. I only gave one possible way of doing it on the off chance you had boost installed. For quickness sakes, just use a stringstream – const_ref Mar 21 '14 at 16:00
  • Even outside of this one question it will be worth it in the long run to upgrade to C++11 via new Visual Studio (Express is free, by the way). The myriad of additional features and goodies largely outweigh the trouble of having to change versions. – Omar Himada Mar 21 '14 at 16:10
  • I've just upgraded to Visual Studio 2013 im getting the same error; R6010 – NoobProgrammer Mar 24 '14 at 17:58
  • @const_ref I've just upgraded to Visual Studio 2013 im getting the same error; R6010 – NoobProgrammer Mar 24 '14 at 18:04
0

If you dont have C++11 for std::stoi but do have boost you could use lexical cast

#include <boost/lexical_cast.hpp>

int main() 
{
    std::string s = "100";
    try 
    {
        int n = boost::lexical_cast<int>(s);
        std::cout << "n = " << n << std::endl;
    } 
    catch (boost::bad_lexical_cast) 
    {
        std::cout << "conversion failed" << std::endl;
    }
}

This ensures a valid conversion can take place and throws an exception if it cannot

Regarding your Edit - This requires a C++11 Compiler to work

std::string str;

for(int i = 0; i < numberofvalues; i++)
{   

    str = cmemblock[i];
    //std::stol(str); -> This line is unneeded as it converts string to a long
    // Ideally you should check that str is valid here etc. before changing it to an int
    int number = std::stoi(str);
    cout << number;


}
const_ref
  • 4,016
  • 3
  • 23
  • 38
0

Another option is to use std::stringstream:

 #include <sstream>
 #include <string>

 int string_to_int(const std::string &string) {
     std::stringstream s(string);
     s >> number;
     if (!s.good()) {
         throw std::exception();
     }
     return s;
 }

 int main(int argc, const char* argv[]) {
     int number = string_to_int(argv[1]);
     return 0;
 }

This doesn't require any external libraries or C++11, and should be compatible with any C++ compiler out there.

Edit

Fleshed out the example to show how you could write your own string_to_int function to simplify the use of std::stringstream.

Nathan
  • 4,777
  • 1
  • 28
  • 35