Creating a dynamic array for a structure
Mar 19, 2020 at 11:03pm UTC
I would appreciate some advice in creating a dynamic array for my structure so I can manually choose how many students I want to input data for. Here is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include<iostream>
#include<iomanip>
#include<cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;
const int NUM_TESTS = 4;
const int SIZE = 40;
struct studentInfo
{
char studentName[SIZE];
char idNum[SIZE];
char testScores[NUM_TESTS];
double average;
char grade;
};
void getInfo(studentInfo [], int );
void showInfo(studentInfo [], int );
int main()
{
double *ARRAY_SIZE = nullptr ; //Local double pointer to dynamically allocate an array
int dynamic = 0;
cout << " Course Grade Program" << endl;
cout << "========================================\n" << endl;
cout << "How many students: " ;
cin >> dynamic;
ARRAY_SIZE = new double [dynamic]; //Dynamically allocates an array large enough to hold both strings and names
studentInfo student[ARRAY_SIZE];
getInfo(student,ARRAY_SIZE);
showInfo(student,ARRAY_SIZE);
delete [] ARRAY_SIZE; //Deallocates array
ARRAY_SIZE = nullptr ; //Makes ARRAY_SIZE a null pointer
return 0;
}
void getInfo(studentInfo prGroup[], int size)
{
studentInfo* ptr;
int index = 0;
int testScore = 0;
int convert;
for (ptr = prGroup; ptr < &prGroup[size]; ptr++)
{
cout << "Student name#" << index + 1 << ": " ;
cin.getline(ptr->studentName, SIZE);
cout << "ID Number: " ;
cin.getline(ptr->idNum, SIZE);
for (int numCount = 0; numCount < NUM_TESTS; numCount++)
{
cout << " Test #" << numCount + 1 << ": " ;
cin.getline (ptr->testScores, NUM_TESTS);
int e = atoi(ptr->testScores);
ptr->average += e/2;
/*if(ptr->grade >= 91 || ptr->grade <= 100)
{
ptr->grade = 'A';
}
if(ptr->grade >= 91 || ptr->grade <= 90)
{
ptr->grade = 'B';
}
if(ptr->grade >= 71 || ptr->grade <= 80)
{
ptr->grade = 'C';
}
if(ptr->grade >= 61 || ptr->grade <= 70)
{
ptr->grade = 'D';
} */
}
cout << endl;
index++;
}
}
void showInfo(studentInfo prGroup[], int size)
{
studentInfo* ptr;
// Prints ID number, name, and salary for 5 programmers.
cout << "------ Course Grade Report: --------------\n" ;
// for(int index = 0; index < MAX_PROG; index++)
for (ptr = prGroup; ptr < &prGroup[size]; ptr++)
{
cout << "Student Name: " << ptr->studentName << endl;
cout << "ID Number: " << ptr->idNum << endl;
cout << "Average Test Score: " << ptr->average << endl;
cout << "Grade: " << ptr->grade << endl;
cout << "-------------------------------------------------\n" ;
}
}
Mar 19, 2020 at 11:20pm UTC
Mar 19, 2020 at 11:21pm UTC
Will check it out thx
Topic archived. No new replies allowed.