0

I have a piece of code:

#include <bits/stdc++.h>
using namespace std;

int main() {
//ios_base::sync_with_stdio(false);
string s[5];

s[0] = "Hello";
s[1] = "12345";

cout << s[0] << " " << s[1] << "\n"; 
cout << s[0][0] << " " << s[1][1] << "\n";

int y = stoi(s[1]);          //This does not show an error
cout <<"y is "<< y << "\n";
//int x = stoi(s[1][1]);       //This shows error
//cout <<"x is "<< x << "\n";
return 0;
}

The output of this code is:

Hello 12345  
H 2  
y is 12345

But it shows an error when I uncomment

int x = stoi(s[1][0]);
cout <<"x is "<< x << "\n";

If in both the cases a string is being converted to int using stoi() function then why do the later part of code gives an error?
I have tried the same using atoi(s[1][0].c_str()) but it also gives an error.

What is the alternative for this, if I want to convert the second type of elements to int?

sarthakgupta072
  • 451
  • 6
  • 13
  • 1
    What according to you `s[1][0]` refers to? – Aditi Rawat Dec 17 '17 at 19:11
  • It should be a string element I guess. @AditiRawat – sarthakgupta072 Dec 17 '17 at 19:12
  • 2
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Dec 17 '17 at 19:13
  • 1
    As for your problem, `s[1]` is a `std::string`, and `s[1][0]` is a single *character* in the string `s[1]`. There's no overload of [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) which takes a single character. – Some programmer dude Dec 17 '17 at 19:14
  • Umm let me put it this way.. `char s [2]` is an array of 2 elements...`char arr [3][2]` is array of 3 elements of type `char [2]`. It is 2nd dimension. – Aditi Rawat Dec 17 '17 at 19:17
  • If it's a `character` then either of `atoi()` or `atoi(s[1][0])` should work, but they are also also not working. :( What may be the alternative for this problem? @Someprogrammerdude – sarthakgupta072 Dec 17 '17 at 19:22
  • No, all the functions are for converting ***strings*** containing numbers. – Some programmer dude Dec 17 '17 at 19:27
  • 1
    It is not solution with stoi but maybe also useful try to int y = s[1][0] - '0'; to convert ascii number to int – Nick S Dec 17 '17 at 19:35
  • @NickS -- `s[1][0] - '0'` converts a decimal digit to the corresponding integer value **regardless of the character encoding**. So it's true that it works for ASCII, but there's no need to assume ASCII. – Pete Becker Dec 17 '17 at 20:09

2 Answers2

0

s[1] is a std::string, so s[1][0] is a single char in that string.

Calling std::stoi() with a char as input doesn't work because it takes only a std::string as input, and std::string doesn't have a constructor that takes just a single char as input.

To do what you are attempting, you need to do this instead:

int x = stoi(string(1, s[1][0]));

Or

int x = stoi(string(&(s[1][0]), 1));

Your call to atoi() doesn't work because you are trying to call c_str() on a single char instead of the std::string it belongs to, eg:

int x = atoi(s[1].c_str());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So, this cannot be implemented using atoi()? `int x = atoi(s[1][0])` is also not working? – sarthakgupta072 Dec 17 '17 at 19:32
  • Yes, atoi works fine. `int x = atoi(&s[1][0])`. I was forgetting the "&". Thanks a lot, I learnt something today :) – sarthakgupta072 Dec 17 '17 at 19:52
  • @sarthak-sopho `atoi()` expects a null-terminated C-style string as input, you can't pass it a single `char` unless you copy it to a `char[2]` first, where `[0]` is the character and `[1]` is zero – Remy Lebeau Dec 18 '17 at 04:40
-1

stoi has as input a string not a char. Try this:

string str(s[0][0]);
int y = stoi(str);
Vlad Isoc
  • 2,181
  • 1
  • 15
  • 15