Problem with setting a member variable in a constructor

Hello everyone! I am having a two (was three) issues with my program.


2. Aside from the char initialization problem, when I try to declare a puzzle that has more rows than columns (so something like this:

Wordpuzzle(6,1,'B') ), it compiles, but when I run the program, I get a Windows box saying that "Wordpuzzle_Driver.exe has encountered a problem and needs to close. ...".

3. In my constructor "Wordpuzzle(const char* filename)", when I try and initialize the private member variables to some values found within the constructor, I get the same error that occured in problem 2. Is there something incorrect with my syntax? I have been able to readout the values counted correctly from the file, but when I try to initialize the private member variables, I get that error.

Here is the code for the header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * @file Wordpuzzle.h
 * The file for the class Wordpuzzle
 * @author Him Cheng
 */

#ifndef _WORDPUZZLE_H_
#define _WORDPUZZLE_H_

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

/**
 * A class for storing the wordpuzzle and providing basic operations to the puzzle
 */
class Wordpuzzle
{
 public:
  /**
   * Default constructor, the puzzle is set to empty state
   */
  Wordpuzzle();
  
  /**
   * Construct a new row x col rectangular puzzle, all filled in the specified character
   * @param row Number of row in the puzzle
   * @param col Number of col in the puzzle
   * @param c A default letter to fill into the puzzle
   */
  Wordpuzzle(int row, int col, char c);
  
  /**
   * Construct a new rectangular puzzle by reading a file
   * @param filename Filename of the file storing the puzzle
   */
  Wordpuzzle(const char* filename);
  
  /**
   * Copy constructor
   * @param wp another instant of Wordpuzzle to copy from
   */
  //Wordpuzzle(const Wordpuzzle &wp);
  
  /**
   * Destructor
   */
  //~Wordpuzzle();
  
  /**
   * Get the character in [row,col] of the puzzle
   * @param row The row index, starting from 0
   * @param col the column index, starting from 0
   * @return The character stored in the [row,col] of the puzzle
   */
  //char getCharacter(int row, int col) const;
  
  /**
   * Set the character in [row,col] of the puzzle
   * @param row The row index, starting from 0
   * @param col The column index, starting from 0
   * @param c The new letter to be set
   * @return Return true if set successfully, otherwise, return false
   */
  //bool setCharacter(int row, int col, char c);
  
  /**
   * Set the word puzzle instant to a new puzzle by reading a puzzle file
   * @param filename The filename of the file which store the new puzzle
   * @return Return true if set successfully, otherwise, return false
   */
  //bool setPuzzle(const char* filename);
  
  /**
   * Count the number of existance of the word in the puzzle
   * @param word The target word to be counted
   * @return The number of existance
   */
  //int countWord(const std::string &word) const;
  
  /**
   * Resize the puzzle
   * @param row The new number of row in the puzzle
   * @param col The new number of col in the puzzle
   * @param c The character used to fill in the enlarged part
   * @return Return true if resize successfully, otherwise, return false
   */
  //bool resize(int row, int col, char c = '*');
  
  /**
   * Clear the puzzle into empty state
   */
  //void clear();
  
  /**
   * Print out the puzzle
   */
  void print() const;
  
 private:
  /* ====================================
     Add your own function here if needed
     ===================================*/
  char **_data;	///2-D dynamic array to store the puzzle
  int _numOfRow;	///Number of row in the puzzle
  int _numOfCol;	///Number of column in the puzzle
};

#endif //_WORDPUZZLE_H_ 


Here is the code for the implementation file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Wordpuzzle.h"

using namespace std;

//Sets the Dynamic 2-D array pointing to 0
Wordpuzzle::Wordpuzzle()
{
	_data = 0;
}


Wordpuzzle::Wordpuzzle(int row, int col, char c)
{
	_numOfRow = row;
	_numOfCol = col;
	
	_data = new char*[_numOfRow]; //Assigns _data an array (row in length) of pointers that point to chars
	
	for(int index = 0; index < col; index++) //Assigns _data an array (col in length) of pointers that point to chars
	{
		_data[index] = new char[_numOfCol];
	}
	
	for(int rowIndex = 0; rowIndex < _numOfRow; rowIndex++)
	{
		for(int colIndex = 0; colIndex < _numOfCol; colIndex++)
		{ 
    		_data[rowIndex][colIndex] = c;
		}
	}
}


