Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- #include <sstream>
- #include <cmath>
- using namespace std;
- void Default_Display(int board[4][4]);
- void moveLeft();
- void moveRight();
- void moveUp();
- void moveDown();
- int main(){
- int board[4][4];
- ifstream configfile;
- int ROW, COL;
- string filename;
- cout << "Enter the initial configuration file:" << endl;
- cin >> filename;
- configfile.open(filename.c_str());
- if(!configfile.is_open()){
- cout << "File not found, using default start configuration" << endl;
- Default_Display(board); //when using an array as an argument, there is no need
- //to declare the size of the array again (board[4][4])
- //Just calling the name of the array will be fine.
- //Technically speaking, when you do Default_Display(board[4][4])
- //You are not passing the array, you are passing a pointer to the 4th element
- // of the 4th row
- }
- else if(configfile.is_open()){
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4; j++){
- configfile >> ROW;
- board[i][j] = ROW;
- cout << board[i][j] << " " ;
- }
- cout << "\n" << endl;
- }
- }
- configfile.close();
- //Why this didnt work (your code)
- /*
- configfile >> ROW >> COL; // What are you doing here? your looking into the text file and taking 2 consecutive numbers?
- 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
- for(int j = 0; j < COL; j++){
- configfile >> board[i][j]; //Did you think that doing configfile >> ROW >> COL would give
- cout << board[i][j] << " " ; //ROW = 4 and COL = 4 ?
- }
- cout << "\n" << endl;
- }
- */
- }
- void Default_Display(int board[4][4]){
- board[0][0] = 0;
- board[0][1] = 0;
- board[0][2] = 0;
- board[0][3] = 0;
- board[1][0] = 0;
- board[1][1] = 0;
- board[1][2] = 0;
- board[1][3] = 0;
- board[2][0] = 0;
- board[2][1] = 0;
- board[2][2] = 0;
- board[2][3] = 0;
- board[3][0] = 0;
- board[3][1] = 0;
- board[3][2] = 0;
- board[3][3] = 2;
- for (int i=0; i < 4; i++){
- for(int j = 0; j < 4; j++){
- cout << board[i][j]<< " " ;
- }
- cout << "\n " << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement