Source Code Problem

Hi all!

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace 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 = new int[*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)
Last edited on
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.
Last edited on
You are trying to store a value at zero address because pChoice is equal to NULL. So this statement

cin >> *pChoice;

and others that follow it are incorrect.
Last edited on
Ah, thanks!
@peter87: those lines indeed don't compile, i changed them later in the post, without checking them :)
Topic archived. No new replies allowed.