jeudi 11 février 2021

Why I am not able to access string individual chars using index

string strMap[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
//string digits received as parameter in function // digits = "234";

Accessing it as below.

int digitmapIndex = digits[index] - 46;   //index is some int 0,1 etc..
int mapstr_len = strMap[digitmapIndex];

or

int mapstr_len = strMap[digits[index] - 46];

Error: Line 25: Char 13: error: no viable conversion from 'std::string' (aka 'basic_string') to 'int' int mapstr_len = strMap[digits[index] - 46]; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:816:7: note: candidate function operator __sv_type() const noexcept

Aucun commentaire:

Enregistrer un commentaire