int getFormat(){
while(true){
cout << "What movie format would you like to rent?\n" <<
"1. VHS\n" <<
"2. DVD\n" <<
"3. Blu-ray\n";
cin >> format;
switch(format){
case 1:
cout << "You have chosen to rent a VHS.\n";
formatChar = "VHS";
return 1;
case 2:
cout << "You have chosen to rent a DVD.\n";
return 2;
case 3:
cout << "You have chosen to rent a Blu-ray.\n";
return 3;
default:
cout << "You have not selected a valid choice.\n";
break;
}
}
}
I have formatChar declared as "char formatChar[15];" earlier in the struct. I want to set the array to the format selected before returning the number of the format. However, it returns an error:
C:\CodeBlocks\Exercises\Chapter 4\exercise4.13.cpp|34|error: incompatible types in assignment of `const char[4]' to `char[15]'|
I sort of understand what this means, but I don't know how to fix it. (Also, I know strings would probably be a better choice, but I'm using a book and it hasn't really taught strings yet so I want to stick to the basic data types.)
You can't just assign arrays. You have to copy their elements one by one. In the case of char arrays, there already exists a function to do that: strcpy(formatChar,"VHS");
This is the least annoying of the reasons why using std::string is encouraged. Just wait until you get to memory management.
Yeah, I can easily how strings are preferred over char arrays. However, it feels better to keep in the context of the book and wait until later to learn the easy way :D
Also, I love this forum. I haven't been on many forums about anything where the other members were so helpful and prompt in responding. Every single question I've asked on here has been answered well. It's really a great place.