I
I
iRumba2015-01-22 06:43:29
Programming
iRumba, 2015-01-22 06:43:29

Why is extra information written to char (C++)?

So there is a structure

struct userInfo{
  char uCardNum[16];
  char uPIN[4];
  float uBalance;
};

There is a class that manages a collection of such structures. It has an add procedure
void add(char CN[16],char PIN[4],float Balance){
    userInfo ui;
    strcpy(ui.uCardNum,CN);
    strcpy(ui.uPIN,PIN);
    ui.uBalance=Balance;
    users.push_back(ui);
    printf(ui.uCardNum);
  };

Everything is simple, we create a structure, fill it with data, put it in vector users
. But not only the card number is recorded in uCardNum, but also the pin code is added. That is,
printf(ui.uCardNum);
displays the card number with a pincode (for example, I send the card number 1111111111111111 and the pincode 2222, the output will be 1111111111111112222) ((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SHVV, 2015-01-22
@SHVV

In zybyli place under the terminal zero. Add one character to each line.

M
maaGames, 2015-01-22
@maaGames

Actually, the functions "strcpy(ui.uCardNum,CN); strcpy(ui.uPIN,PIN);" do not work correctly for you and can lead to the crash of the program due to damage to the stack. In debugging, the compiler can even swear at this. These strings copy characters from one string to another until a null terminator is encountered in the source string, and you don't have a null terminator anywhere. Theoretically, at least gigabytes of garbage can be copied in these places ...
As already mentioned, you either need to do a null terminator at the end of the lines (but this will increase the size of each line by one character), or output these lines not as a string, but through a format string , character by character.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question