I’m new to C++ and I can’t figure out why my code is losing the value for username and password. Any help would be appreciated. TIA
Here is the code...
Client *client = new Client();
char *authBuffer = new char[MAX_BUFFER_SIZE]; // buffer contains 234567|john|pwd4john
bufferToClient(authBuffer, client);
printf("id=%i username=%s password=%sn", client->id, client->username, client->password);
// output is: id=234567 username= password=
// function
void bufferToClient(char *buffer, Client *client) {
memset(client, 0x00, CLIENT_STRUCT_SIZE);
char *token = strtok(buffer, PIPE); // pipe |
client->id = atoi(token);
client->username = strtok(0, PIPE);
client->password = strtok(0, PIPE);
printf("%in", client->id);
printf("%sn", client->username);
printf("%sn", client->password);
// everything is ok and prints out the values as expected
// 234567
// john
// pwd4john
delete token;
}
// struct
struct Client {
unsigned int id;
const char *username;
const char *password;
virtual ~Client() {
delete username;
delete password;
}
};
I’m sure it must be something simple but I just can’t figure out why it keeps dropping the values for username and password. At one point I had username coming back ok but password was was all messed up.
New contributor
XRP9X club is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.