using answer of question for a struct

I'm trying to use a anwer of a question as input for a struct:\

This is the first struct:

struct Product *CreateProduct(char *name, char *company, char *properties, int dateOnMarket[3], unsigned int inProduction) {

struct Product *what = new Product;
assert(what != NULL);

what->name = _strdup(name);
what->company = _strdup(company);
what->properties = _strdup(properties);
what->dateOnMarket[3] = dateOnMarket[3];
what->inProduction = inProduction;

return what;

}
In the main function I would like to use the anwer on a question for this struct, ie:

printf("What is the name of the product?: ");

struct Product *data = CreateProduct("NAME" ,"", "", , );

Is there a way the answer of the question is used as input for NAME?

In C++, you don't need to say struct other than when you're defining it. Nowhere in your code above do you need to say struct.

This being C++, use proper C++ strings.

struct Product *CreateProduct(string name, char *company, char *properties, int dateOnMarket[3], unsigned int inProduction) (and likewise for other char arrays, and don't use strdup)

what->dateOnMarket[3] = dateOnMarket[3];
This doesn't do what you think it does. This attempts to copy the 4th element in an array (which does not exist) over another 4th element in an array (which also does not exist), so a total disaster.

Anyway,
1
2
3
4
cout << "What is the name of the product? ";
string productName;
cin >> productName;
Product* data = CreateProduct(productName ,"", "", , );


I also see no value here in creating the object using new. Why are you doing that?
Last edited on
Topic archived. No new replies allowed.