run time error life, universe.....

i get a run time error

#include<iostream>
using namespace std;
int main()
{
	int x,*a;
	int i=1;
	do
	{
		cin>>x;
		a[i]=x;
		i++;
	}while(x!=42);
int j;
j=i;
i=1;
while(i<(j-1))
{
	cout<<a[i]<<endl;
	i++;
}
}
1 Like

Just do this

#include<iostream>
using namespace std;
int main()
{
    int x,a[100000]; //<--- this
    int i=1;
    do
    {
        cin>>x;
        a[i]=x;
        i++;
    }while(x!=42);
int j;
j=i;
i=1;
while(i<(j-1))
{
    cout<<a[i]<<endl;
    i++;
}
}
1 Like

Are you getting NZEC error? You have to return 0.
Also i would recommend printing the number as soon as you read it instead of storing it to save memory.

1 Like

Yes, you can do it either way, but printing after reading is much better than storing it, i just pointed out the problem in his method :slight_smile: