Aoa,
Hello All,
i am having problem in this question
Write a program that takes integer input from the user and store into the array dynamically allocated each time a new element is added. Your program should prompt user to take integers until he enters -1, which means end of input. After taking all the numbers print them in the reverse order as entered by the user.
the code which i have written is attached along.
it is printing junk values.
need help :)
regards
code goes here
#include <iostream>
using namespace std;
void question1();
int main()
{
question1();
}
void question1()
{
int i=0;
int* ptr=nullptr;
int* temp=nullptr;
bool boolean=true;
while(boolean!=false)
{
cout << "Enter number: " << endl;
int* ptr=new int[i+1];
cin >> ptr[i];
if(ptr[i]!=-1)
{
temp = new int[i+2];
for(int k=0; k<i; k++)
{temp[k]=ptr[k];}
i++;
}
else
{
for(int j=i-1; j>=0; j--)
{
cout << temp[j] << " " ;
}
boolean = false;
}
}
delete[]ptr;
delete[]temp;
}