【发布时间】:2019-09-27 01:55:23
【问题描述】:
首先是输入文件:
0 a a b c
0 b d e f
1 c g h i
1 d j k l
我们试图从输入文件中读取并将接收到的字符存储到一个向量中,然后从那里将它存储到一个名为T 的二维表中。我们得到了初始的 while 循环,因此它抓取第一列并输出“a b c d”,但我们无法让它返回通过while 或for 循环来抓取其他行和列。以下是我们目前所拥有的:
int readTable() {
int row, col; // row and col numbers
char col_c; // column indicator
ifstream fin("C:\\Users\\name\\OneDrive\\Desktop\\lines.txt", ios::in);
// Read in the file into T
while (fin >> row) // next line of file
{
fin >> col_c;
col = convert(col_c); // convert to a slot number
vector<char> v; // a vector to fill
char c; // one char from the file
// ** Fill v with chars from the file (there are VM chars)
for (int i = 0; i < VM; i++) {
fin >> c;
v.push_back(c);
}
// ** Put v in T[row][col]
T[row][col] = v;
} // end of while
convert 函数只是将“a”转换为 0,“b”转换为 1,“c”转换为 2,以便我们将向量放置在 T 中的正确位置。任何有**的地方都应该是我们编辑和修改的部分。
【问题讨论】:
-
你怎么知道 while 循环不起作用?
-
T是二维向量数组吗?
标签: c++ visual-studio computer-science