+ 8
std::stoi() requires a constant std::string reference as first argument, here you have given it a `char`, due to the use of subscript (index) operator. You can also play with the digits ASCII value, given you are sure all the characters in the std::string were digit (0 ~ 9). for( size_t i = 0; i < L; i++) { arr[ i ] = Num[ i ] - '0'; }
15th Feb 2022, 10:59 AM
Ipang
+ 3
Oh your question is trending today wow 😲👌 I'd have answered but i didn't see this till now. Next time if you post a question tag me or message me it's link I'll answer :)
16th Feb 2022, 11:08 AM
Rishi
Rishi - avatar
+ 2
15th Feb 2022, 11:05 AM
Ipang
+ 2
The error happens because you pass a char to std::stoi() which were expecting a constant std::string reference. It's not because <string> header not explicitly included.
15th Feb 2022, 11:25 AM
Ipang
+ 2
string Num="1234"; In the previous code, you get each character from the string variable <Num> by using subscript operator []; you then pass the character as argument for std::stoi(). But that didn't work because std::stoi() was expecting a constant string reference, whereas a char was given instead. arr[i] = stoi(Num[i]); Num[0] = '1' Num[1] = '2' Num[2] = '3' Num[3] = '4' In the recent code, you use an array of string, thus each element are a string themselves, and passing them as argument to std::stoi() is fine. It's the type of argument that was expected by std::stoi(). (Edited)
15th Feb 2022, 11:36 AM
Ipang
+ 2
Yes that's right Manav Roy, std::string str {"Hello"}; str[0] = 'H' str[1] = 'e' str[2] = 'l' str[3] = 'l' str[4] = 'o' And each of them are char ...
15th Feb 2022, 12:02 PM
Ipang
+ 2
Contd: string s(1,Num[i]); is creating a string with constructor , argument 1 represents number of length and is filled with Num[i] charecter ex: string s(3,'4'); //this creates a string 444 so now you can pass to stoi(), like cout<<stoi(s); There are other ways also.. it's equal to string s ; s += Num[i]; ex2: s += 'a'; //this makes s="a"; string from char 'a'
15th Feb 2022, 1:46 PM
Jayakrishna 🇮🇳
+ 1
#include <iostream> using namespace std; int main() { string Num="1234abcde"; int arr; size_t ss;//unsigned integer datatype used to represent the size of objects in bytes, //here ss used as pointer to store position of argument after the last digit taken for convertion arr=stoi(Num,&ss,10); //10 base represents base type of number in string, which is going to be extracted from strong to convert to int. cout<<arr<<endl; //output: 1234 cout<<Num.substr(ss)<<endl; //output: abcde //in case, you want specify base type but you dont want pointer position then you can use nullptr cout<<stoi("112",nullptr,2); //output:3 (11 in base 2 => 3 in base 10) //because cout<<stoi("112",10) cout<<stoi("112",&ss); valid cout<<stoi("112",nullptr); valid but equalto cout<<stoi("112"); return 0; } edit:
15th Feb 2022, 1:18 PM
Jayakrishna 🇮🇳
+ 1
What you want is string.substr(). Num[i] gives you char, stoi needs string. Num.substr(i,1) gives you string. Why use char only to convert back to string. https://code.sololearn.com/cyqC7o57xw45/?ref=app
16th Feb 2022, 4:58 PM
Bob_Li
Bob_Li - avatar
0
You can do it without loop just in one line. Error is because the first argument to stoi() function is expect a string. But you are giving a character. Syntax : stoi( string, position, int base); position is from where to start convertion Base is type to convert,default base 10 (decimal) #include <iostream> using namespace std; int main() { string Num="1234"; //int L=Num.length(); int arr; //for(int i=0;i<L;i++) { arr=stoi(Num); } cout<<arr; return 0; } edit: Manav Roy '1' is a character (single quotes) where as "1" is a string (using of double quotes) Your first code loop, from string Num[i] extracts character, and 2nd code loop, from string of array, Num[I] extracts a string. so its fine to stoi(), but first not.
15th Feb 2022, 11:37 AM
Jayakrishna 🇮🇳
0
Then convert char to string and use it like: #include <iostream> using namespace std; int main() { string Num="1234"; int L=Num.length(); int arr[L]; for(int i=0;i<L;i++) { string s(1,Num[i]); arr[i]=stoi(s); cout<<arr[i]; } return 0; }
15th Feb 2022, 11:58 AM
Jayakrishna 🇮🇳
0
hello guys can anyone help me
17th Feb 2022, 7:55 AM
DEVELOPER TJ
DEVELOPER TJ - avatar