You forgot to delete the memory, resulting in a memory leak.
1 2 3 4 5 6 7 8
cout << "enter a number" << endl;
//cin >> ptr[i];
string s = "123" ;
//cin >> s;
istringstream is(s);
getline(cin, s);
int iconv = atoi(s.c_str());
ptr[i] = iconv;
You're overcomplicating this. You can just read an integer directly from cin.
1 2
cout << "enter a number" << endl;
cin >> ptr[i];
1 2 3 4 5 6 7 8 9 10
++i;
cout << "enter -1 to exit loop or any number to continue ";
cin >> input;
}
// should be j< i to take out garbage value
for (unsigned j = 0; j <= i; ++j)
cout << "ptr[j] = " << ptr[j] << "\n";
cout << endl;
system("pause");
}
What happens when i is greater than or equal to 2?
i have changed the code now and it works , and it even works when i > = 2 , it looks like that the size of the array is changing dynamically , at least from what i understand.
# include <iostream>
# include <vector>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
/*
unsigned const size = 3;
int array[size];
for (unsigned i = 0; i < size ; ++i)
{
cout << " enter a number" << endl;
cin >> array[i];
cout << array[i];
}*/
unsigned size = 2;
int * ptr = new int[size];
int input;
cin >> input;
int i = 0;
while (input != -1)
{
cout << "enter a number" << endl;
//cin >> ptr[i];
//string s = "123";
////cin >> s;
//istringstream is(s);
//getline(cin, s);
//int iconv = atoi(s.c_str());
//ptr[i] = iconv;
//getline(cin, ptr[i]);
cin >> ptr[i];
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.clear();
//cin.getline( )
++i;
cout << "enter -1 to exit loop or any number to continue ";
cin >> input;
}
// should be j< i to take out garbage value
for (unsigned j = 0; j <= i; ++j)
cout << "ptr[j] = " << ptr[j] << "\n";
cout << endl;
system("pause");
}
i am still perplexed why this did not work out :
cout << "enter a number" << endl;
//cin >> ptr[i];
string s = "123" ;
//cin >> s;
istringstream is(s);
getline(cin, s);
int iconv = atoi(s.c_str());
ptr[i] = iconv;
Well your snippet in question still has lines of code that don't do anything. For example you never use the stringstream so there is no need for that line. But other than that there is nothing wrong with that snippet, as long as the variable 'i' never exceeds the size of the array. I suggest you get rid of everything not related to the snippet and try again making sure you stay within the bounds of your array. Something like the following:
i don't understand why i has to be within the bounds of size. this code even works when i is greater than size.
You can only access [0, size) of an array because that's what C++ has allocated for that array. Anything outside the range maybe be other parts of your program, which you do not have permission to read/write to. Doing so will cause undefined behaviour, meaning we don't know what will happen but it's usually something not good.
You just got lucky and your program didn't crash, but don't rely on this type of behaviour. There's a reason why it's called undefined.