trouble with processing input

Mar 21, 2011 at 8:54pm
Hello.

Let's say, that I have N lines of input where on each line are from 1 to K integers.

I have a bit trouble with reading this input correctly. I've tired:

1
2
3
4
5
6
for (int i = 0; i < N; i++) {
    int x;
    while (cin >> x) {
        ...
    }
}

but it doesn't seem to work correctly. Can you pleas give me some tips on how to solve this? And I would like to read the integers directly into a integer variable. I mean I would like to avoid for example reading the whole line through for example getline().

thanks.
Mar 21, 2011 at 9:49pm
while (cin >> x;) {
I don't think that that is valid.
To get a certain amount of integers, try:
1
2
3
int myarray[10];
for (int c = 0; c<=9; c++){
    cin >> myarray[c];}
Last edited on Mar 21, 2011 at 9:50pm
Mar 21, 2011 at 10:02pm
Ye but I don't know how many there will be. For example if N = 5 and K = 5, the input can look like this:

54 45 11 10
12 546 12 45 12
12 12
122
12 123 12
Mar 21, 2011 at 10:53pm
Where do you get the input? If you get it from the user, simply ask them how many numbers they will input, then dynamically allocate an array of that size. Then get the input until the array is full.
Mar 22, 2011 at 6:35am
I get it from a system, not from a user.
Mar 22, 2011 at 6:39am
Use a vector to store the data. If lines are important, then use getline() to get each line separately, then read those lines into vectors.
Topic archived. No new replies allowed.