Link to home
Start Free TrialLog in
Avatar of edelossantos
edelossantos

asked on

HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I apologize in advance for the reposting of this similar program, I have been working on it all night and it has well beyond exceeded my limitations.  Anyone and all...drastically need help.  This version which I need is hanging up somwhere.  Partially runs but not correctly.  Will be back online at 0930 PDT.Please advise.  

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# Note: If you want to use the g++ compiler on pegasus or helios
# uncomment the g++ lines and comment the Digital Unix lines

# the Digital Unix C++ compiler needs these lines:
CPP = cxx
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall

# the g++ compiler on pegasus and helios needs these lines:
# CPP = g++
# CFLAGS = -DPOSIX_4D9 -w -g

# link in the math library
LFLAGS = -lm

RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:     .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
     ${CPP} -c ${CFLAGS} $<

OFILES=          main.o life.o util.o

HFILES=          life.h  util.h

# dependencies
#
default:     main    
#
main:           $(OFILES)
     ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:          main.cpp life.h util.h
life.o:          life.cpp life.h util.h
util.o:          util.cpp util.h

#
clean:
     ${RM} *.o
     ${RM} core
#
veryclean:     clean
     ${RM}  main  

//util.h

#ifndef UTIL_H  
#define UTIL_H  

#define NULL 0L

#define STDSCREEN 80        
#define DOWN 0                        
#define UP   1
#define END   0
#define START 1
#define FAIL    0
#define SUCCESS 1
#define MISS -1
#define HIT   1

   enum Error_code { success, fail, range_error, underflow, overflow, fatal,
                  not_present, duplicate_error, entry_inserted, entry_found,
                  internal_error };

   void clearScreen ();                    
   void clearTop();                        
   void clearBelowTop();                  
   void goTop();                              
   void topLine(const char * text = " "  );    
   void bottomLine (char * text = " ");  

   void hitAnyKey ();                    
   void flushInput ();                    
   void Warning(char *);                  
   void Error(char *);                    
   bool promptYesNo(char * prompt="");      
   void EatWhiteSpace(void);              

#endif

//util.cpp
   

#include <iostream>
//#include <cstdlib>
//using namespace std;

#include "util.h"

void clearScreen (void)
{
  cout << "\033[2J";              cout << "\033[;f";          
}

void clearTop()
 
  {cout << "\033[0;0f" << "\033[0K";}

void clearBelowTop()
 
  {cout << "\033[1;0f" << "\033[1B" << "\033[J";}

void goTop ()
 
  {cout << "\033[0;0f";}

void topLine(const char str[])
 
  {cout << "\033[0;0f" << "\033[0K" << str;}

void bottomLine (char * str)
   
  {cout << "\033[23;0Hf" << "\033[2K" << str;}

void hitAnyKey ()
{
   char ch;
   bottomLine ("Hit any key to continue...");
   cin.get(ch);
   clearScreen();
}

void flushInput ()
{
   char buff[81];
   if (cin)
      cin.getline(buff,80);    
}

void Warning(char *message)
{
   cout << message;
}

void Error(char *message)
{
   cout << message;
   //exit(1);
}
/*
void EatWhiteSpace()
{
    char ch;
    do {
      cin.get(ch);
    }
   while(isspace(ch));

    if(ch != EOF)
      cin.putback(ch);
}*/

bool promptYesNo(char * c)
{
   char ch;
   cout << c << " [y|n]?";
   do {
      cin.get(ch);  
      //tolower(ch);
      } while (ch != 'y' && ch != 'n');
   return (ch == 'y');
}

//life.h
   
#ifndef LIFE_H
#define LIFE_H

static const int maxrow = 20, maxcol = 60;  

class Life {

public:
   void initialize();
   void initializeRandom();
   void print();
   void update();
   void instructions();

private:
   int grid[maxrow+2][maxcol+2];  
   int neighbor_count(int row, int col);

};

#endif

//life.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

#include <time.h>

using namespace std;

#include "util.h"
#include "life.h"

void Life::initialize()
{
   int row, col;
   for (row = 0; row <= maxrow+1; row++)
      for (col = 0; col <= maxcol+1; col++)
         grid[row][col] = 0;
   
   cout << "\nList coordinate pair for living cells (e.g. 4 5)"
        << " or -1 -1 to end:" << endl;
   cin >> row >> col;

   while (row != -1 || col != -1) {
      if (row >= 1 && row <= maxrow)
         if (col >= 1 && col <= maxcol)
            grid[row][col] = 1;
         else
            cout << "Column " << col << " is out of range." << endl;
      else
         cout << "Row " << row << " is out of range." << endl;
      cin >> row >> col;
   }
}

