I am working on a program where I can select how many students I can enter data for, I am not familiar with vectors and am new when it comes to dynamic arrays within structures. Test scores has a fixed array size of 4 and is not changed since every student requires four test scores. I am just looking to dynamically allocate an array successfully and I think i should be able to do the rest.
I am trying to get an input like this:
How many students are you entering data for: 2
Student #1 Name:
ID:
Test Score #1
Test Score #2
Test Score #3
Test Score #4
-----------------------
Student #2 Name:
ID:
Test Score #1
Test Score #2
Test Score #3
Test Score #4
I do not see any problem with what you have except that "cstring" and "cstdlib" may not be needed or needed yet.
Line 43 is an extra closing brace that is not needed.
You have used "new' to create memory, but you need "delete" before the "return 0" to free the memory. Do not count on the program ending to free the memory.
With what you posted there it is difficult to test if "new" worked. I will have to work on that.
In your getInfo() function, you never set the value of ptr to anything. It's pointing to random memory. So when you try and put data into that random memory, you get undefined behaviour.
You're lucky that, in this case, it causes a crash.
In getInfo, ptr has never been initialized. When you try to dereference it (with the -> operator) your program crashes.
Also, since pointer points to an array, delete pointer should be delete[] pointer.
BTW, "pointer" is a bad name, only slightly better than "bag_o_bits". Yes it's a pointer, but it points to an array of student information. So maybe "students" would be better. And unless "prGroup" means something it could just be "students", too.
Maybe you mean something like this:
1 2 3 4 5 6 7 8 9 10 11
void getInfo(Student students[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "Student name#" << i + 1 << ": ";
cin >> students[i].name;
cout << "ID Number: ";
cin >> students[i].idNum;
}
}
how would I go about making the testScores variable work in the forloop? Since it has a fixed array size of four, because I would like it to output four test scores per student.
#include<iostream>
#include<iomanip>
//#include<cmath> // <--- Not used yet and may not be needed.
#include <string> // <--- Changed.
//#include <cctype> // <--- Not used right now.
#include <limits> // <--- Added.
usingnamespace std;
constint NUM_TESTS = 4;
struct Student
{
string name;
int idNum;
int testScores[NUM_TESTS];
double average;
char grade;
};
// Function prototypes
Student *initarrays(int);
void getInfo(Student prGroup[], int size);
void showInfo(Student student[], int size);
int main()
{
int numStudents; // Local integer variable that holds the number of students
Student *studentPtr; // Pointer that points to the student struc array
cout << " Course Grade Program\n";
cout << "========================================\n" << endl;
cout << "How many students: ";
cin >> numStudents;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
//cout << endl; // <--- To many blank lines
studentPtr = new Student[numStudents];
getInfo(studentPtr, numStudents);
showInfo(studentPtr, numStudents);
delete[] studentPtr;
return 0;
}
void getInfo(Student studentPtr[], int size)
{
Student* ptr;
int index = 0; // <--- Do not see the use of this variable?
for (ptr = studentPtr; ptr < &studentPtr[size]; ptr++)
{
cout << "\n Student name for #" << index + 1 << ": ";
std::getline(std::cin, ptr->name);
cout << " ID Number: ";
std::cin >> ptr->idNum;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
// Rest of input.
}
In the "getInfo" function I adjusted what you did previous. In the end dutch has a better solution that is much simpler and easier to work with.
With the "getInfo" function I used I got this output:
Course Grade Program
========================================
How many students: 1
Student name for #1: Nigel Smithe
ID Number: 123456
Enter test score 1: 95
Enter test score 2: 89
Enter test score 3: 96
Enter test score 4: 99
You entered:
Name: Nigel Smithe
ID: 123456
Test scores: 95, 89, 96, 99
Thank you for your insight Andy! You are always a great help as well, I will look into modifying and making your perspective fit my formatting style and also see where I can improve on.
# tests print is tied to index2 which is only set to zero outside the loops. if you want it zero for each outer loop go, then put index2=0; in the outer loop.
Course Grade Program
========================================
How many students: 2
Student #1 Name: Joe
ID Number: 1
Test #1: 1
Test #2: 1
Test #3: 1
Test #4: 1
Student #2 Name: Mama
ID Number: 2
Test #1: 2
Test #2: 2
Test #3: 2
Test #4: 2
------ Course Grade Report: --------------
Student Name: Joe
ID Number: 1
Average Test Score: 0
Grade:
-------------------------------------------------
Student Name: Mama
ID Number: 2
Average Test Score: 9.72658e-315
Grade:
-------------------------------------------------
Process returned 0 (0x0) execution time : 11.391 s
Press any key to continue.