As part of a homework problem, I'm trying to declare a new struct within the definition, but my new definition is an array, and I am only to define some of the values. For example:
#define STUDENTS 100
struct Test { int score; int grade;
} score[STUDENTS], ???
but I want to only initialize the first 5. Thanks!
On Nov 8, 3:06 am, crystal twix <jonwongfanc...@gmail.com> wrote:
> As part of a homework problem, I'm trying to declare a new struct > within the definition, but my new definition is an array, and I am > only to define some of the values. For example:
> #define STUDENTS 100
> struct Test { > int score; > int grade;
> } score[STUDENTS], ???
> but I want to only initialize the first 5. Thanks!
Hi Because it is a homework, I should give you just few hints: 1. Use const types rather than macros: const int STUDENTS = 100; 2. The name of your array and one of its struct members are same: score. use different names. 3. You can intialize the first 5 array elements using array initilizers list almost in the same way as initializing array of integers: int a[100] = { 0, 1, 2, 3, 4 };
On Sun, 2009-11-08, crystal twix wrote: > As part of a homework problem, I'm trying to declare a new struct > within the definition, but my new definition is an array, and I am > only to define some of the values. For example:
> #define STUDENTS 100
> struct Test { > int score; > int grade; > } score[STUDENTS], ???
> but I want to only initialize the first 5. Thanks!
I assume your homework is a C++ assignment since you are posting here. Use std::vector<Test> instead of a C-style array, and make Test a class with a constructor.
(And if your teacher is not aware of the standard containers, quit the class and buy a good C++ book instead.)
/Jorgen
-- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .