Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //pastebin.com/u/thewhitefang
- //QUESTION 1 FINALS TRIMESTER 2, 2013/2014
- //Rabbit
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class Screen{
- char **m_ppScreen;
- int m_Width;
- int m_Height;
- public:
- //constructor
- Screen(int inWidth, int inHeight)
- {
- m_Width = inWidth;
- m_Height = inHeight;
- m_ppScreen = new char *[m_Height]; // pointer-to-pointer
- for (int i = 0; i < m_Height; i++){
- m_ppScreen[i] = new char[m_Width];
- }
- for (int i = 0; i < m_Height; i++){ //initialize each element with a blank space
- for (int j = 0; j < m_Width; j++)
- m_ppScreen[i][j] = ' ';
- }
- };
- //destructor
- ~Screen(){
- for (int i = 0; i < m_Height; i++){
- delete[] m_ppScreen[i];
- }
- delete[] m_ppScreen;
- }
- //setter function
- void Set(int x, int y, char pixel)
- {
- if (x >= m_Width || y >= m_Height){
- throw string("Input exceeding boundary array!"); //EH throw
- }
- m_ppScreen[y][x] = pixel;};
- //works like a getter function
- char Peek(int x, int y)
- {
- if (x >= m_Width || y >= m_Height){
- throw string("Input exceeding boundary array!");//EH throw
- }
- return m_ppScreen[y][x];};
- void Display()
- {
- for (int i = 0; i < m_Height; i++){
- for (int j = 0; j < m_Width; j++)
- {
- cout << m_ppScreen[i][j];
- }
- cout << endl;
- }
- cout << endl;
- };
- };
- int main(){
- string name;
- int row = 0;
- int col = 0;
- char v;
- ifstream myFile;
- cout << "Enter Filename: ";
- cin >> name;
- myFile.open(name);
- if (myFile.is_open()){
- myFile >> row >> col;
- Screen obj(row, col);
- while (!myFile.eof()){
- myFile >> v >> row >> col;
- try{ //try the function
- obj.Set(row, col, v);
- }
- catch (string &err){ //catch the error
- cout << err << endl;
- }
- }
- try{
- cout << "Pixel content at location 4,3: " << obj.Peek(4, 3) << endl;
- }
- catch (string &err){
- cout << err << endl;
- }
- myFile.close();
- cout << "Output: " << endl;
- obj.Display();
- }
- else
- cout << "Unable to open file!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement