0

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);
  • I can't be the only one to think that the compiler's translation of the code's `line[2]` into the message's `line.std::basic_string<_CharT, _Traits, _Alloc>::operator[], std::allocator >(2ul)` is insane. – molbdnilo Feb 05 '15 at 13:18
  • @molbdnilo g++ 4.9 made big improvements in its error messages over g++ 4.8 – M.M Feb 05 '15 at 13:20

4 Answers4

3

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
madduci
  • 2,635
  • 1
  • 32
  • 51
1

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.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • @Clockwork again `line[2]` is a `char` so you cannot call `stoi` on it. `char` and `string` are different. A char is not just a short string. If `line[2] - '0'` is not what you want then please explain in words what you want that piece of code to do , as it is not clear. – M.M Feb 05 '15 at 13:16
  • How do I convert a char to an int in that case. It is the number being retrieved by getline( ) as a char –  Feb 05 '15 at 13:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70305/discussion-between-clockwork-and-matt-mcnabb). –  Feb 05 '15 at 13:19
0

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 ::.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • @Clockwork: As I said, add `-std=c++11` to the compiler arguments. In your case, add it to the definition of `CXXFLAGS` in the makefile. – Mike Seymour Feb 05 '15 at 13:07
0

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

emvee
  • 4,371
  • 23
  • 23