我写了我正在处理的代码的简化版本,以说明我遇到的问题。我认为main()
中出现问题,导致showUsers()
方法为每个元素(总是添加的最后一个)输出相同的Login / Password组合。
这是我的代码:
#include <iostream>
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& operator<<(ostream& out, AccountInfo& x) {
out << "Login: " << x.getUsername() << endl
<< "Password: " << x.getPassword() << 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 < _size; i++) {
cout << *_accounts[i] << endl;
}
}
//---------------------------------------------------------emptyString function
void emptyString(char* token, int size) {
for (int i=0; i < 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 < 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] == ' ') && (i < bufSize)) i++; //skipblanks
if (i < 256) {
while ((buffer[i] != delimeter) && (i < 256) && (j < 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 << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
输出如下所示:
================================================== ============================
> adduser bob -p密码1
登录:bob
密码:password1
> adduser杰克-p mypassword
登录名:jack
密码:mypassword
> adduser吉尔-p pass1234
登录:jill
密码:pass1234
>显示用户
登录:jill
密码:pass1234
登录:jill
密码:pass1234
登录:jill
密码:pass1234
================================================== ============================
输出应如下所示:
================================================== ============================
> adduser bob -p密码1
登录:bob
密码:password1
> adduser杰克-p mypassword
登录名:jack
密码:mypassword
> adduser吉尔-p pass1234
登录:jill
密码:pass1234
>显示用户
登录:bob
密码:password1
登录:杰克
密码:mypassword
登录:jill
密码:pass1234
================================================== ============================
注意:当我更改
main()
时(通过直接传递信息而不是使用cin
从控制台获取信息)看起来像这样:// ================================================ ============================
int main(){
UsersDB *用户=新的UsersDB();
AccountInfo * tempAccount;
tempAccount = new AccountInfo(“ jack”,“ mypassword”);
(* users).addUser(tempAccount);
tempAccount = new AccountInfo(“ jill”,“ pass1234”);
(* users).addUser(tempAccount);
(* users).showUsers();
返回0;
}
...我得到所需的输出。
非常感谢。
最佳答案
您的问题是,您的UserAccounts(全部)最终都是指向main()中(单个)用户和密码字符数组的指针。
您可以通过每次使用这个主要函数创建一个新数组来修复它:
//=============================================================================
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 << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
请记住,之后要删除它们。这是修复该问题的最少代码,我在这里仅演示该问题。更好的解决方案是在构造时在UserAccount内创建新数组(因为似乎总是需要它们),并在dtor上删除它们,如下所示:
//At top of file:
#include <string.h>
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);
}