-
multiple integer input
I need to take in any arbitrary amount of integers on one line. Like "enter integers:" 2 3 4 1 5
I do not know the amount that will be entered, so I am not sure how to go about doing this. I have to store the data in an array. Can someone give me a few hints on how to do this? thanks
-
Read in the entire line as a string. Then scan the string for your integers.
-
If you dont know the length of the line you will need to limit the size of the input with an array (or the getline function that Cshot suggested) or use a dynamic container, ie vector. Then use a loop until you encounter a '\n' character
-
Code:
string str;
vector<int> ivec;
getline(cin,str);
stringstream sin(str);
do
{
int i;
sin >> i;
ivec.push_back(i);
} while (sin);
Not tested, but it goes somthing like that.