getline

i am using a getline method to get the values for the dynamic array. however when i print the output all the values of the dynamic array are zero ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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.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"); 
}  


??
1
2
unsigned size = 2;
int * ptr = new int[size];

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?

You also seem to be wanting to let the user enter an arbitrary amount of numbers. I'd suggest that you use a vector.
http://www.cplusplus.com/reference/vector/vector/

Please don't create a new post of the same problem 2 minutes after your previous one.
http://www.cplusplus.com/forum/beginner/217799/
Last edited on
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;

Last edited on
i am still perplexed why this did not work out :


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    size_t size = 2;
    int *ptr = new int[size];

    for(size_t i = 0; i < size; ++i)
    {
        cout << "enter a number" << endl;

        string s;

        getline(cin, s);
        ptr[i] = stoi(s);
    }

    for(size_t i = 0; i < size; ++i)
        cout << "ptr[i] = " << ptr[i] << "\n";

    delete[] ptr;  // Don't forget to delete the memory.

    return 0;
}


And please use code tags when posting code.

Last edited on
it looks like that the size of the array is changing dynamically
No, it doesn't. Accessing an array out of its bounds results in undefined behavior.
i don't understand why i has to be within the bounds of size. this code even works when i is greater than size.

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.
Topic archived. No new replies allowed.