質問

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?

役に立ちましたか?

解決

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

他のヒント

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;


}

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top