Your for loop is slightly odd. You've created a pointer, pointed it to the memory address of the first element of prGroup, and then you're checking if that memory address is less than the memory address of prGroup[size]. I assume your professor wrote this loop?
Anyway, you can easily gather the test scores within the getInfo function, the loop is already made for you. You'd just add a few extra lines (this code assumes 4 test score per student):
1 2 3 4 5
for(int i = 0; i < NUM_TESTS; i++)
{
cout << "Test Score " << i << ": ";
std::cin >> ptr->testScores[i];
}
This loop would go inside the already existing one.
EDIT
@Ganado On line 43, he sets the pointer to prGroup.
I am just stuck on creating a dynamic array for the structure
This is too out of context to know what you mean :( Glad you go it going.
Btw, some of your code is very reminiscent of C, C++ has plenty of features that would make what you're doing much easier and simpler. If this is for your own practice, you should try to get some experience with making this program using more updated methods.
That is true, for this program i just need to get dynamic arrays working so I can modify the array size whenever I enter an input and it can show the right amount of users to enter the data for.
You could have static array, but use only part of it:
1 2 3 4 5 6
constint MAX_INFO = 42;
studentInfo student[MAX_INFO];
int info = 0;
std::cin >> info;
if ( MAX_INFO < info ) info = MAX_INFO;
getInfo( student, info );
For dynamic array, default to vector: std::vector<studentInfo> student = getInfo();
If you really have to do it manually, then you should manage the deallocation too:
1 2 3 4 5
int info = 0;
std::cin >> info;
studentInfo* student = new studentInfo[ info ];
getInfo( student, info );
delete [] student;
I tried it your way, only thing is, it messes up my for loop and skips the student name entry.
I did list four different approaches. Hard to say what you did (wrong).
My changes were in main(). Only the vector-based version requires changes into functions. In other words, my solutions (if correctly implemented) cannot possibly mess up your loops. Either your loops were messed up to begin with, or you failed to apply the idea to your code.
Lets look at your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void getInfo(studentInfo [], int);
int main()
{
double *ARRAY_SIZE = nullptr; // ARRAY_SIZE is a pointer
int dynamic = 0;
cin >> dynamic;
ARRAY_SIZE = newdouble[dynamic]; // ARRAY_SIZE points to dynamic array of double values
studentInfo student[ARRAY_SIZE]; // static array of studentInfo values
// error: pointer is not integer
// error: size of static array must be constant integer
getInfo(student,ARRAY_SIZE); // error: function expects int, ARRAY_SIZE is pointer
I was trying to point the dynamic array of double values to the structure so I could create a dynamic structure that can be modified like a regular dynamic array and I can either choose to input data for one student only or four.
How would I go about altering the amount of students I would like to enter data for. For example: I enter "1" because I only want to enter data for one student or "3" for three separate students and not rely on a fixed number for an array size?
cin >> size;
vector<type> name(size); //pretty much like type name[size].
name[0] = first...
name[1] = second... just like an array.
for pointers.
cin >> size;
type *name = new type[size];
name[0] = first;... etc
if you need to change the sizes later, the pointer needs a do-over, you make a new bigger one and copy everything into it, or use C's realloc. Vectors can simply be extended using push_back(oops_needed_one_more_nth_input) or other ways easily.
constint MAX_INFO = 4; // this is decided before you compile the program
studentInfo student[MAX_INFO]; // array of MAX_INFO students
vs dynamic array:
1 2 3
int MAX_INFO = 0;
std::cin >> MAX_INFO; // user decides number of students when the program runs
studentInfo* student = new studentInfo[MAX_INFO]; // array of MAX_INFO students
And the "we have room for MAX_INFO students, but only N did show up":
1 2 3
int N = 0;
std::cin >> N; // actual students
if ( MAX_INFO < N ) // more than expected, what to do?
A difference between static and dynamic array is that the size of static is set in stone, but you can replace a dynamic array while the program runs.
This latter point is not important for you, because you receive the count of students before the students arrive. You need to create array only once and it will be of "right size" (unless the user lies*).