Access Violation Error

I think the problem is with my pointers but I cannot seem to find the

Page 1 of 1

2 Replies - 716 Views - Last Post: 28 July 2009 - 02:42 PM Rate Topic: -----

#1 Gaurang Patel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-July 09

Access Violation Error

Posted 28 July 2009 - 02:04 PM

I think the problem is with my pointers. Try to look in the getId_Scores function because that is where it messes up.
It compiles fine but runtime, it doesn't work so well. I hope you can help. It is supposed to be an access violation.

// Course Grade
// By Gaurang Patel
// ID: 0362602

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

// Structure
struct TestScores
{
	char Name[40];		// Student name
	int Idnum;			// Student ID number
	double *Tests;		// Pointer to an array of test scores
	double Average;		// Average test score
	char Grade;			// Course grade
};

// Function Prototypes
void getId_Scores(TestScores *, int, int);
void getAvgGrade(TestScores *, int, int);
void displayAll(TestScores *, int);

int main()
{
	// Declare variables
	int tests;				// How many tests
	int totalStudents;		// How many students
	TestScores *student;	// Student variable that will be dynamically allocated

	// Introduce program
	cout << "Course Grade\n";
	cout << "------------\n\n";
	cout << "Purpose: Assign a course grade and average based on the\n";
	cout << "number of tests. (Any number of tests and students)\n\n";

	// Ask for how many students and tests
	cout << "How many students and tests will there be? Enter each\n";
	cout << "in the order written above seperated by a space. ";
	cin  >> totalStudents >> tests;

	// Input Validation for negative #s
	while (totalStudents < 0 || tests < 0)
	{
		cout << "You entered a negative entry. Enter again. ";
		cin  >> totalStudents >> tests;
	}
		
	
	// Makes a structure variable and dynamically make an array of structures
	student = new TestScores[totalStudents];
	
	// Check for allocation error
 	if (student == NULL)
 	{
 		cout << "Error allocating memory!\n";
 		return 0;
 	}

	// Dynamically allocate an array of test scores to pointer Tests
	student->Tests = new double[tests];

	// Check for allocation error
 	if (student->Tests == NULL)
 	{
 		cout << "Error allocating memory!\n";
 		return 0;
 	}

	// Pass on 3 arguements. Function collects info
	getId_Scores(student, totalStudents, tests);

	// Get average and grade based on average
	getAvgGrade(student, totalStudents, tests);

	// Display everything in a formatted table
	displayAll(student, totalStudents);

	delete [] student;
	student = 0;

	return 0;
}


// Gets ID numbers, names, and test scores of every student
void getId_Scores(TestScores *student, int totalStudents, int tests)
{
	int count;		// Counter

	// Loop will get student names
	for (count = 0; count < totalStudents; count++)
	{
		cout << "Enter student " << count + 1 << "'s name. ";
		cin  >> student[count].Name;
	}

	// Loop will collect ID numbers of all students
	for (count = 0; count < totalStudents; count++)
	{
		cout << "Enter " << student[count].Name << "'s ID number: ";
		cin  >> student[count].Idnum;

		// Input Validation
		while (student[count].Idnum < 0)
		{
			cout << "You entered a negative ID number. Enter again. ";
			cin  >> student[count].Idnum;
		}
	}

	/* Loop will collect test scores of all students.
	Big loop for number of students and small loop for 
	number of tests. */
	for (int studNum = 0; studNum < totalStudents; studNum++)
	{
		for (int testNum = 0; testNum < tests; testNum++)
		{
			cout << "Enter " << student[studNum].Name << "'s score on test ";
			cout << testNum + 1 << ": ";
			cin  >> student[studNum].Tests[testNum];

			// Input Validation
			while (student[studNum].Tests[testNum] < 0)
			{
				cout << "You entered a negative test score. Enter again. ";
				cin  >> student[studNum].Tests[testNum];
			}
		}
	}
}


void getAvgGrade(TestScores *student, int totalStudents, int tests)
{
	double total = 0; // Running total

	// Loop calculates and stores average member in structure
	for (int count = 0; count < totalStudents; count++)
	{
		total = 0; // Reset total back to zero
		for (int index = 0; index < tests; index++)
		{
			total += student[count].Tests[index];
		}

		student[count].Average = (total / tests);
	}

	// Assign letter grade based on average
	char grade[5] = {'F', 'D', 'C', 'B', 'A' };

		for (int count = 0; count < totalStudents; count++)
		{
			if (student[count].Average <= 60)
				student[count].Grade = grade[0];
			else if (student[count].Average < 71)
				student[count].Grade = grade[1];
			else if (student[count].Average < 81)
				student[count].Grade = grade[2];
			else if (student[count].Average < 91)
				student[count].Grade = grade[3];
			else
				student[count].Grade = grade[4];
		}
}


// Display everything in a formatted table
void displayAll(TestScores *student, int totalStudents)
{
	cout << "\n\n\nName\tID Number\tAverage Test Score\tCourse Grade\t\n";
	cout << "----\t---------\t------------------\t------------\t\n\n";
	for (int count = 0; count < totalStudents; count++)
	{
		cout << student[count].Name << "\t" << student[count].Idnum << "\t";
		cout << student[count].Average << "\t" << student[count].Grade << "\t";
		cout << endl;
	}

}

This post has been edited by Gaurang Patel: 28 July 2009 - 02:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Access Violation Error

#2 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Access Violation Error

Posted 28 July 2009 - 02:35 PM

Each student needs a list of test scores, not just the first student!
for (i = 0; i < totalStudents; i++)
{
	// Dynamically allocate an array of test scores to pointer Tests
	student[i]->Tests = new double[tests];

	// Check for allocation error
//	if (student[i]->Tests == NULL)
	{
		cout << "Error allocating memory!\n";
		return 0;
	}
}


This post has been edited by wildgoose: 28 July 2009 - 02:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 Erik Drakken  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 24-July 09

Re: Access Violation Error

Posted 28 July 2009 - 02:42 PM

Be sure to replace -> with . in those statements, or you'll get errors. Tests is a pointer, but your student array is not, so the arrow is incorrect.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1