| | |
reading a txt file into a 2d array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
I am needing some help with my CS114 project. I am not expecting people here to do it for me just to help me with it.
The project is to do a Sudoku game with a 2d array (9x9) and read in a txt file that the instructor has supplied.
The txt file will have the nearly completed sudoku board.
I am having trouble reading the txt file into the 9x9 array
also, the output is confusing me
they want the board to be displayed in this manner ::
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
any help would be AMAZING and i kinda need it ASAP
thanks guys!
The project is to do a Sudoku game with a 2d array (9x9) and read in a txt file that the instructor has supplied.
The txt file will have the nearly completed sudoku board.
I am having trouble reading the txt file into the 9x9 array
also, the output is confusing me
they want the board to be displayed in this manner ::
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
1 2 3 | 1 2 3 | 1 2 3
4 5 6 | 4 5 6 | 4 5 6
7 8 9 | 7 8 9 | 7 8 9
-----------------------
any help would be AMAZING and i kinda need it ASAP

thanks guys!
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
we are to write the entire code..here is the project
"Your Sudoku class should have a constructor, and a “read” function that reads in the file “Sudoku_game.txt,” a 9 by 9 matrix of digits from 0 to 9. (Zero indicates that no number has been put in that cell.) Your class should also have a print function, a test for whether the current state of the game is one that cannot work (violating some of the constraints, such as two 3‟s in the same box), a test to tell whether all the cells have been filled with a number, and a function that would allow a player to enter numbers in the open cells of the game. The main function should create an empty game, read a file into the game, print the current state of the game board, and then allow the player to add numbers (using row and column numbers) to complete the puzzle. After each number entered, the game should decide if the game is still winnable (that is, whether any of the constraints have already been violated.) If the last number entered won‟t work, print an error message and change the number back to zero. If the player successfully completes the game, entering the last number without violating the rules, the program should print a congratulatory message and quit. If the last cell is filled with a number, but the solution is not correct, the game should print a sympathetic message."
but i am starting small..and right now just trying to get the board to print out.
the txt file they give us has this:
"6 5 9 4 1 3 2 8 7
7 4 3 2 9 8 1 6 5
1 8 2 5 6 7 9 4 3
9 1 4 3 2 5 8 7 6
3 7 8 6 4 9 5 1 2
2 6 5 7 8 1 4 3 9
4 3 7 1 5 2 6 9 8
5 9 1 8 3 6 7 2 4
0 0 0 0 0 0 0 0 0
"
where the "0" are suppose to be blank places to be filled in.
how would i get this txt document to print out in the manner i mentioned in my previous thread.
"Your Sudoku class should have a constructor, and a “read” function that reads in the file “Sudoku_game.txt,” a 9 by 9 matrix of digits from 0 to 9. (Zero indicates that no number has been put in that cell.) Your class should also have a print function, a test for whether the current state of the game is one that cannot work (violating some of the constraints, such as two 3‟s in the same box), a test to tell whether all the cells have been filled with a number, and a function that would allow a player to enter numbers in the open cells of the game. The main function should create an empty game, read a file into the game, print the current state of the game board, and then allow the player to add numbers (using row and column numbers) to complete the puzzle. After each number entered, the game should decide if the game is still winnable (that is, whether any of the constraints have already been violated.) If the last number entered won‟t work, print an error message and change the number back to zero. If the player successfully completes the game, entering the last number without violating the rules, the program should print a congratulatory message and quit. If the last cell is filled with a number, but the solution is not correct, the game should print a sympathetic message."
but i am starting small..and right now just trying to get the board to print out.
the txt file they give us has this:
"6 5 9 4 1 3 2 8 7
7 4 3 2 9 8 1 6 5
1 8 2 5 6 7 9 4 3
9 1 4 3 2 5 8 7 6
3 7 8 6 4 9 5 1 2
2 6 5 7 8 1 4 3 9
4 3 7 1 5 2 6 9 8
5 9 1 8 3 6 7 2 4
0 0 0 0 0 0 0 0 0
"
where the "0" are suppose to be blank places to be filled in.
how would i get this txt document to print out in the manner i mentioned in my previous thread.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
we are to write the entire code..here is the project
"Your Sudoku class should have a constructor, and a “read” function that reads in the file “Sudoku_game.txt,” a 9 by 9 matrix of digits from 0 to 9. (Zero indicates that no number has been put in that cell.) Your class should also have a print function, a test for whether the current state of the game is one that cannot work (violating some of the constraints, such as two 3‟s in the same box), a test to tell whether all the cells have been filled with a number, and a function that would allow a player to enter numbers in the open cells of the game. The main function should create an empty game, read a file into the game, print the current state of the game board, and then allow the player to add numbers (using row and column numbers) to complete the puzzle. After each number entered, the game should decide if the game is still winnable (that is, whether any of the constraints have already been violated.) If the last number entered won‟t work, print an error message and change the number back to zero. If the player successfully completes the game, entering the last number without violating the rules, the program should print a congratulatory message and quit. If the last cell is filled with a number, but the solution is not correct, the game should print a sympathetic message."
but i am starting small..and right now just trying to get the board to print out.
the txt file they give us has this:
"6 5 9 4 1 3 2 8 7
7 4 3 2 9 8 1 6 5
1 8 2 5 6 7 9 4 3
9 1 4 3 2 5 8 7 6
3 7 8 6 4 9 5 1 2
2 6 5 7 8 1 4 3 9
4 3 7 1 5 2 6 9 8
5 9 1 8 3 6 7 2 4
0 0 0 0 0 0 0 0 0
"
where the "0" are suppose to be blank places to be filled in.
how would i get this txt document to print out in the manner i mentioned in my previous thread.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; void DisplayBoard (int grid[][9]) { // display grid. Use nested for-loop } void ReadFromFile (string filename, int grid[][9]) { // create ifstream // open ifstream with filename // read from file into grid using nested for-loop // close ifstream } int main () { int grid[9][9]; string filename = "Sudoku_game.txt"; ReadFromFile (filename, grid); DisplayBoard (grid); return 0; }
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
ok for my void DisplayBoard function i have this :
void DisplayBoard (int grid[9][9]) // display grid. use nested for-loop
{
cout << "\t+---+---+---+" << endl;
for (int a=0; a<9; a++) {
cout << "\t| ";
for (int b=0; b<9; b++)
cout << grid[a][b] << " | ";
cout << endl;
cout << "\t+---+---+---+" << endl;
}
but when it displays it has "-858993460" in each area...?
my ultimate goal is to have the txt document provide the numbers to go in each block on the game board
void DisplayBoard (int grid[9][9]) // display grid. use nested for-loop
{
cout << "\t+---+---+---+" << endl;
for (int a=0; a<9; a++) {
cout << "\t| ";
for (int b=0; b<9; b++)
cout << grid[a][b] << " | ";
cout << endl;
cout << "\t+---+---+---+" << endl;
}
but when it displays it has "-858993460" in each area...?
my ultimate goal is to have the txt document provide the numbers to go in each block on the game board
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
ok for my void DisplayBoard function i have this :
void DisplayBoard (int grid[9][9]) // display grid. use nested for-loop
{
cout << "\t+---+---+---+" << endl;
for (int a=0; a<9; a++) {
cout << "\t| ";
for (int b=0; b<9; b++)
cout << grid[a][b] << " | ";
cout << endl;
cout << "\t+---+---+---+" << endl;
}
but when it displays it has "-858993460" in each area...?
my ultimate goal is to have the txt document provide the numbers to go in each block on the game board
Code tags:
[code]
// paste code here
[/code]
This function looks OK. Are you initializing grid anywhere?
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
thats what im trying to get done now, is the grid display function.
i can have it display numbers if i do the code as this
but im stumped on how i get the array to have the numbers that are set in the txt document provided.
am i close by thinking it is done this way.
THANKS FOR THE HELP!
i can have it display numbers if i do the code as this
C++ Syntax (Toggle Plain Text)
int grid [9][9] = {1,2,3,4};
but im stumped on how i get the array to have the numbers that are set in the txt document provided.
am i close by thinking it is done this way.
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt"); int grid[9][9] = "sudoku_game.txt");
THANKS FOR THE HELP!
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
am i close by thinking it is done this way.
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt"); int grid[9][9] = "sudoku_game.txt");
THANKS FOR THE HELP!
http://www.cplusplus.com/doc/tutorial/files.html
The general idea is this. See my prior post on the ReadFile function and the tutorial above. That's the idea. Use my program as a skeleton.
This line is fine:
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt");
This line isn't:
C++ Syntax (Toggle Plain Text)
int grid[9][9] = "sudoku_game.txt");
This is better:
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt"); for (i = ?;?;?) { for (j = ?;?;?) { in >> grid[i][j]; } } in.close ();
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
No, that is not close.
http://www.cplusplus.com/doc/tutorial/files.html
The general idea is this. See my prior post on the ReadFile function and the tutorial above. That's the idea. Use my program as a skeleton.
This line is fine:
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt");
This line isn't:
C++ Syntax (Toggle Plain Text)
int grid[9][9] = "sudoku_game.txt");
This is better:
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt"); for (i = ?;?;?) { for (j = ?;?;?) { in >> grid[i][j]; } } in.close ();
so far i have this as my printBoard
C++ Syntax (Toggle Plain Text)
int a,b; int grid[9][9]; ifstream gameBoard; gameBoard.open("sudoku_game.txt"); gameBoard >> grid[9][9]; gameBoard.close(); for (int a=0; a<9; a++) { cout << "\t| "; } for (int b=0; b<9; b++){ cout << grid[a][b] << " | "; cout << endl; cout << "\t-------------------------------" << endl; }
but it still isnt printing out the numbers in the txt document.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
so far i have this as my printBoard
C++ Syntax (Toggle Plain Text)
int a,b; int grid[9][9]; ifstream gameBoard; gameBoard.open("sudoku_game.txt"); gameBoard >> grid[9][9]; gameBoard.close(); for (int a=0; a<9; a++) { cout << "\t| "; } for (int b=0; b<9; b++){ cout << grid[a][b] << " | "; cout << endl; cout << "\t-------------------------------" << endl; }
but it still isnt printing out the numbers in the txt document.
C++ Syntax (Toggle Plain Text)
ifstream in("sudoku_game.txt"); for (i = ?;?;?) { for (j = ?;?;?) { in >> grid[i][j]; } } in.close ();
This is illegal:
C++ Syntax (Toggle Plain Text)
gameBoard >> grid[9][9];
grid doesn't have an index of 9. It has indexes of 0 through 8 due to this declaration:
C++ Syntax (Toggle Plain Text)
int grid[9][9];
grid[n][n] has legal indexes from grid[0][0] through grid[n-1][n-1] in C++. You're going to get a segmentation fault. ![]() |
Similar Threads
- Reading from text file into array (C++)
- Setting an array and reading in a txt file backwards (C++)
- txt data into 2d Array (C++)
- reading txt file into array (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
Other Threads in the C++ Forum
- Previous Thread: complier needed
- Next Thread: Linked List: Mp3 Menu (String and Delete problem)
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux 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 return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






