L
learnfpga
Here is a little code I wrote to add the numbers input by the
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....
//trying to get input from the user to add all the numbers that user
inputs
//tried to do it without dynamic memory usage but probably cannot
achieve it
//here is using "new" and "delete" operator....
#include <iostream>
using namespace std;
int main()
{
int i,n,sum=0;;
int *p;
cout<<"How many numbers would you like to type ";
cin>>i;
p= new (nothrow) int;
if (p == 0) //to check the value returned by nothrow when allocating
memory
cout<<"Error in allocating memory ";
else
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
{
cout << p[n] << ", ";
sum=sum+(*(p+n));
}
cout <<"\nSum of numbers entered by you is "<<sum<<endl;
delete[] p;
return 0;
}
Thanks and cheers
user.....I was wondering if its possible to have the same functionality
without using dynamic arrays.....just curious.....
//trying to get input from the user to add all the numbers that user
inputs
//tried to do it without dynamic memory usage but probably cannot
achieve it
//here is using "new" and "delete" operator....
#include <iostream>
using namespace std;
int main()
{
int i,n,sum=0;;
int *p;
cout<<"How many numbers would you like to type ";
cin>>i;
p= new (nothrow) int;
if (p == 0) //to check the value returned by nothrow when allocating
memory
cout<<"Error in allocating memory ";
else
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
{
cout << p[n] << ", ";
sum=sum+(*(p+n));
}
cout <<"\nSum of numbers entered by you is "<<sum<<endl;
delete[] p;
return 0;
}
Thanks and cheers