Get multiple values from cin

Nov 28, 2013 at 3:44pm
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
Nov 28, 2013 at 4:00pm
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.
Nov 28, 2013 at 4:44pm
Thanks :3 Do I do this with cin.get? or cin >> intarray?
Nov 28, 2013 at 5:05pm
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
Nov 28, 2013 at 5:17pm
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?
Nov 28, 2013 at 5:53pm
I know how many ints I need from a variable, but I don't know how many digits
Nov 28, 2013 at 6:17pm
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?
Nov 28, 2013 at 6:38pm
What about this? 1:

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

What about this? 2:

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

What about this? 3:

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]<<" ";
Nov 28, 2013 at 6:57pm
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:
1
2
3
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.
Nov 28, 2013 at 7:11pm
then how about this:
1
2
3
4
5
    int count;
    cin >> count;
    int * array = new int[count];
    for (int i=0; i<count; i++)
        cin >> array[i];

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

Nov 28, 2013 at 7:24pm
Thanks, that worked, but how? I thought that would make it ask for count lines? With enter between?
Nov 28, 2013 at 7:25pm
The good things about arrays is that you can use an integer when calling one.

for example:

1
2
3
4
for(i=0;i<x;i++)//Where x is number of ints
{
     cout<<a[i]<<" ";// where a is name of array.
}
Nov 28, 2013 at 7:28pm
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.