Thread: Quick: Alternative to using global variable

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    Quick: Alternative to using global variable

    Hi All,

    I have a list of a class "row" - which contains a list of chars.
    I have a static variable "TOTAL_ROWS" for the row class in my code which I want to update every time I add a new row to my list of the row classes.

    I have done that successfully. That means I can always access the total amount of rows when running tests.

    However, my lecturer's submit program has stated that it is a global variable and I am not allowed to use them for this assignment. I'll post the code below. I thought if it was private in the class definition this would make it OK?

    Does anyone have an alternate suggestion? Apologies for the large chunk of code I'm posting - you need to see all of it if you want to see how it uses the total row number.

    Note - the compiler picks up the error directly after the class definition when I declare the TOTAL_ROWS variable. Also, I'm just showing a summary of my code.

    Code:
    #include <iostream>
    #include <fstream>
    #include <list>
    #include <ctype.h>
    #include <cstdlib>
    
    using namespace std;
    
    class row 
    {
       private:
       
       list<char> row_chars;
       int row_number, char_position, row_size;
       char current_char;
       static int TOTAL_ROWS;
          
       public:   
       
       row(int number) ;   
       void store_chars(char ch);
       void new_row();
       void print_row(list<char>::iterator &list_iter);
       bool is_match(list<char>::iterator &list_iter, char data);
       void clear_row();
       bool is_char_valid();
       bool not_first();
       bool not_last();
       bool is_outside_maze(char findData);
       bool check_counts(int hash, int s, int f);
       int get_row_number();
       int get_char_position();
       char get_char(list<char>::iterator &list_iter, int position);
       int next_valid_position(list<char>::iterator &list_iter, int old_position);
       int get_size();
       static int get_TOTAL_ROWS();
       static void increase_TOTAL_ROWS();
    };
    
    int row::TOTAL_ROWS;
    
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein);
    void print_maze(list<row> &maze_rows);
    bool find_char(const char &findData, 
       list<row>::iterator &itr, list<row> &maze_rows);
    int count_char(const char &findData, 
       list<row>::iterator &itr, list<row> &maze_rows);
    bool is_maze_valid(list<row>::iterator &itr, list<row> &maze_rows);
    bool is_outside(const char &findData, 
       list<row>::iterator &itr, list<row> &maze_rows);
    int test_maze(list<row> &maze_rows);
    int tally_metrics(int x, list<row>::iterator &itr, list<row> &maze_rows);
    void calculate_metrics(list<row> &maze_rows);
    int add_metrics(int i, char up, char forward, 
       char down, char back, char start);
    
    int main (int argc, char *argv[]) 
    {
       string filein;
       list<row> maze_rows;   
       
       row row_chars(1);
       
       //Make sure a maze file was provided   
       if (argc != 2) 
       {
          cout << "Must supply 1 argument to this program\n";
          return 0; 
       }
      
       filein = argv[1];
      
       //Loading the file into lists...  
       load_maze(maze_rows, row_chars, filein);
      
       //Running the tests on the lists...  
       if(test_maze(maze_rows) == 0)
       {
          cout << "Unable to load maze " << filein << " \n";
          exit(0);
       }
        
       print_maze(maze_rows);
      
       //Calculate and print the maze metrics...  
       calculate_metrics(maze_rows);
    
       return 0; 
    }
    
    row::row(int number)
    {
       row_number = number;
       char_position = row_size = 0;
       TOTAL_ROWS = 0;
    }
    
    int row::get_TOTAL_ROWS()
    {
       return TOTAL_ROWS;
    }
    
    bool row::not_first()
    {
       if(row_number > 1)
          return true;
       else
          return false;
    }
    
    bool row::not_last()
    {
       if(row_number < TOTAL_ROWS)
          return true;
       else
          return false;
    }
    
    void row::increase_TOTAL_ROWS()
    {
       TOTAL_ROWS++;
    }
    
    void load_maze(list<row> &maze_rows, row &row_chars, const string &filein) 
    {
       ifstream fin;
       char ch;
      
       //open stream to filein
       fin.open(filein.c_str());
       if (!fin) 
       {
          cerr << "Unable to load maze " << filein << " \n";
          exit(0); 
       }
      
       while (!fin.eof()) 
       {
          fin.get(ch);
          row_chars.store_chars(ch);
          list<row>::iterator itr = maze_rows.begin();
          itr->increase_TOTAL_ROWS();      
          if(ch == '\n') 
          {         
             maze_rows.push_back(row_chars);
             row_chars.clear_row();
             row_chars.new_row();
             itr->increase_TOTAL_ROWS();        
          }
       }
      
       fin.close(); 
    }
    Last edited by abrownin; 05-01-2010 at 11:59 PM. Reason: Summarised code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > list<row> maze_rows;
    So make a maze class, which contains this, and the number of rows (as private members)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 02:30 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM