C++ cout string print its memory value -


i trying create keygen reversing challenges around there. decided try code in language don't know well, c++

i not sure how use pointers yet, thing implemented algorithm without them. problem print answer it's memory location instead string.

char decrypt(char c, int position); int main(){      cout << "enter username:" << endl;     string username;     string answer[20] = "";     cin >> username;     (int = username.length(); > 0; i--){         answer[username.length() - i] = decrypt(username[i-1],i);         if (i == 0){             answer[username.length() +1] = '\0';         }     }     return 0; } char decrypt(char c, int position) {     if (position == 4){          return '-'; }     if (c > 'm'){         c -= 17;    }     else{         c += 21;    }     c ^= position;     c ^= 2;     return c; } 

if try print string username string , not memory location of username. i'm not sure going on.. help, or

first, try use answer variable string , not char*. string authomatically resize , realocates internal buffer if needed.

the code looks this:

string username; string answer; cin >> username; (int = 0; < username.length(); i++) {     answer += decrypt(username[i],i+1); } 

then if want see content of internal string buffer, can use answer.c_str ();

edit:

as songyuanyao said, code uses array of string. solution of using array of 20 chars (char answer [20]) leads memory issue if username has size of 20 or more.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -