Why can't I copy a string pointed by a pointer to an array of char

Hi and help,

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.

1
2
3
4
5
6
7
private:
        StatusPanel &statusPanel;
        char allowedProductName[MAX_NAME_LENGTH];
        DeliveryChute &deliveryChute;
        Product *products[MAX_PRODUCTS];
        unsigned productCount;
        unsigned productPriceCents;




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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(const char *productName) const
{
    // Done: Implementing 
    const char FIRST_PRODUCT[] = "Pepsi";
    const char SEC_PRODUCT[] = "Coke";
    const char 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
        return true;
    }
    else
        return false;
}
`Does not work' is not and error message
How about
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Project1::ProductRack::isCompatibleProduct(const string& productName)
{
    const char* FIRST_PRODUCT = "Pepsi";
    const char* SEC_PRODUCT = "Coke";
    const char* THIRD_PRODUCT = "7 Up";
    if(productName == FIRST_PRODUCT || productName == SEC_PRODUCT || productName == THIRD_PRODUCT)
    {
        allowedProductName = productName;
        return true;
    }
    else
        return false;
}


(given string allowedProductName;of course)
Last edited on
@Cubbi, he already tried that method and said it did not work which is kinda surprising to me because that looks like it should work
Cubbi,

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.

So I still is looking for a solution.
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.
Last edited on
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

strcpy(allowedProductName, productName);

or else use

strncpy(allowedProductName, productName, sizeof(allowedProductName));
Kot4Kot4,

You are right the method is a const as shown:
bool
Project1::ProductRack::isCompatibleProduct(const char *productName) const

This means I cannot modify my private variables. And the specification does not allow me to add other method to access the private.

I guess my instructor wants us to think.
Just in case I missed to mention the method
bool
Project1::ProductRack::isCompatibleProduct(const char *productName) const

and the variable char allowedProductName[MAX_NAME_LENGTH];

are component of a class ProductRack.
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.
Topic archived. No new replies allowed.