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:
#include <iostream>
usingnamespace 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];
}