-1

I want to convert a string to int in C++, the below code is printing the last digit of the sum of the two numbers. I know I can do it with other different ways but I am curious why the below code is showing an error.

Code:

        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        string ans = to_string(sum);
        cout<< atoi(ans[ans.length()-1])<<endl;

Error:


prog.cpp: In function 'int main()':
prog.cpp:12:40: error: invalid conversion from '__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}' to 'const char*' [-fpermissive]
         cout<< atoi(ans[ans.length()-1])<<endl;
                                        ^
In file included from /usr/include/c++/5/cstdlib:72:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:47,
                 from prog.cpp:2:
/usr/include/stdlib.h:147:12: note:   initializing argument 1 of 'int atoi(const char*)'
 extern int atoi (const char *__nptr)

I have used both STOI and ATOI but I am still showing this error.

Can you please tell me why this error is originating and how to solve it.

Bill P
  • 3,622
  • 10
  • 20
  • 32
Ayush Pant
  • 31
  • 1
  • 6
  • atoi takes in a const char*, not a char. – Harrand Sep 30 '19 at 07:37
  • 1
    Why don't you just do: std::cout << std::stoi(ans) – jignatius Sep 30 '19 at 07:39
  • 1
    You don't need to convert the last digit to a number in order to print it. (And you don't need to convert a number to a string in order to extract its last digit,) – molbdnilo Sep 30 '19 at 07:39
  • You just have to remove the call to `atoi()` and just display directly the desired `char`: `std::cout << ans[ans.length()-1] << std::endl;` – Fareanor Sep 30 '19 at 07:42
  • @AyushPant See my answer. I don't think there's much need to use stoi or atoi here to be honest; you only want to convert one char to an int after all. – Harrand Sep 30 '19 at 07:51
  • Looking at the code there is no need to convert to a string at all. Just do this: cout << sum << endl; – jignatius Sep 30 '19 at 07:51
  • Avoid using `atoi` in any circumstance. It provides zero ability to validate the conversion and will happily return zero without any indication of an error if, e.g., you pass it `atoi ("gorilla");` – David C. Rankin Sep 30 '19 at 08:03
  • My question was that I wanted to know how to extract a specific char from a string and then convert it to an int and not the ASCII value of the char. – Ayush Pant Sep 30 '19 at 10:56

3 Answers3

1

You can use stringstream class of library sstreamas follows:

string s = "23031"; 

    stringstream test(s);
    int x = 0; 
    test>> x; 

    // Now the variable x holds the value 23031
    cout << "Value of x : " << x; 

in above piece of code x has integer value

0

From http://www.cplusplus.com/reference/cstdlib/atoi/

int atoi (const char * str);

The function atoi demands a const char*.

ans[ans.length()-1]

This retrieves the last element of the std::string ans, namely, a char. You get an error because you're giving a char to a function which requires a const char*. You can print out the last digit without needing to convert:

cout<< ans[ans.length()-1] <<endl;

Of course, this will print 0, because the last char of the string "30" is "0". If you really want it as an int, you can simply cast it or do the following to prevent '0' from printing 48:

int last_digit = ans[ans.length()-1] - '0';

You can find an existing explanation of this already: Convert char to int in C and C++

Harrand
  • 355
  • 6
  • 14
0

Atoi only receive const char in param. You can use stoi() instead:

cout<< stoi(ans)%10<<endl;
Thuc Nguyen
  • 235
  • 2
  • 8