I kept getting an error with this loop. If there are something i missed, please help. Thank You!
int main(){
string hasil;
int cod[5];
hasil = "99999";
for(int i = 0; i < 5; i++){
cod[i] = stoi(hasil[i]);
}
for(int i = 0; i < 5; i++){
cout << cod[i] + 1;
}
std::stoi()
takes a std::string
, not a char
. But std::string
does not have a constructor that takes only a single char
, which is why your code fails to compile.
Try one of these alternatives instead:
cod[i] = stoi(string(1, hasil[i]));
cod[i] = stoi(string(&hasil[i], 1));
string s;
s = hasil[i];
cod[i] = stoi(s);
char arr[2] = {hasil[i], '\0'};
cod[i] = stoi(arr);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments