2d array displaying unwanted characters












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










share|improve this question




















  • 4





    C++ - Why is iostream::eof inside a loop condition considered wrong?

    – WhozCraig
    Nov 17 '18 at 19:58











  • 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, Complete, and Verifiable example will help people answer your question.

    – anatolyg
    Nov 17 '18 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.

    – 8TrackRobot
    Nov 17 '18 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?

    – 8TrackRobot
    Nov 17 '18 at 20:52
















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










share|improve this question




















  • 4





    C++ - Why is iostream::eof inside a loop condition considered wrong?

    – WhozCraig
    Nov 17 '18 at 19:58











  • 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, Complete, and Verifiable example will help people answer your question.

    – anatolyg
    Nov 17 '18 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.

    – 8TrackRobot
    Nov 17 '18 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?

    – 8TrackRobot
    Nov 17 '18 at 20:52














-1












-1








-1


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










share|improve this question
















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







c++ arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 20:26







8TrackRobot

















asked Nov 17 '18 at 19:49









8TrackRobot8TrackRobot

5027




5027








  • 4





    C++ - Why is iostream::eof inside a loop condition considered wrong?

    – WhozCraig
    Nov 17 '18 at 19:58











  • 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, Complete, and Verifiable example will help people answer your question.

    – anatolyg
    Nov 17 '18 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.

    – 8TrackRobot
    Nov 17 '18 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?

    – 8TrackRobot
    Nov 17 '18 at 20:52














  • 4





    C++ - Why is iostream::eof inside a loop condition considered wrong?

    – WhozCraig
    Nov 17 '18 at 19:58











  • 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, Complete, and Verifiable example will help people answer your question.

    – anatolyg
    Nov 17 '18 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.

    – 8TrackRobot
    Nov 17 '18 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?

    – 8TrackRobot
    Nov 17 '18 at 20:52








4




4





C++ - Why is iostream::eof inside a loop condition considered wrong?

– WhozCraig
Nov 17 '18 at 19:58





C++ - Why is iostream::eof inside a loop condition considered wrong?

– WhozCraig
Nov 17 '18 at 19:58













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, Complete, and Verifiable example will help people answer your question.

– anatolyg
Nov 17 '18 at 20:18





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, Complete, and Verifiable example will help people answer your question.

– anatolyg
Nov 17 '18 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.

– 8TrackRobot
Nov 17 '18 at 20:33





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.

– 8TrackRobot
Nov 17 '18 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?

– 8TrackRobot
Nov 17 '18 at 20:52





@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?

– 8TrackRobot
Nov 17 '18 at 20:52












1 Answer
1






active

oldest

votes


















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





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354950%2f2d-array-displaying-unwanted-characters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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





    share|improve this answer






























      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





      share|improve this answer




























        3












        3








        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





        share|improve this answer















        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 2:22

























        answered Nov 17 '18 at 20:52









        ryanecristryanecrist

        24828




        24828






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354950%2f2d-array-displaying-unwanted-characters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()