i'm working on my class project in data structure class. this is my code:
#include <iostream>
#include <string>
using namespace std;
main ()
{
string * pn;
long int n;
cout<<"Product Number: ";
cin>>n;
pn= new (nothrow) string [10];
pn[1]="Aluminum Gold Engraved\n";
pn[2]="Aluminum Silver Engraved\n";
pn[3]="Brass\n";
pn[4]="Stainless Steel\n";
pn[5]="Acrylic Imprint\n";
pn[6]="Acrylic Engraved\n";
pn[7]="Mirrorized\n";
pn[8]="Impact\n";
pn[9]="Magnet\n";
pn[10]="US Pin\n";
cout<<pn[n];
delete [n] pn;
}
Whenever i put memory allocation number 10, the program crashes or debugging error. what should i do?
You made an array of size 10. Here is a list of the elements in that array:
pn[0]
pn[1]
pn[2]
pn[3]
pn[4]
pn[5]
pn[6]
pn[7]
pn[8]
pn[9]
Do you see the mistake you made?
As an aside, this delete [n] pn;
should be delete [] pn;
Last edited on
Programmers always count from zero. ;-)
@ Moschops.. Thanks! big help.. =)
@iHutch 105.. I'll remember that. =D