Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 2.78 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. #define MAX_FILENAME_LEN 30
  9.  
  10. struct Maze
  11. {
  12.   char* cells;
  13.   int width;
  14.   int length;
  15. };
  16.  
  17. void write_maze(Maze &maze);
  18. void read_maze(bool &format_check, int &width, int &length, ifstream &maze_in);
  19. void solve();
  20. void open_input_file(ifstream &in, const char prompt[]);
  21. void determine_size(ifstream &in, int &width, int &length, bool &format_check);
  22.  
  23. int main()
  24. {
  25.  
  26.   ifstream maze_in;
  27.   bool format_check = true;
  28.   int width = 0; int length = 0; int index = 0;
  29.   char ch;
  30.   Maze maze;
  31.  
  32.   read_maze(format_check, width, length, maze_in);
  33.  
  34.   if(!format_check)
  35.     {
  36.       cout << "There is an error in your input file, please check the file and \
  37. re-start program" <<  endl;
  38.     }
  39.   else
  40.     {
  41.       maze.width = width;
  42.       maze.length = length;
  43.       cout << "creating an array of size " << width * length << endl;
  44.       maze.cells = new char[width * length];
  45.       while(!maze_in.eof())
  46.         {
  47.           maze_in.get(ch);
  48.           if(maze_in.eof())
  49.            break;
  50.           maze.cells[index] = ch;
  51.           index++;
  52.         }
  53.       write_maze(maze);
  54.     }
  55. }
  56.  
  57. void write_maze(Maze &maze)
  58. {
  59.   int index = 0;
  60.  
  61.   cout << endl;
  62.   cout << maze.length << " " << maze.width << endl;
  63.   cout << maze.length * maze.width << endl;
  64.  
  65.   for(int row = 0; row < maze.length; row++)
  66.     {
  67.       for(int col = 0; col <= maze.width; col++)
  68.         {
  69.           cout << maze.cells[index];
  70.           index++;
  71.         }
  72.     }
  73.   cout << "\n";
  74.   cout.flush();
  75. }
  76.  
  77. void read_maze(bool &format_check, int &width, int &length, ifstream &maze_in)
  78. {
  79.   open_input_file(maze_in, "enter the name of the maze file           :");
  80.   determine_size(maze_in, width, length, format_check);
  81.   //to do: find start location
  82. }
  83.  
  84. void open_input_file(ifstream &in, const char prompt[])
  85. {
  86.   bool success = false;
  87.   char filename[MAX_FILENAME_LEN + 1];
  88.  
  89.   cout << prompt;
  90.   cin.getline(filename, MAX_FILENAME_LEN + 1, '\n');
  91.  
  92.   in.open(filename);
  93.  
  94.   while(success == false)
  95.     {
  96.       if (in.fail())
  97.         {
  98.           cout << "couldn't open file" << endl;
  99.           cout << prompt;
  100.           cin.getline(filename, MAX_FILENAME_LEN + 1, '\n');
  101.           in.open(filename);
  102.           if(!in.fail())
  103.             success = true;
  104.         }
  105.       else
  106.         {
  107.           success = true;
  108.         }
  109.     }
  110. }
  111.  
  112. void determine_size(ifstream &in, int &width, int &length, bool &format_check)
  113. {
  114.   char line_in[100];
  115.   int first_width;
  116.   in.getline(line_in, 100, '\n');
  117.   length++;
  118.   first_width = strlen(line_in);
  119.   while(!in.eof() && format_check)
  120.     {
  121.       in.getline(line_in, 100, '\n');
  122.       length++;
  123.       width = strlen(line_in);
  124.       if(width != first_width)
  125.         {
  126.           format_check = false;
  127.         }
  128.     }
  129.   width = first_width;
  130.   in.clear();
  131.   in.seekg(0, ios::beg);
  132. }