显示不必要字符的二维数组

时间:2018-11-17 19:49:42

标签: c++ arrays

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

我正在编写一个程序,该程序允许用户打开.txt文件,该文件始终包含30 x 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,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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,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 << endl而创建的问题。在我的for循环中,因此如果堆栈溢出,我会联系一些好的成员,以寻求一些急需的指导。

1 个答案:

答案 0 :(得分:3)

要扩展@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