Input of several sets of numbers

Hey guys,

I want to read sets of numbers separated by spaces.
The sets are separated by 0.
Each set contains between 2 and 1000 numbers.
The last line of the input is a lone 0.

Example:
105 1053 1427 2412 0
14 21 17 -13 -125 0
0

After I got one set I will calculate something with the numbers (except the 0), output the result and then I won't need that set anymore.

I tried with getline and delimeter 0, but it will also cut if a number has a 0 as part of it, like 105.
If I try to use ' 0' as delimeter it warns me about multi character constant.

I tried to just cin >> myVector[i] until myVector[i] is 0, but that leads to address boundary error.

I would greatly appreciate any ideas on how to make this work.
Instead of using myVector[i] for your inputted value you may want to use a "temporary" variable and then push that "temporary" value into your vector.

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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
   string filename = "data.txt";
   vector< vector<int> > matrix;                           // Effectively, vector of rows
   int num;

   ifstream in( filename );
   vector<int> row;
   while ( in >> num )
   {
      if ( num == 0 )                                      // Signifies end of a row
      {
         if ( row.size() != 0 ) matrix.push_back( row );   // Non-"empty" rows pushed into matrix
         row.clear();                                      // Ready to start a new row
      }
      else
      {
         row.push_back( num );                             // Non-zero item just added to current row
      }
   }
   in.close();


   cout << "Read " << matrix.size() << " rows, as follows:\n";
   for ( int i = 0; i < matrix.size(); i++ )
   {
      cout << "Row " << i + 1 << ": ";
      for ( int j = 0; j < matrix[i].size(); j++ ) cout << '\t' << matrix[i][j];
      cout << '\n';
   }
}


Read 2 rows, as follows:
Row 1: 	105	1053	1427	2412
Row 2: 	14	21	17	-13	-125
The sets are terminated by zero. (And the collection of sets is terminated by an empty set.)

Anyway, write a function:

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
#include <ciso646>
#include <iostream>
#include <vector>

typedef std::vector <int> collection;

bool read_collection( std::istream& ins, collection& xs )
{
  xs.clear();
  int x;
  while (ins >> x and x) xs.push_back( x );
  return ins.good();
}

int main()
{
  // Read the first two collections
  collection xs, ys;
  read_collection( std::cin, xs );
  read_collection( std::cin, ys );
  
  // Ignore the final collection
  {
    collection foo;
    read_collection( std::cin, foo );
  }

These functions do not assume that each collection is on a single line. (If they did then there would be no need for the semaphore zero to terminate them.)

Now, the following input:

    2 3 5 7 11 0
    -1 2 -3 4
    -5 6 -7 8
    0 0

Produces the following values:

    xs = { 2, 3, 5, 7, 11 }
    ys = { -1, 2, -3, 4, -5, 6, -7, 8 }
    (foo = {})


Hope this helps.
Thanks a lot Duthomhas, that did help.
I had to modify the main() a bit so I can have any amount of sets as input, as I don't know that beforehand either ;)
Since I don't need the vectors after my calculations I just overwrite it and check if it has at least 2 numbers to avoid writing a 0 at the end.

1
2
3
4
5
6
7
8
9
10
main(){
   collection xs;
   do{
        read_collection(cin, xs);
        if (xs.size() < 2) {
            break;
        }
        solve(xs);
       }while(xs.size() > 1);
}
Brilliant! That is exactly the correct way to view it. Nice job. (And, you’re welcome!)
Topic archived. No new replies allowed.