Hi, I have this simple code:

const int kiCharSize=30;
const int kiDate=7;
struct Customer{
	char caName[kiCharSize];
	char caSurname[kiCharSize];
	char caCity[kiCharSize];
	char caPhone[kiCharSize];
	char caDate[kiDate];
	char cDay;
	enum eDay{M,A};
	char caBrand[kiCharSize];
};

blablabla

Customers::Customers(int x){
	switch(x){
		case 1:
			Cust.caBrand="HolyBrand";
			Cust.caCity="Heaven";
			Cust.caDate="110205";
			Cust.caName="Jeesus";
			Cust.caPhone="999333999";
			Cust.caSurname="HolySpirit";
			Cust.cDay='M';
			break;
		case 2:
			Cust.caBrand="Brandutsu";
			Cust.caCity="Japan";
			Cust.caDate="110205";
			Cust.caName="Naruto";
			Cust.caPhone="123456789";
			Cust.caSurname="Uzumaki";
			Cust.cDay='P';
			break;
		case 3:
			Cust.caBrand="DeadSoDead";
			Cust.caCity="Graveyard";
			Cust.caDate="110206";
			Cust.caName="Undertaker";
			Cust.caPhone="000000000";
			Cust.caSurname="Grimreaper";
			Cust.cDay='M';
			break;}

}

And getting this simple error:

Error 1 error C2440: '=' : cannot convert from 'const char [10]' to 'char [30]'

For all the lines.
So how Do I assign a name, a string to a char[kiSize] Declared before?
THank you.

As Nandomo suggested, you need to use strncpy. You are using the char arrays as C strings, and as such, they use a different method than normal char arrays, or even strings.

It would be easier if you just changed all the types (chars) to string. So you'd have

struct Customer{
	string caName[kiCharSize];
	string caSurname[kiCharSize];
	string caCity[kiCharSize];
	string caPhone[kiCharSize];
	string caDate[kiDate];
	string cDay;
	string eDay{M,A};
	string caBrand[kiCharSize];
};

// instead of what you currently have:

struct Customer{
	char caName[kiCharSize];
	char caSurname[kiCharSize];
	char caCity[kiCharSize];
	char caPhone[kiCharSize];
	char caDate[kiDate];
	char cDay;
	enum eDay{M,A};
	char caBrand[kiCharSize];
};

By changing from C string to string, you can keep your assignments in your constructor the same and still be able to manipulate the string using element access like you do in arrays.. Guessing as how you are using this as a class work, or book work, you will be likely be sticking with the char type.

The char type, as you have written, is a C string. As such, to equate a C string array to the desired string, you need to use the C string function:

strncpy, which takes 3 arguments.
strncpy(Variable, string, array_limit);
example:
Cust.caBrand="HolyBrand"; would be changed to

strncpy(caBrand, "HolyBrand", kiCharSize);

And seeing how this is implemented in a CLASS CONSTRUCTOR, you do not need the dot (.) operator.

The most likely of times when you need a dot (.) operator in a class member function is when you passed an OBJECT as a PARAMETER that is NOT the CALLING OBJECT.

As such,

class Customer(
public: 
void getvalue();
void setvalue(Customer& one){
value = one.value;
// new.value = one.value   as new is the calling object and one is the passed object
}

private:
<some variables>
};

int main() {

Customer new, old;

new.getvalue();    // no need for dot (.) operator as new is the CALLING OBJECT.
new.setvalue(old);  
// You will use the dot (.) operator as old is an OBJECT that is being passed as a PARAMETER used in the member function 

return 0;
}

A "string literal" is a constant pointer to char. You can't assign a constant pointer to a non-constant pointer. That's a conversion that allows you to edit a constant using a "backdoor".

You will need to use functions from the <cstring> header, such as strncpy(), to perform the assignment so that you aren't attempting to perform an illegal conversion on the pointer.

As Nandomo suggested, you need to use strncpy. You are using the char arrays as C strings, and as such, they use a different method than normal char arrays, or even strings.

It would be easier if you just changed all the types (chars) to string. So you'd have

struct Customer{
	string caName[kiCharSize];
	string caSurname[kiCharSize];
	string caCity[kiCharSize];
	string caPhone[kiCharSize];
	string caDate[kiDate];
	string cDay;
	string eDay{M,A};
	string caBrand[kiCharSize];
};

// instead of what you currently have:

struct Customer{
	char caName[kiCharSize];
	char caSurname[kiCharSize];
	char caCity[kiCharSize];
	char caPhone[kiCharSize];
	char caDate[kiDate];
	char cDay;
	enum eDay{M,A};
	char caBrand[kiCharSize];
};

By changing from C string to string, you can keep your assignments in your constructor the same and still be able to manipulate the string using element access like you do in arrays.. Guessing as how you are using this as a class work, or book work, you will be likely be sticking with the char type.

The char type, as you have written, is a C string. As such, to equate a C string array to the desired string, you need to use the C string function:

strncpy, which takes 3 arguments.
strncpy(Variable, string, array_limit);
example:
Cust.caBrand="HolyBrand"; would be changed to

strncpy(caBrand, "HolyBrand", kiCharSize);

And seeing how this is implemented in a CLASS CONSTRUCTOR, you do not need the dot (.) operator.

The most likely of times when you need a dot (.) operator in a class member function is when you passed an OBJECT as a PARAMETER that is NOT the CALLING OBJECT.

As such,

class Customer(
public: 
void getvalue();
void setvalue(Customer& one){
value = one.value;
// new.value = one.value   as new is the calling object and one is the passed object
}

private:
<some variables>
};

int main() {

Customer new, old;

new.getvalue();    // no need for dot (.) operator as new is the CALLING OBJECT.
new.setvalue(old);  
// You will use the dot (.) operator as old is an OBJECT that is being passed as a PARAMETER used in the member function 

return 0;
}

Let the guy do a little bit of research, don't ruin all the fun ^.^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.