So, my assignment is to take in a student ID, first and last name, and 5 test scores.
I am then to average the test scores, collect all of that data into an array and output all of the information in a tabular format(which I have yet to do).
I have much of the bulk programming done, but I'm struggling with two specific things. Averaging the data for ALL students and pulling the data out of an array to print it on screen.
Here's my code so far:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int SCORES = 5;
const int MAX_SIZE = 25;
struct Student
{
int studentId;
string firstName;
string lastName;
int testScores[SCORES];
};
bool getStudent(Student& stu);
void printData(Student list[], int count);
double calcAverage(Student& stu);
int main()
{
Student stu;
Student list[MAX_SIZE];
int index = 0;
calcAverage(stu);
bool done = false;
while(!done && index <= MAX_SIZE)
{
done = getStudent(stu);
if(!done)
{
list[index] = stu;
index++;
}
}
system("cls");
printData(list, index);
system("pause");
return EXIT_SUCCESS;
}
bool getStudent(Student& stu)
{
cout << "=======================New Student=======================" << endl;
cout << "!!When finished adding data use CNTRL-Z to clear & print!!\n" << endl;
cout << "Enter student ID: ";
cin >> stu.studentId;
if(cin.eof() || cin.fail())
return true;
cin.ignore();
cout << "Enter student's first name: ";
getline(cin, stu.firstName);
cout << "Enter student's last name: ";
getline(cin, stu.lastName);
cout << "Please enter 5 test scores (seperate with spaces): ";
for(int i = 0; i < SCORES; i++)
cin >> stu.testScores[i];
return false;
}
double calcAverage(Student& stu)
{
//avg = (stu.testScores[0] + stu.testScores[1] + stu.testScores[2]
//+ stu.testScores[3] + stu.testScores[4]) / 5.0;
}
void printData(Student list[], int count)
{
for(int i = 0; i < count; i++)
{
cout << list[i].studentId << " ";
cout << list[i].firstName << " ";
cout << list[i].lastName << " ";
cout << list[i].testScores << " ";
//cout << list[i].avg << " ";
}
}
Notice how I commented out the entire body of the calcAverage function; this is because it only calculates the average of 5 test scores of ONE student, not ALL students individually. The next problem I have lies within "cout << list.testScores << " ";" How would I print this data? Would I just form a for loop to output everything inside testScores[]?
I'm pretty sure the answers will be simple and I am just having a brain block atm.
Thanks so much!