Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that your UserAccounts (all of them) end up being pointers to the (single) user and password char arrays in main(). You can fix it by creating a new array each time with this main:</p> <pre><code>//============================================================================= int main() { char buffer[256]; char *userLoginName; char *password; 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') { userLoginName = new char[9]; password = new char[17]; 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> <p>Just remember to delete them afterwards. This is the least lines of code to fix it, which I'm showing just to demonstrate what the problem is. A better solution would be to instead create new arrays within the UserAccount on construction (since it seems you will always need them) and delete them on dtor like this:</p> <pre><code>//At top of file: #include &lt;string.h&gt; AccountInfo::AccountInfo() { _username = new char[9]; _password = new char[17]; } AccountInfo::AccountInfo(char* username, char* password) { strcpy(_username, username); strcpy(_password, password); } AccountInfo::~AccountInfo() { delete _username; delete _password; } void AccountInfo::setUsername(char* username) { strcpy(_username, username); } void AccountInfo::setPassword(char* password) { strcpy(_password, password); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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