Correctly identify items of 2D vector for comparison

I request the input of a number from the user. My code is supposed to query the info in ContainerQ to find the correct vector row items that is also equal to the number the user enters. So users enters 2. My code is supposed to query for vector [2][n] in ContainerQ. I have a lot of print statements to see where I am and attempting to find where I'm going wrong. What do I need to do so that I can correctly capture and save, for instance query 2, so that I can compare it to info in ContainerD?

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

#include <Windows.h>
#include <math.h>
#include "pch.h"
#include <cmath>
#include <cassert>
#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;


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

	void setRow(int r, int query_nums)
	{
		row = r;
		if (r + 1 > values.size()) values.resize(r + 1);
		cout <<"setRow: "<< query_nums << " ";
		int get_query, q;
		get_query = query_nums;
		if (query_nums = row) {
			cout << "query_nums = row: " << query_nums << " "<< row << endl;
		}
	}

	void addString(string str)
	{
		values[row].push_back(str);
		cout << "values[row].push_back(str): " << row << "    " << str << "    " << endl;
	}

public:
	void addItem(string str, int query_nums)
	{
		if (isdigit(str[0])) 
			setRow(stoi(str), query_nums);
		else                     
			addString(str);
		//cout << query_nums << " ";
	}
	/*
	void set_query_num(int query_nums, const Container &C) {
		int get_query = query_nums, q;
		if (get_query = row) {
			cout << "get_query = row: " << get_query << row;
		}
	}
	*/
	friend ostream & operator << (ostream &out, const ContainerQ &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';
				cout << "C.values[i]: " << C.values[i].size() << "    ";
				//cout << "i: " << i << values[i] << "    ";
			}
		}

		return out;
	}
};

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

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

	void addStringD(string str)
	{
		values[row].push_back(str);
		cout << "values[row].push_back(str): " << row << "    " << str << "    " << endl;
	}

public:
	void addItemD(string str)
	{
		if (isdigit(str[0]))
			setRowD(stoi(str));
		else
			addStringD(str);
		//cout << query_nums << " ";
	}
	/*
	void set_query_num(int query_nums, const Container &C) {
		int get_query = query_nums, q;
		if (get_query = row) {
			cout << "get_query = row: " << get_query << row;
		}
	}
	*/
	friend ostream & operator << (ostream &out, const ContainerD &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';
				cout << "C.values[i]: " << C.values[i].size() << "    ";
				//cout << "i: " << i << values[i] << "    ";
			}
		}

		return out;
	}
};




int main(int argc, char** argv) {
	
	int query_num;
	cout << "Which query would you like to compare with our current set of documents? [1 - 225] ";
	cin >> query_num;


	// Open query
	ifstream in_query("query_word_test.txt");
	assert(in_query);
	ofstream out_query("output_query.txt");



	// Input to query
	ContainerQ Query;
	for (string s; in_query >> s; ) Query.addItem(s, query_num);

	// Output from query
	cout << Query;
	out_query << Query;


	ifstream in1("output_query.txt");

	// Open document
	ifstream in_doc("doc_word_test.txt");
	assert(in_doc);
	ofstream out_docs("output_document.txt");

	// Input to document
	ContainerD Docs;
	for (string sd; in_doc >> sd; ) Docs.addItemD(sd);

	// Output from document
	cout << Docs;
	out_docs << Docs;


}


Last edited on
Cannot test your code without an example of your input files.

Anyway:
1) You assign inside the if condition; e.g.:
if (query_nums = row)

2) You compare signed and unsigned values; e.g.:
if (r + 1 > values.size()) <-- r is int

3) You declare variables without using them; e.g.:
int get_query, q;

4) You declare variables without initializing them; e.g. see above.

5) You pointlessly introduce the entire std namespace:
using namespace std;

6) You use deprecated libraries; e.g.:
#include <conio.h>

7) You mix C and C++ headers; e.g.:
1
2
3
#include <conio.h>
#include <math.h>
#include <stdio.h> 


8) You keep your code untidy; e.g. you include headers twice. List of duplicates:
1
2
3
4
5
6
7
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
#include <sstream>
#include <string>
#include <string> 


9) You pass not lightweight objects by copy when not needed; e.g.:
void addString(string str)

Please provide examples of your input files to get smarter answers.
Hello sunshine4,

This covers some of what Enoizat has said, but I will say it anyway.

I will start with the header files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "pch.h"
#include <cctype>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <vector>

#include <cassert>
#include <fstream>
#include <algorithm>

//#include <Windows.h>
//#include <math.h>
//#include <cmath>  // <--- Same as "math.h", but the C++ version.
//#include <sstream>
//#include <conio.h>
//#include <stdio.h>  // <--- Covered under "iostream
//#include <sstream>
//#include <sstream>
//#include <iostream>       // std::std::cout
//#include <string>         // std::std::string
//#include <cstddef>        // std::size_t <--- I have never needed this for "size_t". And you never use "size_t" in the program. 

The first section is what I consider the header files that you are most likely to use in a program. At first I did not realize that they are in alphabetical order. Being this way it helps to remind you when you miss an include file. The second section I think of as the extra header files the the program needs. The third section, the ones commented out, are the duplicates or the header file that you do not need.

"Windows.h" is never used in this program.

"math.h" and "cmath" are the same thing. The "cmath" just puts "math.h" in the standard name space.

"sstream" is not used right now, but could be useful.

"stdio.h" The comment says it all.

"conio.h"
salem c wrote:

> #include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.

Also not everyone has this header file available for use. Then there is the point that you never use anything from this header file.

I think I found the input file in another thread.If you are going to remove the new line characters then you file should look 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


This way you could use std::getline(inFile, line); then use the string stream to extract the number into a numeric variable and then the words that you can put into your vector.

The function "setRow" is not working the way you are thinking. values.resize(r + 1) is working, but you are resizing the columbs of the second dimension not the rows.

What I ended up trying is to write the default ctor as:
1
2
3
4
ContainerQ()  // <--- Default ctor.
{
	values.resize(MAXROWS, std::vector<std::string>());
}

"MAXROWS" is defined above the class as constexpr size_t MAXROWS{ 11 };. You may think that the 11 is an odd number, but what it allows for is not using element (or row) zero. I started with a small number until the program is working. Later you can change the 11 to what you need.

Since the first "cout" statement says [1 - 225] you will need to make it 226.

I believe that resizing the vector in the ctor should eliminate the need for the "setRow" function.

I will do some more testing and see what else I can find.

Hope that helps,

Andy
Hi...just a fyi...I have cleaned up my file.

My query and document file 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
Topic archived. No new replies allowed.