Input issues from a text file

Hi, recently I've been working on developing source code to run the famous John Conway's Game of Life. My source code compiles and works, but it doesn't have anywhere near the correct output.

The Program at this point is supposed to output an array.
The source of the inputs of the array is supposed to be from a text file that the user can input.

Here is what the text file I input looks like
gun_one.txt
1
2
3
4
5
6
7
8
9
10
50 30 10 5
                         *
                       * *
             **      **            **
            *   *    **            **
 **        *     *   **
 **        *   * **    * *
           *     *       *
            *   *
             **


The Program grabs the number values in the textfile (see Read_Initial_file() function in source code)
But the Program is also supposed to then read the blank spaces and the * characters, if a character is blank the input to the array is 0, if it is a * the input should be 1.

However when I run the code(below)
it compiles properly but only outputs 0's and no 1's.

I am worried that I am using the get() function wrong for inputing characters to the array.
I know the issue has to be occurring in my Read_Ch_from_file() function
But I don't know what the issue is exactly I just need a solution that will allow me to fully input the stars and blanks.

What I think is happening now is the function just continues to read the same line it is on which is full of blank spaces, and therefore no stars (1s) are inputted into my array.

Does anyone have any suggestions?
I've included my Read_Ch_from_file() function, my Read_Initial_file() function separately for ease as well as the entire source code.

Note: Output is handled by the Display_Generation_Current function.



Read_Initial_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Read_Initial_file(fstream& first_pattern, int& row, int& column)
{
char ch_row[2];
first_pattern >> ch_row;
int row_check;
istringstream(ch_row) >> row_check;
if (row_check <= MaxRow)
row = row_check;

char ch_col[2];
first_pattern >> ch_col;
int col_check;
istringstream(ch_col) >> col_check;
if (col_check <= MaxCol)
column = col_check;
}


Read_Ch_from_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
void Read_Ch_from_file(char a[][MaxCol],fstream& first_pattern)
{
bool loop = true;
int bad_char_cnt;

while (loop)
 {
 for(int i=0; i<Rows; i++)
	  for(int j=0; j<Columns; j++)
  {
char temp;
first_pattern.get(temp);

 if (temp == ' ') 
a[i][j] = '*';

 if (temp == '*')
a[i][j] = '1';
else
if ( temp != ' ' || temp != '*')
   {
first_pattern.unget();
bad_char_cnt++;

	if (bad_char_cnt >= 4)
		loop = false;
   }

if (first_pattern.eof())
loop = false;

if (i >= Rows || j >= Columns)
		loop = false;
  }
 }
}


Source Code
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <fstream>  
#include <string>  
#include <sstream>      
#include <vector>  
#include <ctime>   
#include <cstdlib>
#include <stdio.h>

int const MaxRow = 64;
int const MaxCol = 64;
int Rows;
int Columns; 

using namespace std;

void File_Read_Er(string file_name)
{
cout << "Could not open " << file_name << " please try again." << "\n";
}

void Read_Initial_file(fstream& first_pattern, int& row, int& column)
{
char ch_row[2];
first_pattern >> ch_row;
int row_check;
istringstream(ch_row) >> row_check;
if (row_check <= MaxRow)
row = row_check;

char ch_col[2];
first_pattern >> ch_col;
int col_check;
istringstream(ch_col) >> col_check;
if (col_check <= MaxCol)
column = col_check;
}

void Read_Ch_from_file(char a[][MaxCol],fstream& first_pattern)
{
bool loop = true;
int bad_char_cnt;

while (loop)
 {
 for(int i=0; i<Rows; i++)
	  for(int j=0; j<Columns; j++)
  {
char temp;
first_pattern.get(temp);

 if (temp == ' ') 
a[i][j] = '0';

 if (temp == '*')
a[i][j] = '1';
else
if ( temp != ' ' || temp != '*')
   {
first_pattern.unget();
bad_char_cnt++;

	if (bad_char_cnt >= 4)
		loop = false;
   }

if (first_pattern.eof())
loop = false;

if (i >= Rows || j >= Columns)
		loop = false;
  }
 }
}

void Clear_life(char a[][MaxCol])
{
   for(int i=0; i<Rows; i++)
	  for(int j=0; j<Columns; j++)
		 a[i][j]=' ';
}

void Display_Generation_Current(char a[][MaxCol])
{
   for(int i=0; i<Rows; i++) // i=1; i<=nrows
	{
	  for(int j=0; j<Columns; j++) // j= 
 	cout << a[i][j];
  	cout << endl;
	}
   cout << endl;
}


int Neighbor_Cells()
{
}

void Initial_Generation(char a[][MaxCol])
{
 int r, c;
  srand(time(NULL));
  int stars = rand() % (MaxRow*MaxCol);
  for(int i=0; i<stars; i++)
  {
	  r = rand() % MaxRow;
	  c = rand() % MaxCol;
	  a[r][c]='*';
  }
}

void Next_Generation()
{
}


int main()
{


cout << "Please enter the initial pattern you wish to use: " << "\n";
string file_pattern_in;
cin >> file_pattern_in;

fstream initial_pat;
initial_pat.open(file_pattern_in.c_str());
if (initial_pat.fail())
{
File_Read_Er(file_pattern_in);
cout << "Please specify a correct file name: " << "\n";
cin >> file_pattern_in;
initial_pat.open(file_pattern_in.c_str());
}
else
Read_Initial_file(initial_pat, Rows, Columns);


char a[Rows][MaxCol];
char b[Rows][MaxCol];
Clear_life(a);
Clear_life(b);

Read_Ch_from_file(a, initial_pat);



cout << "Please enter the number of generations to compute: " << "\n";
int NumGen;
cin >> NumGen;

 for(int i=0;i<NumGen; i++)
  {      cout << "Generation " << i << endl;
         if (i%2==0)
          { Display_Generation_Current(a);
	     Next_Generation();
          }
         else 
	  { Display_Generation_Current(b);
	     Next_Generation();	
          }
  }

initial_pat.close();
return 0;
}



Any help here would be greatly appreciated
You need to go through your while (loop) and add {} after each for and if and format it so you can tell what it is doing. The problem is there.

I found if I put this at the bottom of your while (loop), i can get it to stop looping out of control.

1
2
3
loop = false;  // IF you remove this the program loops.
 }
}



Once I put in brackets, I had to change
1
2
     for(int i=0; i <= Rows; i++)
     for(int j=0; j <=Columns; j++)


Row and Columns were 0 and your program never ran that code.
I think one problem is Row and Columns are not being set correctly.

Last edited on
Topic archived. No new replies allowed.