I have a problem: the following code debugs and runs without problems (Dev-C++). But when I fill in the amount of numbers the user wants to know the median of (line 11), the program crashes, and Windows "tries to find a solution for the problem". The goal of the program is to calculate the median of previous entered integers. Does anyone know the solution?
Thanks in advance!
#include <iostream>
usingnamespace std;
int main()
{
int * pChoice = NULL, int * pCounter = NULL, int * pTotal = NULL, int * pNumbers = NULL;
float * pAverage = NULL;
cout << "Welcome! You are going to enter some integers, and then I calculate the median!\n";
cout << "Enter the amount of integers you want to know the median of: ";
cin >> *pChoice;
pNumbers = newint[*pChoice];
for(*pCounter; *pCounter < *pChoice; *pCounter++)
{
cout << "Enter an integer: ";
cin >> pNumbers[*pCounter];
}
*pCounter = 0;
for(*pCounter; *pCounter < *pChoice; *pCounter++)
{
*pTotal += pNumbers[*pCounter];
}
delete [] pNumbers;
*pAverage = *pTotal / *pChoice;
cout << "The average is " << *pAverage << ". Good bye!";
cin.get();
cin.get();
}
(I wanted to do everything with pointers, so don't think I'm a pointer addict xD)
I'm surprised line 7 compiles. The first use of int on that line should be enough.
You should not dereference a pointer if it doesn't point to anything. Only pNumbers points to something. It looks like pChoice, pCounter, pTotal and pAverage don't actually have to be pointers. Only use pointers if it's necessary.