Tamanna Islam Tamanna Islam - 2 months ago 15
C++ Question

Conway's game of life error

I am writing Conway's game of life. The code runs perfectly but after running in the Console window it is written that "The "clear" command is either misspelled or
could not be found." I don't get the point. Why this is happening? How can I fix the clear command?

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

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()
{
int gen0[10][10];
int todo[10][10];
int backup[10][10];
char neighborhood;
char again;
char cont;
bool comparison;
string decoration;




do
{

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

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

Answer

Window clearing method depends on system. It appears that Yours doesn't know command "clear". Try with system("CLS")

Also you may be interested in looking at: system("clear"); not working but system("cls"); works

Comments