### Get multiple values from cin

Hi! I'm currently in a competition, and I'm practicing by doing some tasks from last year. I found out that they test most programs by giving an input in one single line like this:
"5 3 23"
and that we need to get the three ints 5, 3 and 23 out of it (we get to know how many ints we need, but not the length). Is there an easy way to do this, or do I have to go through the whole string/char array looking for spaces?

Thanks
If you input a character or int array as a string using `cin`, then the delimiter is the first white space it encounters. So, even if you have multiple cin statements, you can input on one line, separating each by a space.
Thanks :3 Do I do this with cin.get? or cin >> intarray?
Didn't work :( I could make it work with cin >> x[0] >> x[1] >> x[2], etc, but that doesn't work when it's variable-sized
 we get to know how many ints we need, but not the length

the length of what exactly? Are you saying that you don't actually know how many ints you need?
I know how many ints I need from a variable, but I don't know how many digits
Thanks for the clarification ( I think).

However i still don't understand the earlier comment,
 Didn't work :( I could make it work with cin >> x[0] >> x[1] >> x[2], etc, but that doesn't work when it's variable-sized

Why or how doesn't it work?

char a[88]="8 3 23";
cout<<a;

int b[]={8,3,23};
cout<<b[0]<<" ";
cout<<b[1]<<" ";
cout<<b[2]<<" ";

int a[3];
int c;
for(c=0;c<3;c++){
cout<<"Insert a number\n";
cin>>a[c];
}
cout<<a[0]<<" ";
cout<<a[1]<<" ";
cout<<a[2]<<" ";
It's a bit hard to explain, so I'll try again.

The program will be tested by a machine. The machine first gives a value that says how many values it will give next time. Then it gives the all those values in a single line, with space between each value. If I had typed:
 ``123`` ``````string x; cin >> x; cout << x;``````

for the second time it gives a value, and it gives the values 5, 2 and 17, it would come out as "5 2 17".

I want to know if I easily can get those values in an int array.
It cannot give them on multiple lines.

The reason cin >> x[0] >> x[1] >> x[2] won't work, is because then I would need to know how many values it will give, while I'm programming it.
 ``12345`` `````` int count; cin >> count; int * array = new int[count]; for (int i=0; i> array[i];``````

not forgetting to end with `delete [] array;` when you are finished with the array.

Thanks, that worked, but how? I thought that would make it ask for count lines? With enter between?
The good things about arrays is that you can use an integer when calling one.

for example:

 ``1234`` ``````for(i=0;i
 Thanks, that worked, but how?

the `cin>> ` operator will extract a value delimited by whitespace. In this context, whitespace can be any of space, tab or newline, it makes no difference which is used.
Topic archived. No new replies allowed.