简体   繁体   English

2D数组显示不需要的字符

[英]2d array displaying unwanted characters

I have a problem with displaying the contents of a 2d array, I have searched on here for solutions but as I'm not entirely sure what my problem is, I'm not sure I'm asking the right question. 我在显示2d数组的内容时遇到问题,我在这里搜索了解决方案,但是由于我不确定自己的问题是什么,所以我不确定我问的是正确的问题。

I am writing a program that lets the user open a .txt file which will always contain 30 by 30 characters all separated by a comma and the first line and row will be 0,1,2,3,,,9,0,1,2,3,,,9 and the end of each line will be a new line. 我正在编写一个程序,允许用户打开一个.txt文件,该文件始终包含30 x 30个字符,所有字符之间都用逗号隔开,并且第一行和第一行分别为0、1、2、3 、、、 9、0、1 ,2,3 ,,, 9和每行的结尾将是一个新行。 The programme should display the contents of the .txt file on the screen without the commas then go on to allow the user to search the location of characters in the file. 该程序应在屏幕上显示.txt文件的内容,但不要使用逗号,然后继续进行操作以允许用户搜索文件中字符的位置。

I thought the best way to do this would be to use the getline function with a delimiter to populate a 2d array, this seems to have worked except when I display the content of the array, the last character is repeated and I'm not sure if it's due to the way I am populating the array or the way in which I am displaying the array. 我认为最好的方法是使用带有分隔符的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;
}

This is my input: 这是我的输入:

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,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,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 
1,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 
2,0,1,1,2,2,D,D,5,5,$,£,!,a,A,a,A,a,A,a,A,a,A,a,A,a,A,a,A,8 
3,0,1,1,2,2,D,D,5,5,$,£,!,a,A,a,A,a,A,a,A,a,A,a,A,a,A,a,A,8 
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,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

This is my output: 这是我的输出:

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

As you can see the last character is repeated which is not what I want. 如您所见,最后一个字符被重复了,这不是我想要的。 I am new to C++ so I'm not sure if I'm even using the best method, I spent nearly 2 days trying to solve a problem I created by mistakingly putting cout << endl; 我是C ++的新手,因此不确定是否使用最佳方法,我花了将近2天的时间来解决我因误将cout << endl而创建的问题。 within my for loop so I am reaching out to the fine members if stack overflow for some much needed guidance. 在我的for循环中,因此如果堆栈溢出,我会联系一些优秀的成员,以获取一些急需的指导。

To expand on the comment by @WhozCraig, you shouldn't use iostream::eof inside the loop condition, since it won't return true until after the end of the file is read. 要扩展@WhozCraig的注释,您不应在循环条件内使用iostream::eof ,因为读取文件末尾之前,它不会返回true。 Instead, something like this should suffice (note there are many ways to do this): 相反,这样的事情就足够了(注意有很多方法可以做到这一点):

#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;
    }
}

Which will provide the desired output: 这将提供所需的输出:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM