C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 1st, 2005, 08:38 PM
jambawalla jambawalla is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 jambawalla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 21 sec
Reputation Power: 0
debugging help - life game problem

I just wrote a c++ program based on Conway's Game of Life, but it fails to calculate the next generation, it just shows a blank box with no asterisks inside.
Here's the code:

#include <iostream>
#include <iomanip>

#include <string>

#include <cctype>

#include <cstdlib>

usingnamespace std;

int star_birth ( char[15][15], int, int );

void printGen ( char [15][15], int& );

void getChoice (char&, char [15][15], int&);

void calcGen ( char[15][15], int&);

void toIni ( int [15][15], char [15][15] );

void outerBox (char[15]);

void iniAray (char [15], char [15][15]);

void getInput ( char [15][15] );

void errChk (int&);

void main (void)

{

char ary[15][15] = {0};

char choice = ' ';

char disp[15] = {0};

int which_gen = 0;

cout << setw(50) << "***Life Simulation***" << endl;

cout << endl;



outerBox (disp);

iniAray (disp,ary);

getInput (ary);

printGen (ary,which_gen);

getChoice (choice,ary,which_gen);

}

void iniAray (char disp[15], char ary[15][15])

{

int row = 0;

int col = 0;

for (row=0; row<15; row++)

{

for (col = 0; col < 15; col++)

{

if (row == 0 || row == 14)

ary[row][col] = disp[col];

elseif (col == 0 || col == 14)

ary[row][col] = '|';

else

ary[row][col] = ' ';

}

}

}

void getInput (char ary[15][15])

{

int row = 0;

int col = 0;

do

{

cout << "Enter coordinate pair (1 - 13 or quit 99) ---> ";

cin >> row;

errChk (row);

if ( row == 99 )

break;

cout << setw(50) << " ";

cin >> col;

errChk (col);

ary[row ][col] = '*';

}
while (col != 99);

cout << endl;

}

void outerBox (char disp[15])

{

for ( int num = 0; num < 15; num++)

{

if ( num == 0 || num == 14)

disp[num] = '+';

else

disp[num] = '-';

}

}





void errChk (int& num)

{

while (num <= 0 || num >= 14 && num != 99)

{

cout << "Invalid Input! Must be from 1 - 13 ---> ";

cin >> num;

}

}

void calcGen (char ary[15][15], int& which_gen)

{

int func[15][15];

toIni (func, ary);

for (int row=0; row<14; row++)

{

for (int col=0; col<14; col++)

{

func[row][col] = star_birth(ary,row,col);

}

}

for (int row=0; row<14; row++)

{

for (int col=0; col<14; col++)

{

if ( ary[row][col] == '*' )

{

if (func[row][col] == 2 || func[row][col] == 3)

ary[row][col] = '*';

else ary[row][col] = ' ';

}

if (ary[row][col] == ' ' )

{

if (func[row][col] == 3)

ary[row][col] = '*';

}

}

}

printGen(ary, which_gen);

}

int star_birth ( char ary [15][15], int row, int col)

{

int num = 0;

int ndx = 0;

for(ndx=0; ndx<8; ndx++)

{

if (ary[row-1][col-1] == '*')

num = num + 1;

}

return num;

}

void printGen ( char ary[15][15], int& gen_which)

{

cout << "Life pattern for Generation Number " << gen_which << endl;

cout << endl;

for (int row=0; row<15; row++)

{

for (int col=0; col<15; col++)

cout << ary[row][col];

cout << endl;

}

cout << endl;

cout << endl;

gen_which++;

}

void getChoice (char& action, char ary[15][15], int& wich_gen)

{

char ans;

do

{

cout << "Next or Quit (N or Q) ? ----> ";

cin >> ans;

ans = toupper(ans);

calcGen(ary,wich_gen);



}
while (ans == 'N');



}



void toIni ( int func [15][15], char ary[15][15])

{

for (int row=0; row<15; row++)

{

for (int col=0; col<15; col++)

{

func[row][col] = 0;

}

}

}

//End of program

The formatting is a bit messed up when posting, but the main gist of the code is all there. I would appreciate help/explanation of any kind. Thanks very much,

Jambawalla

Reply With Quote
  #2  
Old May 1st, 2005, 09:04 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
First, you can preserve indenting easily simply by using the [ code ] tags


So, you problem is that when the user selects "n" to continue, it program fails to prompt for coordinates and simply displays an empty box?

I would recommend slightly changing the flow of the program to be a little more simple, so that the loop is in main().... change your getChoice() function to this:

Code:
char getChoice (char& action, char ary[15][15], int& wich_gen)
{
   char ans;

   cout << "Next or Quit (N or Q) ? ----> ";

   cin >> ans;

   ans = toupper(ans);

   return(ans);
}


and change your main() function to this:

Code:
void main (void)

{

char ary[15][15] = {0};

char choice = ' ';

char disp[15] = {0};

int which_gen = 0;

cout << setw(50) << "***Life Simulation***" << endl;

cout << endl;


do {
outerBox (disp);

iniAray (disp,ary);

getInput (ary);

printGen (ary,which_gen);

} while (toupper(getChoice (choice,ary,which_gen)) == 'N');

}


hope that helps....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old May 2nd, 2005, 12:41 AM
jambawalla jambawalla is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 3 jambawalla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 21 sec
Reputation Power: 0
there's still a problem...

Firstly, Thanks a lot B-Con for the reply. I incorporated your changes but I didn't explain the way the program works
well enough. Once the user inputs the coordinates, the program displays the first generation and asks the user if he
wants to continue to display the next generation ('n') or quit ('q'). If the user selects 'n', the program should calculate
the next generation and display it without the user giving any further input (in terms of coordinates, etc.). Right now,
when I choose 'n', the program takes me back to inputting coordinates to create a new first generation. I want it to
display the next generation calculated correctly. Any ideas? Thanks again,

Jamba

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > debugging help - life game problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT