显示不需要的字符的2d阵列



我在显示2d数组的内容时遇到问题,我在这里搜索了解决方案,但由于我不完全确定我的问题是什么,我不确定我问的问题是否正确。

我正在编写一个程序,让用户打开一个.txt文件,该文件将始终包含30乘30个字符,所有字符都用逗号分隔,第一行和第二行将是0,1,2,3,,9,0,1、2,3、9,每行的末尾都将是一行新行。程序应在屏幕上显示.txt文件的内容,不带逗号,然后继续允许用户搜索文件中字符的位置。

我认为最好的方法是使用带有分隔符的getline函数来填充2d数组,这似乎起到了作用,除了当我显示数组的内容时,最后一个字符是重复的,我不确定这是由于我填充数组的方式还是显示数组的方式。

while(!inputFile.eof())
{
for (int i = 0; i < SIZE; ++i)           // SIZE is defined 30
{
for (int j = 0; j < SIZE; ++j)
{
getline(inputFile, line,',');    
aArray[i][j] = line;             // aArray and line are declared as strings     
std::cout << aArray[i][j];
}
}
cout << endl;
}

这是我的输入:

0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 
1,*,!,8,0,;,*,a,b,0,8,0,.,y,Z,c,4,4,8,8,8,8,y,y,y,4,–,6,8,! 
2,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,0,0,0,5,5,J,J,J,J,J,J,J,J,j 
3,9,8,7,0,8,0,8,0,A,c,4,4,*,F,F,6,F,K,J,H,G,5,s,H,U,P,2,2,0 
4,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 
5,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 
6,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,Y,Y,Y,0,0,0,0,0,0,0 
7,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 
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 
9,P,P,P,P,P,P,P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,8,8,8,8 
6,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,Y,Y,Y,0,0,0,0,0,0,0 
7,0,1,1,2,2,D,D,5,5,$,£,!,a,A,a,A,a,A,a,A,a,A,a,A,a,A,a,0,8 
8,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,Y,Y,Y,0,0,0,0,0,8,0 
9,0,1,1,2,2,D,D,5,5,$,£,!,a,A,a,A,a,A,a,A,a,A,a,A,a,A,a,0,8

这是我的输出:

012345678901234567890123456789 
1*!80;*ab080.yZc448888yyy4–68! 
200000000001112200055JJJJJJJJj 
398708080Ac44*FF6FKJHG5sHUP220 
400000000000000000000000000000 
588888888888888888888888888888 
6PPPPPPPPPPPPPPPPPPPYYY0000000 
700000000000000000000000000000 
888888888888888888888888888888 
9PPPPPPPPPPPPPPPPPPPYYY0000000 
001122DD55$£!aAaAaAaAaAaAaAaA8 
100000000000000000000000000000 
288888888888888888888888888888 
3PPPPPPPPPPPPPPPPPPPYYY0000000 
401122DD55$£!aAaAaAaAaAaAaAaA8 
500000000000000000000000000000 
608088888888888888888888888888 
7P0PPPPPPPPPPPPPPPPPYYY0000000 
801122DD55$£!aAaAaAaAaAaAaAaA8 
900000000000000000000000000000 
088888888888888888888888888888 
1PPPPPPPPPPPPPPPPPPPYYY0000000 
201122DD55$£!aAaAaAaAaAaAaAaA8 
301122DD55$£!aAaAaAaAaAaAaAaA8 
400000000000000000000000000000 
588888888888888888888888888888 
6PPPPPPPPPPPPPPPPPPPYYY0000000 
701122DD55$£!aAaAaAaAaAaAaAa08 
8PPPPPPPPPPPPPPPPPPPYYY0000080 
901122DD55$£!aAaAaAaAaAaAaAa0888888888888888888888888888888

正如你所看到的,最后一个字符是重复的,这不是我想要的。我是C++的新手,所以我不确定我是否使用了最好的方法,我花了将近2天的时间试图解决我错误地将cout<lt;endl;在我的for循环中,所以如果堆栈溢出,我会联系优秀的成员,寻求一些急需的指导。

要扩展@WhozCraig的注释,您不应该在循环条件中使用iostream::eof,因为只有在读取文件末尾后才会返回true。相反,这样的东西应该足够了(注意有很多方法可以做到这一点(:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#define SIZE 30
int main() {
std::string line;
std::string aArray[SIZE][SIZE];
std::ifstream inputFile("file2.txt");
for (int i = 0; getline(inputFile, line) && i < SIZE; ++i) {
std::istringstream lineStream(line);
std::string token;
for (int j = 0; getline(lineStream, token, ',') && j < SIZE; ++j) {
aArray[i][j] = token;
std::cout << aArray[i][j];
}
std::cout << std::endl;
}
}

它将提供所需的输出:

012345678901234567890123456789
1*!80;*ab080.yZc448888yyy4–68!
200000000001112200055JJJJJJJJj
398708080Ac44*FF6FKJHG5sHUP220
400000000000000000000000000000
588888888888888888888888888888
6PPPPPPPPPPPPPPPPPPPYYY0000000
700000000000000000000000000000
888888888888888888888888888888
9PPPPPPPPPPPPPPPPPPPYYY0000000
001122DD55$£!aAaAaAaAaAaAaAaA8
100000000000000000000000000000
288888888888888888888888888888
3PPPPPPPPPPPPPPPPPPPYYY0000000
401122DD55$£!aAaAaAaAaAaAaAaA8
500000000000000000000000000000
608088888888888888888888888888
7P0PPPPPPPPPPPPPPPPPYYY0000000
801122DD55$£!aAaAaAaAaAaAaAaA8
900000000000000000000000000000
088888888888888888888888888888
1PPPPPPPPPPPPPPPPPPPYYY0000000
201122DD55$£!aAaAaAaAaAaAaAaA8
301122DD55$£!aAaAaAaAaAaAaAaA8
400000000000000000000000000000
588888888888888888888888888888
6PPPPPPPPPPPPPPPPPPPYYY0000000
701122DD55$£!aAaAaAaAaAaAaAa08
8PPPPPPPPPPPPPPPPPPPYYY0000080
901122DD55$£!aAaAaAaAaAaAaAa08 

相关内容

  • 没有找到相关文章