| | |
2d Array Help Please!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 31
Reputation:
Solved Threads: 0
How do you read and display a 2d array?
the input text is below
i just really have no idea of tried everything
any sort of help would be appreciated
the input text is below
i just really have no idea of tried everything

any sort of help would be appreciated
* . . . . . . . . . . . . . . . . . . * . * . . . . . . . . . . . . . . . . * . . . * . . . . . . . . . . . . . . * . . . . . * . . . . . . . . . . . . * . . . . . . . * . . . . . . . . . . * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . * . . . . . . . . . . * . . . . . . . * . . . . . . . . . . . . * . . . . . * . . . . . . . . . . . . . . * . . . * . . . . . . . . . . . . . . . . * . * . . . . . . . . . . . . . . . . . . *
•
•
Join Date: Aug 2008
Posts: 31
Reputation:
Solved Threads: 0
heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works
wat im not sure about tho is storing information in a text file into a 2d array so lost!
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works
wat im not sure about tho is storing information in a text file into a 2d array so lost!
C++ Syntax (Toggle Plain Text)
int main() { // VARIABLES string fileName; // the name of the grid file char grid[SIZE][SIZE]; // a char array consiting of '*' or '.' string str; int rows = 0; int columns= 0; int i = 0; int j = 0; ifstream inFile; ofstream outFile; // FILENAME INPUT cout << "Enter the filename of the grid: " << flush; cin >> fileName; cout << endl; // STORE USER INPUT for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cin >> grid[i][j]; } } // PRINT USER INPUT inFile.open(fileName.c_str()); for (int i = 0; i < SIZE; i++) { getline(inFile, str); cout << str << endl; } cout << endl; system("pause"); return 0; }
Last edited by timb89; Aug 23rd, 2008 at 12:42 am.
Do you want to keep a poor user typing this grid char by char, line by line under your program control? It is hardly worth it. Let him/her prepare the grid in his/her favorite text editor then load the result text file in your 2D array. If you don't want to follow this way, why you ask him/her about inFile name?
That's code sceleton:
That's code sceleton:
C++ Syntax (Toggle Plain Text)
int i; for (i = 0; i < SIZE && getline(inFile,str) && str.length() == SIZE; ++i) { for (int j = 0; j < SIZE; ++j) grid[i][j] = str[j]; } inFile.close(); if (i != SIZE) { cout << "*** Invalid grid line #" << i << endl; return 1; // or what else... } // Now you have a filled grid ... // Print it for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) cout << grid[i][j]; cout << '\n'; } cout.flush(); ...
Do you need to take in the input from the user ? And store those values into a file ?
Or Do you need to read from a file and then again put the same back into the file ?
Please Specify.
Or Do you need to read from a file and then again put the same back into the file ?
Please Specify.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works
wat im not sure about tho is storing information in a text file into a 2d array so lost!
C++ Syntax (Toggle Plain Text)
int main() { // VARIABLES string fileName; // the name of the grid file char grid[SIZE][SIZE]; // a char array consiting of '*' or '.' string str; int rows = 0; int columns= 0; int i = 0; int j = 0; ifstream inFile; ofstream outFile; // FILENAME INPUT cout << "Enter the filename of the grid: " << flush; cin >> fileName; cout << endl; // STORE USER INPUT for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cin >> grid[i][j]; } } // PRINT USER INPUT inFile.open(fileName.c_str()); for (int i = 0; i < SIZE; i++) { getline(inFile, str); cout << str << endl; } cout << endl; system("pause"); return 0; }
You have rows and columns initialized to 0 in lines 7 and 8 and they never change, so your for loop in lines 21 through 27 will never execute. You need to initialize row and columns. ArkM has a good point. For a grid like yours, that's an awful lot of typing. Better to read from a file. If it 's a small matrix, like 4 x 4, you may want to type it in to get it working. Use ArkM's code to display. It'll work, assuming that rows, columns, and SIZE are all the same.
C++ Syntax (Toggle Plain Text)
// Print it for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) cout << grid[i][j]; cout << '\n'; }
•
•
Join Date: Aug 2008
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
Do you need to take in the input from the user ? And store those values into a file ?
Or Do you need to read from a file and then again put the same back into the file ?
Please Specify.
their is a text file with a 20x20 grid called in1.txt
the user types in1.txt
the program is meant to store it in an array
then print that array on screen
and it will be the same as the txt input
Hey Timb, If you are using the same program till now, I see that you have initialised your rows and columns to zero, and therefore your program doesnt even start up getting in data from the file itself. I guess you will need to initialise to 19 both and then try them out.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
no
their is a text file with a 20x20 grid called in1.txt
the user types in1.txt
the program is meant to store it in an array
then print that array on screen
and it will be the same as the txt input
// STORE USER INPUT
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cin >> grid[i][j];
}
}![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Error Handling
- Next Thread: Segmentation Fault.... pthread, classes, STL Containers...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






