Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ array of pointers returns the same value for each element
    text
    copied!<p>I wrote a simplified version of the code I'm working on to illustrate a problem I'm having. I think something is wrong in <code>main()</code> that causes the <code>showUsers()</code> method to output the same Login/Password combo for every element (always the last one added).</p> <h3>Here is my code:</h3> <pre><code>#include &lt;iostream&gt; using namespace std; //============================================================================= class AccountInfo { private: char* _username; char* _password; public: AccountInfo(); AccountInfo(char* username, char* password); ~AccountInfo(); void setUsername(char* username); void setPassword(char* password); char* getUsername(); char* getPassword(); friend ostream&amp; operator&lt;&lt;(ostream&amp; out, AccountInfo&amp; x) { out &lt;&lt; "Login: " &lt;&lt; x.getUsername() &lt;&lt; endl &lt;&lt; "Password: " &lt;&lt; x.getPassword() &lt;&lt; endl; return out; } }; AccountInfo::AccountInfo() { _username = ""; _password = ""; } AccountInfo::AccountInfo(char* username, char* password) { _username = username; _password = password; } void AccountInfo::setUsername(char* username) { _username = username; } void AccountInfo::setPassword(char* password) { _password = password; } char* AccountInfo::getUsername() { return _username; } char* AccountInfo::getPassword() { return _password; } //============================================================================= class UsersDB { private: int _size; AccountInfo* _accounts[200]; public: UsersDB(); ~UsersDB(); int getSize(); void addUser(AccountInfo* newUser); void showUsers(); }; UsersDB::UsersDB() { _size = 0; } UsersDB::~UsersDB() { delete[] _accounts; } int UsersDB::getSize() { return _size; } void UsersDB::addUser(AccountInfo* newUser) { _accounts[_size] = newUser; _size++; } void UsersDB::showUsers() { for (int i = 0; i &lt; _size; i++) { cout &lt;&lt; *_accounts[i] &lt;&lt; endl; } } //---------------------------------------------------------emptyString function void emptyString(char* token, int size) { for (int i=0; i &lt; size; i++) token[i] = '\0'; } //----------------------------------------------------------copyString function void copyString (char* from, char* to, int size) { to = new char[size+1]; for (int i=0; i &lt; size; i++) to[i] = from[i]; to[size] = '\0'; } //--------------------------------------------------------getNextToken function int getNextToken(char* buffer, char* token, int startPos, int bufSize, int tokenSize, char delimeter) { int i, j; emptyString (token, tokenSize); i = startPos; j = 0; while ((buffer[i] == ' ') &amp;&amp; (i &lt; bufSize)) i++; //skipblanks if (i &lt; 256) { while ((buffer[i] != delimeter) &amp;&amp; (i &lt; 256) &amp;&amp; (j &lt; tokenSize)) token[j++] = buffer[i++]; } return i; } //============================================================================= int main() { char buffer[256]; char userLoginName[9]; char password[17]; int i, j, k; char flag[3];; char command[11]; char blank = ' '; UsersDB* users = new UsersDB(); AccountInfo* tempAccount; while (!cin.eof()) { //while end of line is not reached cin.getline(buffer, 256); k = getNextToken(buffer, command, 0, 256, 10, blank); if (command[0] == 'a') { tempAccount = new AccountInfo(); k = getNextToken(buffer, userLoginName, k, 256, 8, blank); (*tempAccount).setUsername(userLoginName); k = getNextToken(buffer, flag, k, 256, 2, blank); if (flag[1] == 'p') { k = getNextToken(buffer, password, k, 256, 16, blank); (*tempAccount).setPassword(password); } cout &lt;&lt; *tempAccount &lt;&lt; endl; (*users).addUser(tempAccount); } else if (command[0] == 's') { (*users).showUsers(); } else cout &lt;&lt; "Command not found." &lt;&lt; endl; } return 0; } </code></pre> <h3>The output looks like this:</h3> <pre>=============================================================================== >adduser bob -p password1 Login: bob Password: password1 >adduser jack -p mypassword Login: jack Password: mypassword >adduser jill -p pass1234 Login: jill Password: pass1234 >showusers Login: jill Password: pass1234 Login: jill Password: pass1234 Login: jill Password: pass1234 =============================================================================== </pre> <h3>The output SHOULD look like this:</h3> <pre>=============================================================================== >adduser bob -p password1 Login: bob Password: password1 >adduser jack -p mypassword Login: jack Password: mypassword >adduser jill -p pass1234 Login: jill Password: pass1234 >showusers Login: bob Password: password1 Login: jack Password: mypassword Login: jill Password: pass1234 =============================================================================== </pre> <p>Note: When I alter <code>main()</code> (by passing the info directly instead of getting it from the console using <code>cin</code>) to look like this:</p> <pre>//============================================================================= int main() { UsersDB* users = new UsersDB(); AccountInfo* tempAccount; tempAccount = new AccountInfo("jack", "mypassword"); (*users).addUser(tempAccount); tempAccount = new AccountInfo("jill", "pass1234"); (*users).addUser(tempAccount); (*users).showUsers(); return 0; }</pre> <p>...I get the desired output.</p> <p>Thanks a lot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload