user3283997
user3283997

Reputation:

Converting char to int in C++

I'm trying to convert a character from a c string to an int but I keep running into an error.

Here's my code

while(std::getline(file, line)){
            if(std::isdigit(line[0]) && std::isspace(line[1]) && std::isdigit(line[2])){
                SequenceArray.push_back(line);
                if(std::stoi(line[2])== (SequenceArray.size() -1)){
                    std::cout<< "Success" << std::endl;

The error that I keep getting is as follows:

 a1.cpp: In function ‘int main(int, char**)’:
a1.cpp:30:25: error: call of overloaded ‘stoi(char&)’ is ambiguous
     if(std::stoi(line[2])== (SequenceArray.size() -1)){
                         ^
a1.cpp:30:25: note: candidates are:
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a1.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2823:3: note: int std::stoi(const string&, std::size_t*, int) <near match>
   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
   ^
/usr/include/c++/4.8/bits/basic_string.h:2823:3: note:   no known conversion for argument 1 from ‘char’ to ‘const string& {aka const std::basic_string<char>&}’
/usr/include/c++/4.8/bits/basic_string.h:2926:3: note: int std::stoi(const wstring&, std::size_t*, int) <near match>
   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
   ^
/usr/include/c++/4.8/bits/basic_string.h:2926:3: note:   no known conversion for argument 1 from ‘char’ to ‘const wstring& {aka const std::basic_string<wchar_t>&}’
a1.cpp:35:6: warning: label ‘std’ defined but not used [-Wunused-label]
      std:exit(EXIT_FAILURE);

Upvotes: 0

Views: 2655

Answers (4)

madduci
madduci

Reputation: 2883

A char implicit converts to a int, you don't need to use extra functions.

'a' = 97, 'b' = 98, 'c'=99, etc., following the ASCII table

So if you write,

char a_char = 'a';
int a_val = a_char;
cout << a_val << endl;  

you have:

97

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254411

The first error is because you haven't enabled C++11 support. GCC currently chooses C++03 by default, and stoi didn't exist in that version.

Add -std=c++11 to the compiler's arguments. If that doesn't work, try -std=c++0x, and think about getting a more up-to-date compiler. If you're stuck with an ancient compiler, then use atoi as in the code you originally posted (or perhaps something involving strtol, if you want to detect errors).

Also make sure you've included <string> for the declaration of that function.

The second error is because you wrote : instead of ::.

Upvotes: 0

M.M
M.M

Reputation: 141534

For std::stoi missing, try #include <string> (and enable C++11). However see also this thread - the Windows ports of g++ have had a long-standing issue with support of stoi and to_string.

The second error is that std:exit should be std::exit.

The third error is because of line[2].c_str(). You have not told us what line is but the error message suggests it is a std::string. So line[2] is a char and char does not have any member functions. If you explain what you are trying to do in the code std::atoi(line[2].c_str()) someone will be able to help. Maybe you meant line[2] - '0' which will give an integer between 0 and 9 if the third character in the line was a digit.

Upvotes: 1

emvee
emvee

Reputation: 4449

std::stoi() is C++11. Not all compilers enable C++11 by default.

Upvotes: 0

Related Questions