you should copy the content of the point , try strcpy(pin,newPin)
c++ assigning a string parameter to a string
Domanda
I am trying to make a function that can change my account's pin number but having trouble finding the right way to assign the new one.
class Account;
class Account{
public:
int accountNumber;
char pin[5];
double balance;
void printInfo();
void changeBalance(int n, char * p, double b);
void changePin(int n, char * p, char * newPin);
};
void Account::changePin(int n, char * p, char * newPin)
{
if((n == accountNumber) && (strcmp(p, pin) == 0))
{
//pin = newPin; //ERROR HERE
}
}
am I supposed to not use an equal sign or use some function/pointer to assign a new pin? I am fairly new to c++ so I am still trying to figure out how to properly declare/assign things.
Soluzione
Altri suggerimenti
Never mind the class, the problem is how to assign a value to an array.
You can copy an array one element at a time:
for(unsigned int k=0; k<5; ++k)
pin[k] = newPin[k];
You can take advantage of the fact that this is a char array, and use strncpy
:
strncpy(pin, newPin, 5);
Or when you're tired of messing around with char[]
you can look into std::string
.
Try with this:
if(p!=NULL)
{
strcpy(this.pin,p);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow