-1

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.

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. 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.

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.

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,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,P,P,P,P,P,P,P,P,P,P,P,P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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; within my for loop so I am reaching out to the fine members if stack overflow for some much needed guidance.

4
  • 4
  • Please post the contents of your input file. Please also reduce the size of your input file, and post your program's output. You can edit your question to do that. This minimal reproducible example will help people answer your question.
    – anatolyg
    Nov 17, 2018 at 20:18
  • I have included the contents of the input file and reduced the size of the output file by removing the spaces. It seems @WhozCraig has provided me some good guidance. Nov 17, 2018 at 20:33
  • @WhozCraig Thank you so much for pointing me in the right direction, all I had to do was get rid of the !=EOF and rearrange the lines I had and it works perfectly. Is it appropriate to post my solution too? Nov 17, 2018 at 20:52

1 Answer 1

3

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. 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 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.