int Life::neighbor_count(int row, int col)
{
   int i, j;
   int count = 0;
   for (i = row - 1; i <= row + 1; i++)
      for (j = col - 1; j <= col + 1; j++)
          count += grid[i][j];    //  Increase the count if neighbor is alive.
        count -= grid[row][col];  //  Reduce count, since cell is not its own neighbor.
   return count;
}

void Life::update()
{
   int row, col;
   int new_grid[maxrow + 2][maxcol + 2];

   for (row = 1; row <= maxrow; row++)
       for (col = 1; col <= maxcol; col++)
         switch (neighbor_count(row, col)) {
         case 2:
            new_grid[row][col] = grid[row][col];   //  Status stays the same.
            break;
         case 3:
            new_grid[row][col] = 1;                //  Cell is now alive.
            break;
         default:
            new_grid[row][col] = 0;                //  Cell is now dead.
         }

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         grid[row][col] = new_grid[row][col];
}


void Life::print()
{
   int row, col;
   clearScreen();
   cout << "The current Life configuration is:" <<endl;
   for (row = 1; row <= maxrow; row++) {
      for (col = 1; col <= maxcol; col++)
         if (grid[row][col] == 1) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << endl;
}


void Life::instructions()
{
   clearScreen();
   cout << "Welcome to Conway's game of Life." << endl;
   cout << "The grid contains "
        << maxrow << " by " << maxcol;
   cout << " cells, each occupied by an organism or not." << endl;
   cout << "The occupied cells change from generation to generation" << endl;
   cout << "according to the number of neighboring cells which are alive."
        << endl;
}

//main.cpp

#include<iostream>

//using namespace std;

#include "util.h"
#include "life.h"

int main()  {
   Life configuration;
   configuration.instructions();
   configuration.initialize();
   
   char continue_flag = 'n' ;
   cout << "Viewing new generations? " << endl;
   cin>>continue_flag;
   while (  (continue_flag == 'y' ) ||  (continue_flag =='Y')  ) {
             configuration.update();
             cout << "Continue viewing new generations? " << endl;
             cin>>continue_flag;
   }
}

please check...program is hanging up and doesn't run.  This is similar but different version of previous program.  Del




 
 
 
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bcladd
bcladd

What do you mean it doesn't run? Once I reenabled the std namespace the program runs (BCC 5.5 patch level 2, WinXP). It doesn't show any progress (you don't call the print function for the game while it is running) but it runs. To try things out I went ahead and ran it for a few thousand generations and it appears to run correctly (I put a few blinkers in and they alternate positions correctly and steady-state objects live, so it seems cool).

If you could describe the performance you expect AND the performance you are observing then it might be possible to help diagnose your problem. Many of your recent problems (judging from your posts) have had to do with inaccurate assumptions and expectations of the system. So a little more on where you see an error: What input do you use? What would a CORRECT implementation do with that input? What does YOUR implementation do (what, exactly, is the output)? If the code has warnings or errors when compilng, what are they?

Happy to help but given that I can run your code (and watch Life progress when I add a call to print()) I am unclear on what you need.

-bcl
Avatar of edelossantos

ASKER

Back online.  I need the program to generate.  Do you see any inconsistencies in the code?  If so please advise on the corrections.  Del
Did you install the blinkers on the code above or on your machine?  Del
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# Note: If you want to use the g++ compiler on pegasus or helios
# uncomment the g++ lines and comment the Digital Unix lines

# the Digital Unix C++ compiler needs these lines:
CPP = cxx
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall

# the g++ compiler on pegasus and helios needs these lines:
# CPP = g++
# CFLAGS = -DPOSIX_4D9 -w -g

# link in the math library
LFLAGS = -lm

RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:     .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
     ${CPP} -c ${CFLAGS} $<

OFILES=          main.o life.o util.o

HFILES=          life.h  util.h

# dependencies
#
default:     main    
#
main:           $(OFILES)
     ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:          main.cpp life.h util.h
life.o:          life.cpp life.h util.h
util.o:          util.cpp util.h

#
clean:
     ${RM} *.o
     ${RM} core
#
veryclean:     clean
     ${RM}  main  

//util.h

#ifndef UTIL_H  
#define UTIL_H  

#define NULL 0L

#define STDSCREEN 80        
#define DOWN 0                        
#define UP   1
#define END   0
#define START 1
#define FAIL    0
#define SUCCESS 1
#define MISS -1
#define HIT   1

   enum Error_code { success, fail, range_error, underflow, overflow, fatal,
                  not_present, duplicate_error, entry_inserted, entry_found,
                  internal_error };

   void clearScreen ();                    
   void clearTop();                        
   void clearBelowTop();                  
   void goTop();                              
   void topLine(const char * text = " "  );    
   void bottomLine (char * text = " ");  

   void hitAnyKey ();                    
   void flushInput ();                    
   void Warning(char *);                  
   void Error(char *);                    
   bool promptYesNo(char * prompt="");      
   void EatWhiteSpace(void);              

#endif

//util.cpp
   

#include <iostream>
#include <cstdlib>
using namespace std;

#include "util.h"

void clearScreen (void)
{
  cout << "\033[2J";              cout << "\033[;f";          
}

void clearTop()
 
  {cout << "\033[0;0f" << "\033[0K";}

void clearBelowTop()
 
  {cout << "\033[1;0f" << "\033[1B" << "\033[J";}

void goTop ()
 
  {cout << "\033[0;0f";}

void topLine(const char str[])
 
  {cout << "\033[0;0f" << "\033[0K" << str;}

void bottomLine (char * str)
   
  {cout << "\033[23;0Hf" << "\033[2K" << str;}

void hitAnyKey ()
{
   char ch;
   bottomLine ("Hit any key to continue...");
   cin.get(ch);
   clearScreen();
}

void flushInput ()
{
   char buff[81];
   if (cin)
      cin.getline(buff,80);    
}

void Warning(char *message)
{
   cout << message;
}

void Error(char *message)
{
   cout << message;
   exit(1);
}

void EatWhiteSpace()
{
    char ch;
    do {
      cin.get(ch);
    }
   while(isspace(ch));

    if(ch != EOF)
      cin.putback(ch);
}

bool promptYesNo(char * c)
{
   char ch;
   cout << c << " [y|n]?";
   do {
      cin.get(ch);  
      tolower(ch);
      } while (ch != 'y' && ch != 'n');
   return (ch == 'y');
}

//life.h
   
#ifndef LIFE_H
#define LIFE_H

static const int maxrow = 20, maxcol = 60;  

class Life {

public:
   void initialize();
   void initializeRandom();
   void print();
   void update();
   void instructions();

private:
   int grid[maxrow+2][maxcol+2];  
   int neighbor_count(int row, int col);

};

#endif

//life.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

#include <time.h>

using namespace std;

#include "util.h"
#include "life.h"

void Life::initialize()
{
   int row, col;
   for (row = 0; row <= maxrow+1; row++)
      for (col = 0; col <= maxcol+1; col++)
         grid[row][col] = 0;
   
   cout << "\nList coordinate pair for living cells (e.g. 4 5)"
        << " or -1 -1 to end:" << endl;
   cin >> row >> col;

   while (row != -1 || col != -1) {
      if (row >= 1 && row <= maxrow)
         if (col >= 1 && col <= maxcol)
            grid[row][col] = 1;
         else
            cout << "Column " << col << " is out of range." << endl;
      else
         cout << "Row " << row << " is out of range." << endl;
      cin >> row >> col;
   }
}

int Life::neighbor_count(int row, int col)
{
   int i, j;
   int count = 0;
   for (i = row - 1; i <= row + 1; i++)
      for (j = col - 1; j <= col + 1; j++)
          count += grid[i][j];    //  Increase the count if neighbor is alive.
        count -= grid[row][col];  //  Reduce count, since cell is not its own neighbor.
   return count;
}

void Life::update()
{
   int row, col;
   int new_grid[maxrow + 2][maxcol + 2];

   for (row = 1; row <= maxrow; row++)
       for (col = 1; col <= maxcol; col++)
         switch (neighbor_count(row, col)) {
         case 2:
            new_grid[row][col] = grid[row][col];   //  Status stays the same.
            break;
         case 3:
            new_grid[row][col] = 1;                //  Cell is now alive.
            break;
         default:
            new_grid[row][col] = 0;                //  Cell is now dead.
         }

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         grid[row][col] = new_grid[row][col];
}


void Life::print()
{
   int row, col;
   clearScreen();
   cout << "The current Life configuration is:" <<endl;
   for (row = 1; row <= maxrow; row++) {
      for (col = 1; col <= maxcol; col++)
         if (grid[row][col] == 1) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << endl;
}


void Life::instructions()
{
   clearScreen();
   cout << "Welcome to Conway's game of Life." << endl;
   cout << "The grid contains "
        << maxrow << " by " << maxcol;
   cout << " cells, each occupied by an organism or not." << endl;
   cout << "The occupied cells change from generation to generation" << endl;
   cout << "according to the number of neighboring cells which are alive."
        << endl;
}

//main.cpp

#include<iostream>

using namespace std;

#include "util.h"
#include "life.h"

int main()  {
   Life configuration;
   configuration.instructions();
   configuration.initialize();
   
   char continue_flag = 'n' ;
   cout << "Viewing new generations? " << endl;
   cin>>continue_flag;
   while (  (continue_flag == 'y' ) ||  (continue_flag =='Y')  ) {
             configuration.update();
             cout << "Continue viewing new generations? " << endl;
             cin>>continue_flag;
   }
}

make veryclean =

[edeloss2@pegasus proto]$ make veryclean
makefile:31: *** missing separator.  Stop.

this has been an ever ending problem.  Please correct code so the program will run.  I am stuck.  Del





Make sure you use the tab character at the beginning of action lines in a makefile. make is very particular about that.

-bcl
Fixing that let your makefile work for me. I assumed the problem had come from copying the file through an HTML posting.

-bcl
I have, and I am still having problems.
possibly
can you make this code look like the one above?

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# the compiler
CPP = cxx
# compiler flags
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall
# linker flags to link in the libraries libm.a and libtask.a
LFLAGS = -lm -ltask
#
RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:      .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
      ${CPP} -c ${CFLAGS} $<

OFILES=            main.o life.o util.o

HFILES=            life.h  util.h

# dependencies
#
default:      main      
#
main:           $(OFILES)
            ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:            main.cpp life.h util.h
life.o:            life.cpp life.h util.h
util.o:            util.cpp util.h

#
clean:
      ${RM} *.o
      ${RM} core
#
veryclean:      clean
      ${RM}  main  

//util.h
   
#ifndef __USE_STD_IOSTREAM
#define __USE_STD_IOSTREAM
#endif

#ifndef UTIL_H  
#define UTIL_H
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

#define NULL 0L

#define STDSCREEN 80        
#define DOWN 0                        
#define UP   1
#define END   0
#define START 1
#define FAIL    0
#define SUCCESS 1
#define MISS -1
#define HIT   1

enum Error_code { success, fail, range_error, underflow, overflow, fatal,
                  not_present, duplicate_error, entry_inserted, entry_found,
                  internal_error };

void clearScreen ();                   // clears all rows of the screen
void clearTop();                       // clears top row 0 only
void clearBelowTop();                 // clears row 1 down on a vt100 screen
void goTop();                          // moves cursor to the top row 0  
void topLine(const char * text = " "  );   // displays text at row 0
void bottomLine (char * text = " "); // displays text at row 23, column 10

/* I/O functions */
void hitAnyKey ();                   // "Hit any key to continue..." prompt
void flushInput ();                    // reads 80 chars from stdin
void Warning(char *);                  /* writes message to stderr          */
void Error(char *);                    /* writes message to stderr & exits  */
bool promptYesNo(char * prompt="");    /* prompts user to enter yes no */
void EatWhiteSpace(void);              /* discards white space input        */
#endif

//util.cpp
   

#include "util.h"
#include <ctype.h>
 
void clearScreen (void)
{
  cout << "\033[2J";          
  cout << "\033[;f";        
}

void clearTop()

  {cout << "\033[0;0f" << "\033[0K";}

void clearBelowTop()

  {cout << "\033[1;0f" << "\033[1B" << "\033[J";}

void goTop ()

  {cout << "\033[0;0f";}

void topLine(const char str[])

  {cout << "\033[0;0f" << "\033[0K" << str;}

void bottomLine (char * str)
 
  {cout << "\033[23;0Hf" << "\033[2K" << str;}



void hitAnyKey ()
{
   char ch;
   bottomLine ("Hit any key to continue...");
   cin.get(ch);
   clearScreen();
}

void flushInput ()
{
   char buff[81];
   if (cin)
      cin.getline(buff,80);   // flush the input buffer of 80 characters
}

void Warning(char *message)
{
   cerr << message;
}

void Error(char *message)
{
   cerr << message;
   exit(1);
}

void EatWhiteSpace()
{
    char ch;
    do {
      cin.get(ch);
    }
    while(isspace(ch));

    if(ch != EOF)
      cin.putback(ch);
}

bool promptYesNo(char * c)
{
   char ch;
   cout << c << " [y|n]?";
   do {
      cin.get(ch);  
      tolower(ch);
      } while (ch != 'y' && ch != 'n');
   return (ch == 'y');
}

//life.h

#ifndef LIFE_H
#define LIFE_H
   
static const int maxrow = 20, maxcol = 60;  

class Life {

public:
   void initialize();
   void initializeRandom();
   void print();
   void update();
   void instructions();

private:
   int grid[maxrow + 2][maxcol + 2];  
   int neighbor_count(int row, int col);
};

#endif

//life.cpp
   
#include "util.h"
#include "life.h"

void Life::initialize()
{
   int row, col;
   for (row = 0; row <= maxrow+1; row++)
      for (col = 0; col <= maxcol+1; col++)
         grid[row][col] = 0;
   
   cout << "\nList coordinate pair for living cells (e.g. 4 5)"
        << " or -1 -1 to end:" << endl;
   cin >> row >> col;

   while (row != -1 || col != -1) {
      if (row >= 1 && row <= maxrow)
         if (col >= 1 && col <= maxcol)
            grid[row][col] = 1;
         else
            cout << "Column " << col << " is out of range." << endl;
      else
         cout << "Row " << row << " is out of range." << endl;
      cin >> row >> col;
   }
}

int Life::neighbor_count(int row, int col)
{
   int i, j;
   int count = 0;
   for (i = row - 1; i <= row + 1; i++)
      for (j = col - 1; j <= col + 1; j++)
         count += grid[i][j];  //  Increase the count if neighbor is alive.
   count -= grid[row][col]; //  Reduce count, since cell is not its own neighbor.
   return count;
}

void Life::update()
{
   int row, col;
   int new_grid[maxrow + 2][maxcol + 2];

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         switch (neighbor_count(row, col)) {
         case 2:
            new_grid[row][col] = grid[row][col];  //  Status stays the same.
            break;
         case 3:
            new_grid[row][col] = 1;                //  Cell is now alive.
            break;
         default:
            new_grid[row][col] = 0;                //  Cell is now dead.
         }

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         grid[row][col] = new_grid[row][col];
}


void Life::print()
{
   int row, col;
   clearScreen();
   cout << "The current Life configuration is:" <<endl;
   for (row = 1; row <= maxrow; row++) {
      for (col = 1; col <= maxcol; col++)
         if (grid[row][col] == 1) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << endl;
}


void Life::instructions()
{
   clearScreen();
   cout << "Welcome to Conway's game of Life." << endl;
   cout << "The grid contains "
        << maxrow << " by " << maxcol;
   cout << " cells, each occupied by an organism or not." << endl;
   cout << "The occupied cells change from generation to generation" << endl;
   cout << "according to the number of neighboring cells which are alive."
        << endl;
}

//main.cpp
   
#include "util.h"
#include "life.h"

int main()  //  Program to play Conway's game of Life.
{
   Life game;
   game.instructions();
   game.initialize();
   game.print();
   while ( promptYesNo("Continue?")) {
      game.update();
      game.print();
  }
}









What problems are you having? Replacing leading spaces with a tab cleared up the "missing separator" error for me.

If you are having problems of this nature you probably want someone local who can look at your output and watch you fix it. I can tell you what to do (or worse, that it works on MY machine) but I am not completely sure we are communicating. That is, when I say replace the leading spaces with a tab, how to do that depends on the editor that you are running. Since I can't see you typing I don't know if my suggestion is being followed (or, even if it is, if it helped).

Good luck,
-bcl
I do not understand your request
   can you make this code look like the one above?
What is it that you need to do?

-bcl
I am going to try to submit different code.  Local help is not very useful.  Del
New actual code has errors.  Please repair.  Del

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# Note: If you want to use the g++ compiler on pegasus or helios
# uncomment the g++ lines and comment the Digital Unix lines

# the Digital Unix C++ compiler needs these lines:
CPP = cxx
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall

# the g++ compiler on pegasus and helios needs these lines:
# CPP = g++
# CFLAGS = -DPOSIX_4D9 -w -g

# link in the math library
LFLAGS = -lm

RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:      .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
      ${CPP} -c ${CFLAGS} $<

OFILES=            main.o life.o util.o

HFILES=            life.h  util.h

# dependencies
#
default:      main      
#
main:           $(OFILES)
            ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:            main.cpp life.h util.h
life.o:            life.cpp life.h util.h
util.o:            util.cpp util.h

#
clean:
      ${RM} *.o
      ${RM} core
#
veryclean:      clean
      ${RM}  main  
      
// util.h

#ifndef UTIL_H  
#define UTIL_H

#define NULL 0L

#define STDSCREEN 80        
#define DOWN 0                        
#define UP   1
#define END   0
#define START 1
#define FAIL    0
#define SUCCESS 1
#define MISS -1
#define HIT   1

enum Error_code { success, fail, range_error, underflow, overflow, fatal,
                  not_present, duplicate_error, entry_inserted, entry_found,
                  internal_error };

void clearScreen ();                  
void clearTop();                        
void clearBelowTop();                  
void goTop();                              
void topLine(const char * text = " "  );    
void bottomLine (char * text = " ");  

 
void hitAnyKey ();                    
void flushInput ();                    
void Warning(char *);                  
void Error(char *);                    
bool promptYesNo(char * prompt="");    
void EatWhiteSpace(void);              
bool user_says_yes();

#endif

// util.cpp
   
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include "util.h"
 
void clearScreen (void)
{
  cout << "\033[2J";          
  cout << "\033[;f";        
}

void clearTop()

  {cout << "\033[0;0f" << "\033[0K";}

void clearBelowTop()

  {cout << "\033[1;0f" << "\033[1B" << "\033[J";}

void goTop ()

  {cout << "\033[0;0f";}

void topLine(const char str[])
 
  {cout << "\033[0;0f" << "\033[0K" << str;}

void bottomLine (char * str)
 
  {cout << "\033[23;0Hf" << "\033[2K" << str;}

void hitAnyKey ()
{
   char ch;
   bottomLine ("Hit any key to continue...");
   cin.get(ch);
   clearScreen();
}

void flushInput ()
{
   char buff[81];
   if (cin)
      cin.getline(buff,80);    
}

void Warning(char *message)
{
   cerr << message;
}

void Error(char *message)
{
   cerr << message;
   exit(1);
}

void EatWhiteSpace()
{
    char ch;
    do {
      cin.get(ch);
    }
    while(isspace(ch));

    if(ch != EOF)
      cin.putback(ch);
}

bool promptYesNo(char * c)
{
   char ch;
   cout << c << " [y|n]?";
   do {
      cin.get(ch);  
      tolower(ch);
      } while (ch != 'y' && ch != 'n');
   return (ch == 'y');
}

bool user_says_yes()
{
   int c;
   bool initial_response = true;

   do {  
      if (initial_response)
         cout << " (y,n)? " << flush;

      else
         cout << "Respond with either y or n: " << flush;

      do { //  Ignore white space.
         c = cin.get();
      } while (c == '\n' || c ==' ' || c == '\t');
      initial_response = false;
   } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
   return (c == 'y' || c == 'Y');
}

// life.h

#ifndef LIFE_H
#define LIFE_H

const int maxrow = 20, maxcol = 60;    //  grid dimensions

class Life {

public:
   void initialize();
   void print();
   void update();

private:
   int grid[maxrow + 2][maxcol + 2];  //  allows for two extra rows and columns
   int neighbor_count(int row, int col);

};

#endif

//life.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

#include <time.h>

using namespace std;

#include "util.h"
#include "life.h"

int Life::neighbor_count(int row, int col)
{
   int i, j;
   int count = 0;
   for (i = row - 1; i <= row + 1; i++)
      for (j = col - 1; j <= col + 1; j++)
         count += grid[i][j];  
   count -= grid[row][col];
   return count;
}

void Life::update()
{
   int row, col;
   int new_grid[maxrow + 2][maxcol + 2];

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         switch (neighbor_count(row, col)) {
         case 2:
            new_grid[row][col] = grid[row][col];  //  Status stays the same.
            break;
         case 3:
            new_grid[row][col] = 1;                //  Cell is now alive.
            break;
         default:
            new_grid[row][col] = 0;                //  Cell is now dead.
         }

   for (row = 1; row <= maxrow; row++)
      for (col = 1; col <= maxcol; col++)
         grid[row][col] = new_grid[row][col];
}


void instructions()
{
   cout << "Welcome to Conway's game of Life." << endl;
   cout << "This game uses a grid of size "
        << maxrow << " by " << maxcol << " in which" << endl;
   cout << "each cell can either be occupied by an organism or not." << endl;
   cout << "The occupied cells change from generation to generation" << endl;
   cout << "according to the number of neighboring cells which are alive."
        << endl;
}


void Life::initialize()
{
   int row, col;
   for (row = 0; row <= maxrow+1; row++)
      for (col = 0; col <= maxcol+1; col++)
         grid[row][col] = 0;
   cout << "List the coordinates for living cells." << endl;
   cout << "Terminate the list with the special pair -1 -1" << endl;
   cin >> row >> col;

   while (row != -1 || col != -1) {
      if (row >= 1 && row <= maxrow)
         if (col >= 1 && col <= maxcol)
            grid[row][col] = 1;
         else
            cout << "Column " << col << " is out of range." << endl;
      else
         cout << "Row " << row << " is out of range." << endl;
      cin >> row >> col;
   }
}


void Life::print()
{
   int row, col;
   cout << "\nThe current Life configuration is:" <<endl;
   for (row = 1; row <= maxrow; row++) {
      for (col = 1; col <= maxcol; col++)
         if (grid[row][col] == 1) cout << '*';
         else cout << ' ';
      cout << endl;
   }
   cout << endl;
}


bool user_says_yes()
{
   int c;
   bool initial_response = true;

   do {  //  Loop until an appropriate input is received.
      if (initial_response)
         cout << " (y,n)? " << flush;

      else
         cout << "Respond with either y or n: " << flush;

      do { //  Ignore white space.
         c = cin.get();
      } while (c == '\n' || c ==' ' || c == '\t');
      initial_response = false;
   } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
   return (c == 'y' || c == 'Y');
}

// main.cpp

#include<iostream>

using namespace std;

#include "util.h"
#include "life.h"

int main()  //  Program to play Conway's game of Life.
/*
Pre:  The user supplies an initial configuration of living cells.
Post: The program prints a sequence of pictures showing the changes in
      the configuration of living cells according to the rules for
      the game of Life.
Uses: The class Life and its methods initialize(), print(), and update().
      The functions  instructions(),  user_says_yes().
*/

{
   Life configuration;
   instructions();
   configuration.initialize();
   configuration.print();
   cout << "Continue viewing new generations? " << endl;
   while (user_says_yes()) {
      configuration.update();
      configuration.print();
      cout << "Continue viewing new generations? " << endl;
   }
}

make =

[edeloss2@pegasus template]$ make
cxx -c -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall  main.cpp
cxx: Error: main.cpp, line 5: name must be a namespace name
using namespace std;
----------------^
cxx: Error: main.cpp, line 22: identifier "instructions" is undefine
   instructions();
---^
cxx: Info: 2 errors detected in the compilation of "main.cpp".
make: *** [main.o] Error 1

Please help.  Del





I don't understand your problem. The first code you submitted above (original to this question) works. You don't print the board so you don't see it working but it is working (add the print to the main loop and you should be good to go). The last problem you mentioned is a problem with a makefile. That can be fixed by replacing the leading spaces on action lines with a leading tab. (line 31 is an action line; make expects all action lines to begin with a tab so that you COULD have more than one action line and they would be done in order; make just looks for the first line without a tab to mark the end of action lines; if there are no action lines you get the error message)

I am sorry that you don't think you have any local assistance. The instructor should be able to help you with things like makefile formatting.

-bcl
You don't include any files that have std:: functions in main.cpp so using namespace std makes no sense to the compiler.  Try including <iostream> so you get cin, cout, and all of that. Thos variables are actually std::cin and std::cout so they should work

instructions() is defined in life.cpp It is NOT visible in main.cpp. Thus it is not a valid identifier.

-bcl
Hello bcladd,

    I was pressured for time this mornining but I wanted to thank you as well jaime olivares for all of the good information.  This is my first time working with makefile projects and needless to say this code project had my head spinning.  I look forward towards working with you again as well as all of your fellow experts.  Regards.  Del