8 Replies - 1093 Views - Last Post: 22 September 2016 - 06:55 AM Rate Topic: -----

#1 farha.tamanna11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-March 16

Reading from file.txt to array and use it in conways game of life

Posted 21 September 2016 - 12:29 PM

I am coding a Conway's Game of Life.My task is reading from file.txt to array of strings.And then using this array as the input array of the game. I have written a code about it. But it is full of error. I don't know what is the correct way to do this.
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

    using namespace std;
namespace gamearray {
  int main() 


    ifstream file_("file.txt");
    if(file.is_open())
    {
        string myArray[10];

        for(int i = 0; i < 10; ++i)
        {
		for(int i = 0; i < 10; i++)
			
            file >> array1[j][i];
        }
    }
 return 0; 
}

void copy(int array1[10][10], int array2[10][10])
{
  for(int j = 0; j < 10; j++)
  {
		for(int i = 0; i < 10; i++)
			array2[j][i] = array1[j][i];
	}
}


void life(int array[10][10], char choice)
{

  int temp[10][10];
  copy(array, temp);
	for(int j = 0; j < 10; j++)
 	{
 		for(int i = 0; i < 10; i++)
		{
			if(choice == 'm')
			{

				int count = 0;
				count = array[j-1][i] +
					array[j-1][i-1] +
					array[j][i-1] +
					array[j+1][i-1] +
					array[j+1][i] +
					array[j+1][i+1] +
					array[j][i+1] +
					array[j-1][i+1];

        if(count < 2 || count > 3)
					temp[j][i] = 0;

        if(count == 2)
					temp[j][i] = array[j][i];

        if(count == 3)
					temp[j][i] = 1;
			}


		}
 	}

  copy(temp, array);
}


bool compare(int array1[10][10], int array2[10][10])
{
	int count = 0;
	for(int j = 0; j < 10; j++)
	{
		for(int i = 0; i < 10; i++)
		{
			if(array1[j][i]==array2[j][i])
				count++;
		}
	}

	if(count == 10*10)
	  return true;
	else
	  return false;
}


void print(int array[10][10])
{
	for(int j = 0; j < 10; j++)
 	{
 		for(int i = 0; i < 10; i++)
		{
			if(array[j][i] == 1)
				cout << '*';
			else
				cout << ' ';
		}
		cout << endl;
 	}
}


