Problem: Course Grade Modification
Write a program that uses a structure to store the following information:
Name, idNum, tests, avg, and grade

I got the most of the program completed so far, however I still need to do the average and grade, if anyone can assist me I would greatly appreciate it and can you explain what I did wrong and how your input resolved it. I am new at this, thanks!

Here's my code:

#include <iostream>
#include <string>
using namespace std;

struct Student
{
	string name,
		   grade;
	int idNum,
	    *tests;
	double	avg;
};

int main()
{
	int numStudents,
		numTests;
	double total = 0;
	
	//Get Number of Students
	cout << "How many students will be entered? ";
	cin >> numStudents;
	//Get Number of Tests
	cout << "How many test scores are there? ";
	cin >> numTests;

	Student * student;
	student = new Student[numStudents];

	//Get Student Names
	for (int i = 0; i < numStudents; i++)
	{
		cout << endl;
		cout << "Enter Student Name: ";
		cin.ignore();
		getline(cin, (student + i)->name);

		//Get Student ID#
		cout << "Enter Student ID Number: ";
		cin >> (student + i)->idNum;

		(student + i)->tests = new int[numTests];

		//Get Test Scores
		for (int t = 0; t < numTests; t++)
		{
			cout << "Enter Test Score " << t + 1 << ": ";
			cin >> (student + i)->tests[t];

			//Validate Test entries
			while((student + i)->tests[t] < 0)
			{
				cout << "You must enter a value higher than zero.\n";
				cout << "Enter Test Score " << t + 1 << ": ";
				cin >> (student + i)->tests[t];
			}

			//Add all scores together
			for (int count = 0; count < (student + i)->tests[t]; count++)	
			{		
				total += student[count];
			}

			//Get Average Score
			student->avg = total / numTests;
		}
	}

	//Display output of information entered
	for (int s = 0; s < numStudents; s++)
		{
			cout << endl;
			cout << "Student Name: " << (student + s)->name << endl;   
			cout << "Student ID Number: " << (student + s)->idNum << endl;

			for (int t = 0; t < numTests; t++)
			{
				cout << "Test " << t + 1 << " score: " << (student + s)->tests[t] << endl;
			}

			cout << "The Average score for " << (student + s)->name << " is: "
				<< student->avg << endl;
		}

	//Deletes Memory Locations
	for (int i = 0; i < numStudents; i++)
		delete [] student[i].tests;

	delete [] student;

	return 0;
}

For some reason my average calculates the same no matter how many students I enter. It also doesnt look correct in the output screen. After I get this working I need to figure out how to output a grade level.
IE: 91-100 = A, 81-90 = B, etc.

I'd recommend using an std::vector to store you students. You'd want to use a set of if/else if statements or a switch statement to map the numerical grades to letter grades.

Dave

This article has been dead for over six months. Start a new discussion instead.