0: a bcbef|- 1:- - - - - - - chi 2:- - - - - - - - - - - - |d k 1 |
Help please The picture is how the output should look
Needed Files:
---LINES.TXT---
0 a a b c
0 b b e f
1 c c h i
2 d d k l
// the following mat.cpp program is not reading the input from the file >> lines.txt !
Please help me find the bug that will make it work
The code works to show the table but does not read the file and input to the table
---MAT.CPP---
int const R = 3; // number of rows
int const C = 4; // number of columns
int const VL = 3; // table entry
vector<char> T[R][C]; // 3 rows and 4 columns table for now
// ------------------ Functions --------------------------------
int convert(char x) {
// TODO: use a formula to convert a to 0, b to 1, c to 2 etc.
return x -'a'; //ascii conversion
}
// this reads the input file into the matrix table
void readTable() {
int row, col;
char col_c;
ifstream fin("lines.txt", ios::in);
fin.open("lines.txt");
// Read in the file into T
while (fin >> row) // per row
{ cout << "collect";
vector<char> V;
fin >> col_c;
col = convert(col_c); // to a slot number
char c; // one char from the file
//cout << "123";
// TODO: Fill vector V with chars from the file (a for loop)
while (fin >> c) {
if (c == '\n') {
break; // Stop reading if a newline is encountered or end of file
}
V.push_back(c); // Fill vector V with characters
}
cout << "hello world";
// TODO: Put the vector in T[row][col]
int vectorIndex = 0;
for (int i = 0; i < 4; ++i) {
//&& vectorIndex < V.size()
if (i == col) { // Place characters in the specified column
col = convert(i);
T[row][i] = V[vectorIndex++][];
}
}
}
fin.close();
} // end of while
// display the vector nicely to the outstream
void showVector(vector<char> v, ostream &ost) {
if (v.size() == 0) // empty entry will be dashes {
for (int i = 1; i <= VL; i++) {
ost << "- "; //ost~cout
}
else {
// TODO show the content of v separated by blanks
for (int i = 0; i < v.size(); i++) {
ost << v[0] << ' ';
}
}
}
// display the matrix
void displayTable() {
// display T nicely labeled with row numbers
// by calling showVector with cout
// for each column
for (int r = 0; r < R; r++) {
cout << r << ":";
for (int c = 0; c < C; c++) {
showVector(T[r][c], cout);
cout << "|";
}
cout << endl;
}
}
// save the matrix to the output file
void saveTable() {
ofstream fout("table.txt", ios::out);
// output T nicely labeled with row numbers
// by calling showVector with fout
// for each column
// TODO
if (!fout) {
cerr << "Error opening file." << endl;
return;
}
// Iterate over each column
for (size_t col = 0; col < C; col++) {
// Extract the column
vector<char> column;
for (size_t row = 0; row < R; row++) {
column.push_back(T[row][col][0]);
}
// Output the column nicely labeled with row numbers
showVector(T[R][C],fout);
}
fout.close();
}
int main() {
cout << "reading from lines.txt" << endl;
readTable();
displayTable();
cout << "saving the table in table.txt" << endl;
saveTable();
} // the end
Unlock instant AI solutions
Tap the button
to generate a solution