int main()
{kuppusamy::main(); 

int gen0[10][10];
int todo[10][10];
int backup[10][10];
char neighborhood;
char again;
char cont;
bool comparison;


{do
	{

  do
		{
    cout << "Which neighborhood would you like to use (m): ";
		  cin >> neighborhood;
		}while(neighborhood != 'm');

  system("CLS");
  int i = 0;

  do
		{

			srand(time(NULL));

			for(int j = 0; j < 10; j++)
			{
				for (int i = 0; i < 10; i++)
					gen0[j][i] = rand() % 2;
			}



    if(i == 0)
				copy(gen0, todo);
			copy(todo, backup);
			print(todo);
		  life(todo, neighborhood);
		  i++;


    system("sleep .5");

    if(i % 10 == 1 && i != 1)
    {
      cout << endl;

      do
      {
        cout << "Would you like to continue this simulation? (y/n): ";
        cin >> cont;
      }while(cont != 'y' && cont != 'n');
      if(cont == 'n')
        break;
    }

    comparison = compare(todo, backup);
    if(comparison == false)
      system("CLS");
    if(comparison == true)
      cout << endl;
		}while(comparison == false);
  do
     {
       cout << "Would you like to run another simulation? (y/n): ";
 		  cin >> again;
     }while(again != 'y' && again != 'n');
 	}while(again == 'y');

	return 0;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file.txt to array and use it in conways game of life

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Reading from file.txt to array and use it in conways game of life

Posted 21 September 2016 - 12:30 PM

Please copy/paste your error messages here.
Was This Post Helpful? 0
  • +
  • -

#3 farha.tamanna11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-March 16

Re: Reading from file.txt to array and use it in conways game of life

Posted 21 September 2016 - 12:46 PM

14 expected initializer before 'ifstream' 15 expected unqualified-id before if these are the errors
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Reading from file.txt to array and use it in conways game of life

Posted 21 September 2016 - 12:48 PM

Again.. please copy/paste the error text and not a summary. The information with the error text tends to help point what, where, and in what order, errors are happening.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is online

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: Reading from file.txt to array and use it in conways game of life

Posted 21 September 2016 - 12:58 PM

Why do you have two functions named main()? The function main() is special, there can only be one, and it must always be in the global namespace and it can never be called by another function, including it's self.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 farha.tamanna11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-March 16

Re: Reading from file.txt to array and use it in conways game of life

Posted 22 September 2016 - 02:17 AM

View Postmodi123_1, on 21 September 2016 - 08:30 PM, said:

Please copy/paste your error messages here.

variable 'std::ifstream file' has initializer but incomplete type
Method 'is_open' could not be resolved
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,986
  • Joined: 05-May 12

Re: Reading from file.txt to array and use it in conways game of life

Posted 22 September 2016 - 05:15 AM

Error messages include filenames, line numbers, and line offsets. As two other people have asked, please post the complete error messages. Also, if you have any errors or warnings prior to this, post those as well.
Was This Post Helpful? 0
  • +
  • -

#8 farha.tamanna11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 17-March 16

Re: Reading from file.txt to array and use it in conways game of life

Posted 22 September 2016 - 06:16 AM

Hi, I resolved this writing another code. The new code is here.
 #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstring>
    using namespace std;


    void GetFile();
    bool MakeArray();
    char ChgArray();
    //char GameBoard();

    const int ROW1 =10;
    const int COL1 =10;
    const int BOARD_ROWS(10);
    const int BOARD_COLS(10);
    ifstream myfile;
    string filename;
    char live = 'X';
    char dead = '.';

    char board [BOARD_ROWS][BOARD_COLS];

    int main()
    {
        int q;

        //GetFile();
        if ( MakeArray() ){

          for ( int i(0); i <10; i++)
             ChgArray();
        }
        else {
          cout << "Error parsing input file" << endl;
        }

        cin >> q;
        return 0;
    }


    void GetFile()
    {
        cout<<"Enter the filename: \n";
        cin>>filename;
        return;
    }

    bool MakeArray()
    {
        bool ret(false);
        char val;
        int  totCnt = BOARD_ROWS*BOARD_COLS;
        myfile.open (/*filename.c_str()*/"c_str.txt");
        if ( myfile.is_open() ) {
           for (int r=0; r<ROW1; r++)
           {
        	  for (int c=0; c<COL1; c++)
              {
                 myfile>>val;
                 if ( val == dead || val == live ) {
                    board[r-1][c-1] = val;
                    totCnt--;
                 }
              }
           }
           if ( !totCnt ) {
             ret = true;
           }
           myfile.close();
        }
return ret;
    }
    char getNextState(char b[BOARD_ROWS][BOARD_COLS], int r, int c)
    {
       char ret;

       return ret;
    }
    char ChgArray()
    {
        char boardTmp[BOARD_ROWS][BOARD_COLS];
        for (int r=0; r<BOARD_ROWS; r++)
        {
        	for (int c=0; c<BOARD_COLS; c++)
        	{

                boardTmp[r][c] = getNextState(board,r,c);

                cout << boardTmp[r][c];
        	}
        	cout<<endl;
        }

        memcpy(board,boardTmp,sizeof(board));
        cout << endl;

    }

Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7483
  • View blog
  • Posts: 15,510
  • Joined: 16-October 07

Re: Reading from file.txt to array and use it in conways game of life

Posted 22 September 2016 - 06:55 AM

View Postfarha.tamanna11, on 22 September 2016 - 08:16 AM, said:

I resolved this writing another code. The new code is here.


Um, writing or plagiarizing? It looks suspiciously like response to this question: https://www.daniweb....lp-game-of-life

You can easily find any number of implementations of this program. It is immensely popular. The challenge is to write your own. One approach shows you can program, the other shows you can google.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1