cannot append to vector of vectors from another vector
Mar 21, 2019 at 12:38pm UTC
Hi, I have a csv file that I have read into a string vector. I also have another integer vector of vectors that set initial values to 0.
I am suppose to check if the value in each vectors of vector are 0, if its 0 then I need to insert the values i gotten from my csv file into the vectors of vector. However, I can only insert to the first vector, 203 is suppose to be in the second vector replacing the first 0.
Any help is appreciated, I am new to C++..
Here are my codes:
<
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
using vec = vector<string>;
using matrix = vector<vec>;
int main() {
int diskBlocks;
int entriesPerBlock;
cout << "Enter number of disk blocks: " ;
cin >> diskBlocks;
entriesPerBlock = 128 / diskBlocks;
cout << "Number of entries per block: " << entriesPerBlock << endl;
/*PART 1*/
//Creates a vector containing <diskBlock> vectors of int, each containing <entriesPerBlock> ints
vector<vector<int >> myvecs(diskBlocks, vector<int >(entriesPerBlock));
//Prints all contents in all vectors in myvecs
for (const vector<int > &v : myvecs) {
for (int x : v) cout << x << ' ' ;
cout << endl;
}
/*PART 2*/
//Read from csv files and store into vector named 'list'
vector<vector<string>> list;
ifstream file("test.csv" );
while (file) {
string line;
if (!getline(file, line)) break ;
istringstream ss(line);
vector<string> words;
while (ss) {
string s;
if (!getline(ss, s, ',' )) break ;
words.push_back(s);
}
list.push_back(words);
}
int j;
int dataLen = 0;
for (int i = 0; i < list.size(); i++) {
for (j = 2; j < list[i].size(); j++) {
//For adding of files
if (list[i][0] == "add" ) {
dataLen = list[i].size() - 2; //size of data
//cout << dataLen << endl;
//cout << list[i][j] << endl;
//cout << "Adding file" << list[i][1] << endl;
int toInteger = stoi(list[i][j]);
cout << toInteger << endl;
for (int y = 0; y < diskBlocks; y++) {
for (int x = 0; x < myvecs[i].size(); x++) {
if (myvecs[y][x] == 0) {
myvecs[y][x] = toInteger;
break ;
}
else {
continue ;
}
} break ;
}
}
}
}
cout << "**************************************************************DISK BLOCKS TABLE*************************************************************************" << endl;
//Prints all contents in all vectors in myvecs
for (const vector<int > &v : myvecs) {
for (int x : v) cout << x << ' ' ;
cout << endl;
}
cout << "**************************************************************TEST*************************************************************************" << endl;
string lol = "0" ;
int shit = stoi(lol);
cout << shit;
}
>
my csv file:
add, 100, 101, 102, 103, 104, 105, 106
add, 200, 201, 202, 203
read, 106
delete, 200
my current output is :
101 102 103 104 105 106 201 202
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
and my expected output is :
101 102 103 104 105 106 201 202
203 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Last edited on Mar 21, 2019 at 12:39pm UTC
Mar 21, 2019 at 2:17pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
for (int y = 0; y < diskBlocks; y++) {
for (int x = 0; x < myvecs[i].size(); x++) {
if (myvecs[y][x] == 0) {
myvecs[y][x] = toInteger;
break ;
}
else { //does nothing
continue ;
}
} break ; //unconditional break
}
the outer loop only executes one time
as a suggestion, remove the loops and simply keep track of he index where you are at
1 2 3 4 5 6 7 8 9
y = x = 0; //before line 49
if (list[i][0] == "add" ){
myvecs[y][x] = toInteger;
++x;
if (x==myvecs[y].size()){
++y;
x = 0;
}
}
Topic archived. No new replies allowed.