Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <cmath>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. void Default_Display(int board[4][4]);
  12. void moveLeft();
  13. void moveRight();
  14. void moveUp();
  15. void moveDown();
  16.  
  17.  
  18.  
  19. int main(){
  20.  
  21.     int board[4][4];
  22.     ifstream configfile;
  23.     int  ROW, COL;
  24.     string filename;
  25.     cout << "Enter the initial configuration file:" << endl;
  26.     cin >> filename;
  27.  
  28.     configfile.open(filename.c_str());
  29.     if(!configfile.is_open()){
  30.         cout << "File not found, using default start configuration" << endl;
  31.         Default_Display(board); //when using an array as an argument, there is no need
  32.                                 //to declare the size of the array again (board[4][4])
  33.                                 //Just calling the name of the array will be fine.
  34.                                 //Technically speaking, when you do Default_Display(board[4][4])
  35.                                 //You are not passing the array, you are passing a pointer to the 4th element
  36.                                 // of the 4th row
  37.     }
  38.  
  39.     else if(configfile.is_open()){
  40.         for(int i = 0; i < 4; i++){
  41.  
  42.             for(int j = 0; j < 4; j++){
  43.  
  44.                 configfile >> ROW;
  45.                 board[i][j] = ROW;
  46.                 cout << board[i][j] << " " ;
  47.  
  48.             }
  49.             cout << "\n" << endl;
  50.         }
  51.     }
  52.  
  53.     configfile.close();
  54.  
  55.     //Why this didnt work (your code)
  56.     /*
  57.      configfile >> ROW >> COL;     // What are you doing here? your looking into the text file and taking 2 consecutive numbers?
  58.     for (int i=0; i < ROW; i++){    //also its outside the for loop, so you only do it once. so you only ever get the first 2 numbers
  59.         for(int j = 0; j < COL; j++){
  60.             configfile >> board[i][j];   //Did you think that doing configfile >> ROW >> COL would give
  61.             cout << board[i][j] << " " ; //ROW = 4 and COL = 4 ?
  62.         }
  63.         cout << "\n" << endl;
  64.  
  65.     }
  66.     */
  67.  
  68. }
  69.  
  70. void Default_Display(int board[4][4]){
  71.  
  72.  
  73.     board[0][0] = 0;
  74.     board[0][1] = 0;
  75.     board[0][2] = 0;
  76.     board[0][3] = 0;
  77.     board[1][0] = 0;
  78.     board[1][1] = 0;
  79.     board[1][2] = 0;
  80.     board[1][3] = 0;
  81.     board[2][0] = 0;
  82.     board[2][1] = 0;
  83.     board[2][2] = 0;
  84.     board[2][3] = 0;
  85.     board[3][0] = 0;
  86.     board[3][1] = 0;
  87.     board[3][2] = 0;
  88.     board[3][3] = 2;
  89.     for (int i=0; i < 4; i++){
  90.             for(int j = 0; j < 4; j++){
  91.                 cout << board[i][j]<< " " ;
  92.             }
  93.             cout << "\n " << endl;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement