Advertisement
TheWhiteFang

//QUESTION 1 FINALS TRIMESTER 2, 2013/2014

Feb 4th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. //pastebin.com/u/thewhitefang
  2. //QUESTION 1 FINALS TRIMESTER 2, 2013/2014
  3. //Rabbit
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. class Screen{
  11.     char **m_ppScreen;
  12.     int m_Width;
  13.     int m_Height;
  14.  
  15. public:
  16.     //constructor
  17.     Screen(int inWidth, int inHeight)
  18.     {
  19.         m_Width = inWidth;
  20.         m_Height = inHeight;
  21.  
  22.         m_ppScreen = new char *[m_Height]; // pointer-to-pointer
  23.         for (int i = 0; i < m_Height; i++){
  24.             m_ppScreen[i] = new char[m_Width];
  25.         }
  26.         for (int i = 0; i < m_Height; i++){ //initialize each element with a blank space
  27.             for (int j = 0; j < m_Width; j++)
  28.                 m_ppScreen[i][j] = ' ';
  29.         }
  30.     };
  31.     //destructor
  32.     ~Screen(){
  33.         for (int i = 0; i < m_Height; i++){
  34.             delete[] m_ppScreen[i];
  35.         }
  36.         delete[] m_ppScreen;
  37.     }
  38.     //setter function
  39.     void Set(int x, int y, char pixel)
  40.     {
  41.         if (x >= m_Width || y >= m_Height){
  42.             throw string("Input exceeding boundary array!"); //EH throw
  43.         }
  44.         m_ppScreen[y][x] = pixel;};
  45.  
  46.     //works like a getter function
  47.     char Peek(int x, int y)
  48.     {
  49.         if (x >= m_Width || y >= m_Height){
  50.             throw string("Input exceeding boundary array!");//EH throw
  51.         }
  52.         return m_ppScreen[y][x];};
  53.  
  54.     void Display()
  55.     {
  56.         for (int i = 0; i < m_Height; i++){
  57.             for (int j = 0; j < m_Width; j++)
  58.             {
  59.                 cout << m_ppScreen[i][j];
  60.             }
  61.             cout << endl;
  62.         }
  63.         cout << endl;
  64.     };
  65. };
  66.  
  67. int main(){
  68.  
  69.     string name;
  70.     int row = 0;
  71.     int col = 0;
  72.     char v;
  73.  
  74.     ifstream myFile;
  75.     cout << "Enter Filename: ";
  76.     cin >> name;
  77.     myFile.open(name);
  78.     if (myFile.is_open()){
  79.         myFile >> row >> col;
  80.         Screen obj(row, col);
  81.  
  82.         while (!myFile.eof()){
  83.             myFile >> v >> row >> col;
  84.             try{ //try the function
  85.                 obj.Set(row, col, v);
  86.             }
  87.             catch (string &err){ //catch the error
  88.                 cout << err << endl;
  89.             }
  90.         }
  91.         try{
  92.             cout << "Pixel content at location 4,3: " << obj.Peek(4, 3) << endl;
  93.         }
  94.         catch (string &err){
  95.             cout << err << endl;
  96.         }
  97.         myFile.close();
  98.         cout << "Output: " << endl;
  99.         obj.Display();
  100.     }
  101.     else
  102.         cout << "Unable to open file!";
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement