1
string phone_nb = "173";
char just_one_char = phone_nb[1];
int i_just_one_char = stoi(just_one_char);

I get these errors:

no matching function for call to 'stoi'
int i_just_one_char = stoi(just_one_char);

note: candidate function not viable: no known conversion from 'char' to 'const std::__1::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') for 1st argument
_LIBCPP_FUNC_VIS int                stoi  (const string& __str, size_t* __idx = 0, int __base = 10);

note: candidate function not viable: no known conversion from 'char' to 'const std::__1::wstring' (aka 'const basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >') for 1st argument
_LIBCPP_FUNC_VIS int                stoi  (const wstring& __str, size_t* __idx = 0, int __base = 10);
0

2 Answers 2

4

You can try something like this:

#include <iostream>
#include <string>

int main() {
    std::string phone_nb = "173";
    int i_just_one_char = phone_nb[0] - '0';
    std::cout << i_just_one_char;
}

Which works by relying on how characters/ASCII is structured to convert from a character to its integer equivalent.

The above code will output a 1.

Sign up to request clarification or add additional context in comments.

2 Comments

C++ guarantees that it only supports character encodings where the numbers have have codes that are contiguous and ascending in order. What you have works in ASCII or any other supported encoding.
Just make sure the 1st char is actually between '0'..'9', inclusive, before subtracting '0' from it, otherwise you will get the wrong result.
0

The easiest fix for your code would be to just use atoi instead of stoi You can modify your code like this:

string phone_nb = "173";
char just_one_char_str[2];
just_one_char_str[0] = phone_nb[1];
just_one_char_str[1] = '\0';
int i_just_one_char = atoi(just_one_char);

You can read up more on atoi() here

Alternative

As mentioned by @RemyLebeau, one could also use the substr() function and get a similar result or could also use the answer by @Rietty

string phone_nb = "173";
string just_one_char = phone_nb.substr(0, 1);
int i_just_one_char = stoi(just_one_char);

6 Comments

You could use string just_one_char = phone_nb.substr(0, 1); int i_just_one_char = stoi(just_one_char); instead, but that is just overkill. Rietty's answer is more efficient.
Well, just use phone_nb.c_str() and you are okay to go. I don't think this is such a bad solution
@ranu that would parse the entire string, not just the first character, like the OP wants.
@RemyLebeau, that's quite easy to fix. It doesn't seem to be a bad thing still.
@ranu to fix this, you would have to do something like char temp[2]; temp[0] = just_one_char; temp[1] = '\0'; int i = atoi(temp);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.