reading a txt file into a 2d array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 12
Reputation: forgoodor4awsum is an unknown quantity at this point 
Solved Threads: 0
forgoodor4awsum forgoodor4awsum is offline Offline
Newbie Poster

reading a txt file into a 2d array

 
0
  #1
Nov 16th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: reading a txt file into a 2d array

 
0
  #2
Nov 16th, 2008
Can you be a little more specific in your problem with it? What about it is confusing you, or do you have code of your problem?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: forgoodor4awsum is an unknown quantity at this point 
Solved Threads: 0
forgoodor4awsum forgoodor4awsum is offline Offline
Newbie Poster

Re: reading a txt file into a 2d array

 
0
  #3
Nov 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading a txt file into a 2d array

 
0
  #4
Nov 16th, 2008
Originally Posted by forgoodor4awsum View Post
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.
You need to post some code. You need a 2-D array of int and you need a function that reads in the data and you need a function that displays. Here's a skeleton that may help get you started.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void DisplayBoard (int grid[][9])
  7. {
  8. // display grid. Use nested for-loop
  9. }
  10.  
  11. void ReadFromFile (string filename, int grid[][9])
  12. {
  13. // create ifstream
  14. // open ifstream with filename
  15. // read from file into grid using nested for-loop
  16. // close ifstream
  17. }
  18.  
  19. int main ()
  20. {
  21. int grid[9][9];
  22. string filename = "Sudoku_game.txt";
  23. ReadFromFile (filename, grid);
  24. DisplayBoard (grid);
  25. return 0;
  26. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: forgoodor4awsum is an unknown quantity at this point 
Solved Threads: 0
forgoodor4awsum forgoodor4awsum is offline Offline
Newbie Poster

Re: reading a txt file into a 2d array

 
0
  #5
Nov 16th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading a txt file into a 2d array

 
0
  #6
Nov 16th, 2008
Originally Posted by forgoodor4awsum View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: forgoodor4awsum is an unknown quantity at this point 
Solved Threads: 0
forgoodor4awsum forgoodor4awsum is offline Offline
Newbie Poster

Re: reading a txt file into a 2d array

 
0
  #7
Nov 16th, 2008
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

  1. 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.
  1. ifstream in("sudoku_game.txt");
  2.  
  3. int grid[9][9] = "sudoku_game.txt");

THANKS FOR THE HELP!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading a txt file into a 2d array

 
0
  #8
Nov 16th, 2008
Originally Posted by forgoodor4awsum View Post

am i close by thinking it is done this way.
  1. ifstream in("sudoku_game.txt");
  2.  
  3. int grid[9][9] = "sudoku_game.txt");

THANKS FOR THE HELP!
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:

  1. ifstream in("sudoku_game.txt");

This line isn't:

  1. int grid[9][9] = "sudoku_game.txt");

This is better:
  1. ifstream in("sudoku_game.txt");
  2. for (i = ?;?;?)
  3. {
  4. for (j = ?;?;?)
  5. {
  6. in >> grid[i][j];
  7. }
  8. }
  9. in.close ();
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: forgoodor4awsum is an unknown quantity at this point 
Solved Threads: 0
forgoodor4awsum forgoodor4awsum is offline Offline
Newbie Poster

Re: reading a txt file into a 2d array

 
0
  #9
Nov 16th, 2008
Originally Posted by VernonDozier View Post
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:

  1. ifstream in("sudoku_game.txt");

This line isn't:

  1. int grid[9][9] = "sudoku_game.txt");

This is better:
  1. ifstream in("sudoku_game.txt");
  2. for (i = ?;?;?)
  3. {
  4. for (j = ?;?;?)
  5. {
  6. in >> grid[i][j];
  7. }
  8. }
  9. in.close ();



so far i have this as my printBoard

  1. int a,b;
  2. int grid[9][9];
  3.  
  4. ifstream gameBoard;
  5. gameBoard.open("sudoku_game.txt");
  6.  
  7. gameBoard >> grid[9][9];
  8.  
  9.  
  10. gameBoard.close();
  11.  
  12. for (int a=0; a<9; a++) {
  13.  
  14. cout << "\t| ";
  15. }
  16.  
  17.  
  18. for (int b=0; b<9; b++){
  19.  
  20. cout << grid[a][b] << " | ";
  21. cout << endl;
  22. cout << "\t-------------------------------" << endl;
  23. }

but it still isnt printing out the numbers in the txt document.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: reading a txt file into a 2d array

 
0
  #10
Nov 16th, 2008
Originally Posted by forgoodor4awsum View Post
so far i have this as my printBoard

  1. int a,b;
  2. int grid[9][9];
  3.  
  4. ifstream gameBoard;
  5. gameBoard.open("sudoku_game.txt");
  6.  
  7. gameBoard >> grid[9][9];
  8.  
  9.  
  10. gameBoard.close();
  11.  
  12. for (int a=0; a<9; a++) {
  13.  
  14. cout << "\t| ";
  15. }
  16.  
  17.  
  18. for (int b=0; b<9; b++){
  19.  
  20. cout << grid[a][b] << " | ";
  21. cout << endl;
  22. cout << "\t-------------------------------" << endl;
  23. }

but it still isnt printing out the numbers in the txt document.
See my prior post. You need the nested for-loop:

  1. ifstream in("sudoku_game.txt");
  2. for (i = ?;?;?)
  3. {
  4. for (j = ?;?;?)
  5. {
  6. in >> grid[i][j];
  7. }
  8. }
  9. in.close ();

This is illegal:

  1. gameBoard >> grid[9][9];

grid doesn't have an index of 9. It has indexes of 0 through 8 due to this declaration:
  1. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC