views:

619

answers:

4

Hi, I am getting an error with this code. 'Incompatible types in assignment of char to char[13]' I can't figure out how to initialize these arrays and make this work. Basically, the program takes ISBN codes (4 groups of integers and makes one string with '-' in them between each group of numbers) and verifies that they are correct. The program uses a class ISBN and a main function that loads the actual ISBN codes and tries to use the class ISBN to test them. Here is what I have.

class ISBN {
private:
char group[6];                          
char publisher[8];                     
char book[8];                      
char check;  
char isbn[13];
char compute_check();

public:
ISBN();
ISBN(char newisbn[]);             
ISBN(char group[ ], char publisher[ ], char book[ ], char check);                                       
bool valid();                           
char *getpublisher();                  
void print(ostream &o);                 
};

ISBN::ISBN(char newisbn[]) : isbn(newisbn) {}

The program loads these ISBN numbers and then prints and tests them using the class ISBN in the following way...

strcpy(isbns[index++], "1-57676-074-X");
ISBN isbn(isbns[i]);
isbn.print(cout);
if (isbn.valid())

I'm having trouble converting the ISBN codes into the ISBN class so that they can be operated on by each of these functions. Any help much appreciated! Thanks!

+5  A: 

This:

ISBN::ISBN(char newisbn[]) : isbn(newisbn) {}

doesn't do what you want. Despite what you may have been told, arrays are not identical with pointers - the constructor here is taking a pointer (disguised as an array) and trying to use it to initialise an actual array. You need:

ISBN::ISBN(char newisbn[]) {
   strcpy( isbn, newisbn );
}

I would also suggest investigating the std::string class for your general string-processing needs.

anon
Thank you Neil, that is exactly what I needed to know!
Spencer
+2  A: 

ISBN::ISBN(char newisbn[]) : isbn(newisbn) {}

You cannot initialize char[13] by char*.

You have to manually copy, character-by-character:

ISBN::ISBN(char newisbn[]) { strcpy(isbn, newisbn); }

Pavel Radzivilovsky
A: 

The straight-forward way to store strings in C++ is really only

const char* group = "Group";

If you need extra string functionality look into the string class.

honk
that's not very safe you know. It's resereved in the static memory section (correct me if I am mistaking with the name) and every person with an hex editor can see the "Group" string. Don't do that.
the_drow
@the_drow 1) `group` could be set at run-time. 2) But so what? That's not what `private` is about.
honk
1) no it cannot, it's const.2) What private? this has nothing to do with OOP.
the_drow
@the_drow you are confusing `const char*` and `const char[]`.
honk
A: 

In C/C++, you can treat an array as just a pointer to the first element of the array. So use pointers in your constructor parameters, not arrays. E.g.

ISBN::ISBN(const char* newisbn)
{
    strcpy(isbn, newisbn);
}
Andy Johnson