I am using visual studio running a win32 console application.
I'm new to dynamic memory and i am trying to think of some extra things for an easy assingment. Basically I am trying to allow an input for the array size. I know this requires dynamic memory and i have it set up for that. I get an error after the program finishes with
" Windows has triggered a breakpoint in (my program).
This may be due to a corruption of the heap, which indicates a bug in (my program) or any of the DLLs it has loaded."
It then brings me to a breakpoint in the ostream, if i run it with 2 as my input, it gives me a different error.
Here's my code.
Code:
#include <iostream>
using namespace std;
// ------------------Function Prototypes -----------------------------
void EnterData (int *grades, int,int&);
void OutputData (int *grades, int,int);
//--------------------------------------------------------------------
void main()
{
int x;
int *grades;
cout<<"How many students would you like to calculate? ";
cin>>x;;
grades=new int(x);
int total=0;
int NUMSTUDENTS=x;
// enter the grades information
EnterData (grades,NUMSTUDENTS, total);
// display the grades information
OutputData (grades,NUMSTUDENTS,total);
delete grades;
system ("pause");
}// end main
//--------------- Function definitions to follow --------------------------
void EnterData(int *grades, int NUMSTUDENTS, int &total)
/* This function allows the user to enter the grades information into the data array.
Pre: The grades array has been allocated sufficient memory
Post: The grades entered by the user are returned within the array */
{
int studentNumber;
cout<<"Please enter the grades for the "<<NUMSTUDENTS<<" students...\n\n";
for (studentNumber = 0; studentNumber<NUMSTUDENTS; studentNumber++)
{
cout<<"Enter grade #"<<studentNumber+1<<": ";//counter, inputs the data
cin>>grades[studentNumber];//into each array component starting at zero
total+=grades[studentNumber];//total for finding average
}
}// end EnterData
//--------------------------------------------------------------------------
void OutputData(int *grades, int NUMSTUDENTS, int total)
/* This function displays the grades to the user
Pre: The grades array has already been filled with data
Post: The grades are displayed to the user */
{
int studentNumber;
cout<<"The grades for the "<<NUMSTUDENTS<<" students are:\n\n";
for (studentNumber = 0; studentNumber<NUMSTUDENTS; studentNumber++)
{
cout<<"Student #"<<studentNumber+1<<": ";
cout<<grades[studentNumber]<<endl;
}
cout<<"The average is: "<<double((total/NUMSTUDENTS))<<endl;
}// end OutputData
Thanx for any help. Hope its not to easy.