2

I had a quick question regarding a programming assignment that i'm currently working on. We are currently working on Dynamically Allocated Arrays.

I was able to set my "Book" object to default values / name in my constructor (using strcpy):

Book::Book() 
{
 strcpy(title, " ");
 strcpy(author, " ");
 price = 0.00;
}

However, in another function that was given in the assignement (Set function) I was unable to use the strcpy to do the same for the following:

void Book::Set(const char* t, const char* a, Genre g, double p)
{
    strcpy(title, *t);
    strcpy(author, *a);
    type = g;
    price = p;
}

My question is, how would I be able to take the information coming through the first "Const char * t" parameter, and set it to my private data char array named title[31] ?

This is my member data for my "Book" class btw:

private:
  char title[31];   // may assume title is 30 characters or less
  char author[21];  // may assume author name is 20 characters or 
  Genre type;       // enum Genre
  double price;

Let me know if I need to clarify anything,

Thanks again!

2 Answers 2

3

when you have questions about std functions, you should read the man. The prototype of strcpy is:

char *strcpy(char *dest, const char *src);

So when you write: strcpy(title, *t); it wouldn't compile. You should write: strcpy(title, t);

One advice, when you write names of attrib in class, you should uses _ before or m_ or m like this: _title, m_title, mTitle. Just because it's dangerous to have variable with one letter name.

Another, in c++ you should use std::string cause it's easier to manipulate chains of caracters.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of

strcpy(title, *t);
strcpy(author, *a);

write

strcpy(title, t);
strcpy(author, a);

This is because strcpy is expecting a pointer, and *t is not a pointer to the string.

Have a look here http://www.cplusplus.com/reference/cstring/strcpy/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.