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();
}