Syntax issue with structs

Hello all,

This is my first assignment using structs and I am having a minor syntax issue. I am currently trying to hard code the first six entries of an inventory into my program but cannot assign values to the strings of the struct. Heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	
typedef struct Store{
	char Brand[ArrSize];
	char Object[ArrSize];
	int ItemNumber;
	int Quantity;
	double Cost;
	double Price;
}Inv;

        Inv Catalog[ArrSize];

        Catalog[0].ItemNumber=101;
	Catalog[0].Brand
	Catalog[0].Object
	Catalog[0].Quantity=4;
	Catalog[0].Cost=39.99;
	Catalog[0].Price=59.99;


I dont have any issues assigning the values of the other data types in the struct, just the strings. Thanks!
It looks like you're writing in C and not C++, is that right?

In that case, use the strcpy function.
strcpy(Catalog[0].Brand, "BrandName");
You'll also need to make sure the array is big enough to hold what you are copying in.
No, its c++. I was just about to delete the thread, I cannot believe I did not remember the string commands had to be used. Thank you for the quick reply though. I'm not sure why we are using structs instead of classes, is that why you asked about C/C++?
No, its c++. I was just about to delete the thread, I cannot believe I did not remember the string commands had to be used. Thank you for the quick reply though. I'm not sure why we are using structs instead of classes, is that why you asked about C/C++?
If it was C++ I would just use std::string instead of worrying about char arrays. Then just write
Catalog[0].Brand = "BrandName";
You won't have to worry about size issues, either, among other things.
I'm not sure why we are using structs instead of classes, is that why you asked about C/C++?


2 things make it look C-ish:

1) fixed size char arrays
2) C style struct typedef.

structs are fine in C++.. they're just usually not declared that way. Something like this would be more C++ish:

1
2
3
4
5
6
7
8
9
struct Inv
{
	std::string Brand;
	std::string Object;
	int ItemNumber;
	int Quantity;
	double Cost;
	double Price;
};
Topic archived. No new replies allowed.