Wordpuzzle::Wordpuzzle(const char* filename)
{
	ifstream InStream;
	
	InStream.open(filename); //Open the file containing the Word Puzzle
	
	if(InStream.fail()) //If the file fails to open, let the user know 
	{
		cout << "Input file failed to open correctly. Please try another file name." << endl;
		exit(1);
	}
	
	int countNumCol = 0, countNumRow = 0;
	bool keepGoing = true;
	char nextCharInFile;
	
	while(keepGoing)
	{
		InStream.get(nextCharInFile); //Read in a character from the file
	    
	    if(nextCharInFile == '\n') // '\n' indicates it has reached the end of a row
	    {
	    	countNumCol--; //Subtract the '\n' character (not a part of a column)
	    	countNumRow++; //Add the last '\n' character (shows the end of the first row)
	    	
	    	while(!InStream.eof()) //Now look until the end of the file for '\n' to find the number of columns
	    	{
	    		InStream.get(nextCharInFile);
	    		
	    		if(nextCharInFile == '\n')
	    		{
	    			countNumRow++;
	    		}
	    	}
	    	
	    	countNumRow++; //Add the last '\n' character at the end of the file
	    	
	    	keepGoing = false;
	    }
	    
	    else if(nextCharInFile == ' ')
	    {
	    	countNumCol--; //We don't care about spaces (not a character we can see)
	    }    
	    
	    countNumCol++; //If the character is not a ' ' or a '\n' then it must be a visible character
	}
	
	cout << "CountNumCol = " << countNumCol << endl;
	cout << "CountNumRow = " << countNumRow << endl;
	
	_numOfRow = countNumRow;
	_numOfCol = countNumCol;
	
	//cout << "_NumOfCol = " << _numOfCol << endl;
	//cout << "_NumOfRow = " << _numOfRow << endl;
	

    
	InStream.clear();
	InStream.seekg(ios_base::beg); //Bring the get pointer back to the beginning of the file 
	
	_data = new char*[_numOfRow]; //Assigns _data an array (row in length) of pointers that point to chars
	
	for(int index = 0; index < _numOfCol; index++) //Assigns _data an array (col in length) of pointers that point to chars
	{
		_data[index] = new char[_numOfCol];
	}
	
	for(int rowIndex = 0; rowIndex < _numOfRow; rowIndex++)
	{
		for(int colIndex = 0; colIndex < _numOfCol; colIndex++)
		{
			char temp;
			InStream >> temp;
			_data[rowIndex][colIndex] = temp;
		}
	}
	
	InStream.close();
}

/*
Wordpuzzle::Wordpuzzle(const Wordpuzzle &wp)
{
	_numOfRow = wp._numOfRow;
	_numOfCol = wp._numOfCol;
	
	for(int rowIndex = 0; rowIndex < wp._numOfRow; rowIndex++)
	{
		for(int colIndex = 0; colIndex < wp._numOfCol; colIndex++)
		{
			_data[rowIndex][colIndex] = wp._data[rowIndex][colIndex];
		}
	}
}
*/
void Wordpuzzle::print() const
{
	for(int rowIndex = 0; rowIndex < _numOfRow; rowIndex++)
	{
		for(int colIndex = 0; colIndex < _numOfCol; colIndex++)
		{
			cout << _data[rowIndex][colIndex];
		}
		
		cout << endl;
	}
}


Here is the code for main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Wordpuzzle.h"

using namespace std;

int main()
{
	Wordpuzzle PuzzleOne("TEST.txt");
	
	PuzzleOne.print();
	cin.get();
	cin.get();
	
	cout << "Bye Bye!"; 
	
	return 0;
}


EDIT: Code has been updated. Problem 1 has been fixed by "firedraco"

THANKS FOR ANY HELP!!!!

Last edited on
1.) Try making putting the defaults in the .h (the = '*') and remove them from the .cpp.

2.) What line exactly is causing the problem? AFAICS, there isn't anything wrong...I'm guessing you might have wrote out of bounds on one of your arrays or something.

3.) _data is not initialized here, go you are accessing invalid memory when you are setting it.
Thanks for your help, firedraco! You have fixed my Problem 1 and *hopefully* problem 3.

In problem 3, in line 85 and 86 of the implementation file, I have replaced "countNumRow" and "countNumCol" with their respective values (constant values i.e. 6 and 8. There are 6 rows and 8 columns in TEST.txt), and that produced the appropriate output.

In Problem 2, I believe the issue is happening at line 85 and 86 in the implementation file. I am unable to debug it (as I have not found a debugger (I am not sure if that is the correct thing to look for?) for the Eclipse IDE).

Once again, thanks for solving those problems firedraco!
2.)
1
2
3
4
5
6
_data = new char*[_numOfRow]; //Assigns _data an array (row in length) of pointers that point to chars
	
	for(int index = 0; index < _numOfCol; index++) //Assigns _data an array (col in length) of pointers that point to chars
	{
		_data[index] = new char[_numOfCol];
	}


In the for loop you are using this->_numOfCol instead of this->_numOfRow.
Thanks again firedraco! I apologize for being lazy on my behalf for not catching those mistakes. This program is now fixed :)
Topic archived. No new replies allowed.