Code help

Hi everybody, I'm new with c++ hence this post being in the beginners forums, but anyway, I've been working on a project for a while now, and I think the main problem is that I don't know how to declare an array using heap memory. Is it possible to do this? I'm not very good with pointers yet, but maybe a pointer to the array is what's needed here. The program is supposed to show a "seat chart" which displays 40 numbers in brackets known as seats. Then it makes the user enter multiple numbers to "reserve" seats. Then it should display the "seat chart" again except with x's in the places of the reserved seats. The array "dispreservedarray" goes out of scope after my last for loop. It's not quite finished yet, but you'll get my problem in displayreserved(). Without the output of dispreservedarray the program still works but it stops working after I input the first number. Can you explain this also? Here is the code:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

int seatChart (int array2[], int maxnumberofelements);
int reservearray (int array3[], int maxnumberofelements);

int main()
{
    int array[40];

    cout<<"SEAT CHART: \n\n";
    cout<<seatChart(array, 40);
    cout<<"\n\nThis program makes the user input a 'seat ID number' to be reserved.\n";
    cout<<"(Enter a negative number to quit and display a chart of available seats)\n\n";

    int secondarray[40];
    reservearray (secondarray, 40);
}

int seatChart (int array2[], int maxnumberofelements)
{
    for (int i = 0; i < maxnumberofelements; i++)
    {
        array2[i] = i;

        cout<<"["<<array2[i]<<"]";

        if (i == 7) {cout<<"\n";}
        if (i == 15) {cout<<"\n";}
        if (i == 23) {cout<<"\n";}
        if (i == 31) {cout<<"\n";}
        if (i == 3) {cout<<"\t\t";}
        if (i == 11) {cout<<"\t\t";}
        if (i == 19) {cout<<"\t";}
        if (i == 27) {cout<<"\t";}
        if (i == 35) {cout<<"\t";}
    }
}
int reservearray (int array3[], int maxnumberofelements)
{
    int numberofinputs;

    for (numberofinputs = 0; numberofinputs < maxnumberofelements; numberofinputs++)
    {
        cout<<"Please input a seat ID number to be reserved: ";

        int inputvalue;
        cin>>inputvalue;

        if (inputvalue < 0)
        {
            break;
        }
        array3[numberofinputs] = inputvalue;
        int dispreservedarray[numberofinputs];

        for (int x = 0; x < maxnumberofelements; x++)
        {
            dispreservedarray[x] = x;

            if (array3[numberofinputs] == dispreservedarray[x])
            {
                dispreservedarray[x] = 'x';
            }
        }
    }
    cout<<dispreservedarray[x];
}

Topic archived. No new replies allowed.