i can't find the problem

can any one help me
every time i come a cross an example in this tutorial i type it in to my compiler instead of copying and pasting to help me remember it better but when i tryed to copy the one in dynamic memory chapter a run time glich acurrs i compared the two thurlly and i can't see a differants i was hoping it might be more obviouse too you
thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <new>
using namespace std;
int main(){
int i,n;
int *p;
cout<<"how many numbers would you like to remember:";
cin>>i;
p= new (nothrow) int [i];
if (p == 0) cout<<"either you enterd 0 or that\n number can't be allocated..";
else {
     for (n=0;n<i;n++){
         cout<<"\nenter number:";
         cin>>p[n];
         }
         cout<<"you have enterd:";
         for(n=0;n<i;n++){cout<<p[n]<<',';
             delete []p;}
     }
     cout<<"\n\n";
 
     system ("pause");
}

i would apreciate if any one can see the problem
Last edited on
1
2
      for(n=0;n<i;n++){cout<<p[n]<<',';
             delete []p;}


Format it nicely and you will see the prob.
e.g
1
2
3
4
for(n=0;n<i;n++) {
 cout<< p[n] << ',';
 delete []p; // Shouldn't be inside the loop.
}


Solution:
1
2
3
4
5
for(n=0;n<i;n++) {
 cout<< p[n] << ',';
}

delete []p; // Shouldn't be inside the loop. 
Topic archived. No new replies allowed.