Input from a .txt file issues

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
Well I got a bit lost trying to understand some of your code, so I replaced it.

One thing which caused a problem is the use of variable-length arrays. This is not standard C++, though some compilers will permit it. I changed the code to allocate arrays a and b using new [], which requires a couple of lines of code, and similarly at the end of the program to delete [] the allocate memory.

But to return to the main issues. In function Read_Initial_file you are reading a two-character number into the string ch_row[2]; which is not big enough since it doesn't have room for the null terminator '\0', and then using istringstream to get the integer. I replaced that with a simple read of the integer from the file in one statement first_pattern >> row;.

On to function Read_Ch_from_file. Here I could see you were trying to replace ' ' and '*' with '1' and '0'. But the rest of the code I didn't really follow.

In addition, there are two integers in your input file which represent the row and column where the input pattern is to be placed in your array, but you forgot to either read or do anything with those.

So I replaced all that with code which simply reads each line into a string, then directly copies it to the array, character by character. (Sorry I didn't substitute 0 and 1).

I hope this will at least help you to move forward with this.


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
#include <iostream>
#include <fstream>  
#include <string>  
#include <sstream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstdio>

#pragma hdrstop
#include <keepopen.h>

const int MaxRow = 64;
const int 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)
{
    first_pattern >> row;
    if (row > MaxRow)
        row = MaxRow;

    first_pattern >> column;
    if (column > MaxCol)
        column = MaxCol;
}

void Read_Ch_from_file(char ** a, fstream& first_pattern)
{
    int startrow = 0;
    int startcol = 0;
    first_pattern >> startrow;
    first_pattern >> startcol;

    string line;
    while (getline(first_pattern, line) && (startrow < Rows))
    {
        for (int i=0; i< line.length(); i++)
        {
            if (startcol+i < Columns)
            {
                a[startrow][startcol+i] = line[i];
            }
        }
        startrow++;
    }
}

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

void Display_Generation_Current(char ** a)
{
    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()
{
    return 0;
}

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 = new char * [Rows];
    for (int i=0; i<Rows; i++)
        a[i] = new char[MaxCol];

    char ** b = new char * [Rows];
    for (int i=0; i<Rows; i++)
        b[i] = new char[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();


    // Release the allocated memory
    for (int i=0; i<Rows; i++)
        delete []  a[i];
    delete [] a;

    for (int i=0; i<Rows; i++)
        delete []  b[i];
    delete [] b;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.