I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.
// There are only three product name, so if the parameter *productName is either Pepsi, Coke or 7 Up then the
// product is a valid product.
bool
Project1::ProductRack::isCompatibleProduct(constchar *productName) const
{
// Done: Implementing
constchar FIRST_PRODUCT[] = "Pepsi";
constchar SEC_PRODUCT[] = "Coke";
constchar THIRD_PRODUCT[] = "7 Up";
if (strcmp(productName, FIRST_PRODUCT ) || strcmp(productName, SEC_PRODUCT) || strcmp(productName, THIRD_PRODUCT))
{
//allowedProductName[] = productName; does not work
//allowedProductName = productName; does not work
//strcpy (&allowedProductName[0], productName); does not work
//strcpy(allowedProductName, productName); does not work
//strcpy(*allowedProductName, productName); does not work
returntrue;
}
elsereturnfalse;
}
I did not explain my question correctly. I cannot change the type of the
variable allowedProductName. It is a part of the specification by the instructor. and it is a private component of a class. so it stays
char allowedProductName[MAX_NAME_LENGTH];
ne555,
I was the one who put "does not work" to tell the reader that I already tried it and it did not work. All that are commented have been tried and failed to compile.
Do you have to have isCompatibleProduct a const function? because making not const would allow you to use strcpy.
or even better since the function returns a bool dont do the assignment in the function and if it returns true you do the assignment else you dont...
in short you cant do this because its a const function and const functions arent supposed to be allowed to edit the object thats calling it. they are read only if you will.
The problem you might be having is that MAX_NAME_LENGTH might not be big enough to contain all the characters(including null character) of productName.
Either you increase MAX_NAME_LENGTH considerably large enough and try
As the member function has const qualifier you can not change array allowedProductName. But you can do it if you will define the array as mutable data member.