Attempting to create a 2D dynamic array for unknown values

I have a file that I need to read. The file should be read such that once the code reads a number all the following strings goes in to that row of the array. For instance, when the program reads 1, each word that is read following 1, goes in to [1][n]. But when the program reads 2, each word that is read following 2, goes in to [2][n], etc.

I know my code is very rudimentary at best at this point. I am still learning. However, currently my code recognizes the digit. But continues to read each word in to one big 1D array instead of beginning the column count at 1 (or 0) again when a new digit is reached. What should I do to read in the file as a dynamic 2D array, that begins a new row only after a new/different digit is read?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <Windows.h>
#include <math.h>
#include "pch.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cctype>
#include <iomanip>
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t


using namespace std;


bool isNumber(string s)
{
	for (int i = 0; i < s.length(); i++)
		if (isdigit(s[i]) == false)
			return false;

	return true;
}

int main()
{
	vector<vector<string> > my2Dvector;
	vector<vector<string> > temp;
	vector<vector<string> > v;
	vector<string> my1Dvector;
	string line, str_count, str;
	int vector_counter, move;
	ofstream file_;

	ifstream in_data("doc_word_test.txt");
	ofstream out_data("new_doc_set.txt");

	//string line;
	while (getline(in_data, line))
	{
		out_data << line << ' ';
	}

	in_data.close();
	out_data.close();


	ifstream in_query("query_word_test.txt");
	ofstream out_query("new_query_set.txt");

	while (getline(in_query, line))
	{
		out_query << line << ' ';
	}

	in_query.close();
	out_query.close();


	vector<string> arr;
	vector<vector<string>>content;
	vector<string> Words;

	// filestream variable file 
	fstream file;
	string word, t, q, filename;

	// filename of the file 
	filename = "new_query_set.txt";

	// opening file 
	file.open(filename.c_str());

	vector_counter = 0;
	move = 0;
		while (file >> str)
		{
			move++;
			if (isNumber(str))
			{
				vector_counter++;
				cout << "str: " << str << '\n' << endl;

				istringstream(str) >> vector_counter;

				cout << "vector_counter: " << vector_counter << '\n' << endl;
			}
				
			//}
			// Function returns 0 if the input is 
			// not an integer 
			else
			{
		

				Words.push_back(str);
				cout << "Words.size: " << Words.size();
				cout << "Word: " << str << endl;
				
			}
			my2Dvector.push_back(Words);
		}

}
What does your input file actually look like?
Input:
1
one
two
three
2
four
five
3
six
4
seven
eight
nine
ten
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

// need C++14 for auto here
auto read(const string& filename) {
    vector<vector<string>> vec;
    ifstream fin(filename);
    for (string str; fin >> str; )
        if (isdigit(str[0])) vec.push_back(vector<string>());
        else                 vec.back().push_back(str);
    return vec;
}

template<typename VV> void print(const VV& vec) {
    for (const auto& row: vec) {
        for (const auto& elem: row) cout << elem << ' ';
        cout << '\n';
    }
}

int main() {
    print(read("filein.txt"));
}

one two three 
four five 
six 
seven eight nine ten 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

// if str contains an unsigned integer, convert and store the value in number,
// and return true. return false otherwise
bool is_unsigned_integer( const std::string& str, std::size_t& number )
{
    try
    {
        // try to convert str to an unsigned integer
        // https://en.cppreference.com/w/cpp/string/basic_string/stoul
        std::size_t pos ;
        number = std::stoul( str, &pos ) ;
        return pos == str.size() ; // true if the string was fully consumed
    }

    // return false on conversion failure
    catch( const std::exception& ) {}
    return false ;
}

std::vector< std::vector<std::string> > read_data( std::istream& stm )
{
    std::vector< std::vector<std::string> > vec2d( 1 ) ; // initially only row zero

    std::string str ;
    while( stm >> str ) // for each string read
    {
        std::size_t row_num = 0 ; // keep track of the current row

        if( is_unsigned_integer( str, row_num ) ) // if it is a row number
        {
            // resize the vector if required (so that it has a row row_num)
            if( vec2d.size() < (row_num+1) ) vec2d.resize(row_num+1) ;
        }

        // otherwise, add the string to the end of the current row
        else vec2d[row_num].push_back(str) ;
    }

    return vec2d ;
}

int main()
{
    const std::string input_file_name = "whatever.txt" ;
    std::ifstream file(input_file_name) ;
    const auto vec2d = read_data(file) ;
    // ...
}
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cctype>
#include <cassert>
using namespace std;


class Container
{
   vector< vector<string> > values;
   int row = 0;

   void setRow( int r )
   { 
      row = r;
      if ( r + 1 > values.size() ) values.resize( r + 1 );
   }

   void addString( string str )
   {
       values[row].push_back( str );
   }

public:
   void addItem( string str )
   {
       if ( isdigit( str[0] ) ) setRow( stoi( str ) );
       else                     addString( str );
   }

   friend ostream & operator << ( ostream &out, const Container &C )
   {
       for ( int i = 0; i < C.values.size(); i++ )
       {
          if ( C.values[i].size() )
          {
             out << i << ": ";
             for ( string s : C.values[i] ) out << s << " ";
             out << '\n';
          }
       }
       return out;
   }
};

//==========================================================

int main()
{
   ifstream in( "input.txt" );   assert( in );
   ofstream out( "output.txt" );

   // Input
   Container C;
   for ( string s; in >> s; ) C.addItem( s );

   // Output
   cout << C;
   out << C;
}


input.txt
3
alpha
beta gamma
6
delta
epsilon
1
zeta
4 eta theta
iota kappa lambda


output.txt (and console)
1: zeta 
3: alpha beta gamma 
4: eta theta iota kappa lambda 
6: delta epsilon 
Last edited on
The file initially looked like
1
seven three two eleven
four nineteen two
five seventeen one five
2
eight six twelve eleven
thirteen twenty two
fifteen fourteen sixteen ten
...

But I removed the endline character to hopefully read the file better. So currently the file that I am working with looks like...

1 seven three two eleven four nineteen two five seventeen one five 2 eight six twelve eleven thirteen twenty two fifteen fourteen sixteen ten...

So now, I am trying to recognize the digit 1 for instance and place "seven three two eleven four nineteen two five seventeen one five" in 1[n]. So that [1][1] points to "seven" and [1][2] points to "three" and so on. Then read 2 and place the "eight" that follows 2 in [2][1] and "six" in [2][2] and so on.
Thanks all...tried all. lastchance's and dutch's worked as listed. The other one, unfortunately still had a 1D array, which is what I already had. I tried tweaking to get to a 2D but couldn't get it to work. Thanks again all.
Topic archived. No new replies allowed.