so im trying to build a connect 4 gameboard using charecter arrays and it has to look like this
O | O | O | O
-------------
O | O | O | O
-------------
O | O | O | O
-------------
O | O | O | O


Column: A B C D


so far ive got everything working except for the barriers on the odd numbered lines, it just gives me gibberish for some reason

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
 
// ----------- Function definitions -------------

void setgameboard(char board[7][12]){
    int i,j,k;
     for (i=0;i<7;i++){//row loop
         if (i==0||i==2||i==4||i==6){ //if row isnt barrier
                        for (j=0;j<13;j++){//start row that isnt barrier
                              if(j==0||j==4||j==8||j==12)  {board [i][j]='O';} //is open space for token
                               if(j==1||j==3||j==5||j==7||j==9||j==11){board[i][j]=' ';}//is space
                               if(j==2||j==6||j==10){board[i][j]='|';}//is divider
                                   }  //close if row isnt barrier loop
          if (i==1||i==3|| i==5){for (k=0;k<13;k++){board[i][k]='-';}}          //if row is barrier            
                                             } //close column loop
}//close row loop

}
     
     int main()
{

       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
char board[7][12];
int i,j;
        //--------Main Program Body----------
    cout << "**** ***"<<endl<<endl; //title

setgameboard(board);
for (i=0;i<7;i++){ for (j=0;j<13;j++){cout<<board[i][j];}cout<<endl;}

  cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

is it just something with my print or am i making a mistake with the function? is there an easier way to do this? please help! thanks!

Couple of things....

1) your arrays/indexes are not in sync. Your board is "char[7,12]", meaning the indexes go from 0..6 and 0..11, respectively. Look at some of your loops - you have them checking for "j<13", meaning "j" can go up to 12, which is outside the bounds of the array. I was getting error messages when I ran the code about the stack being corrupt.

2) Your "for" loop for drawing the dashes (on your odd lines) was inside the "if" statement checking for the even lines. Once I properly indented the code it jumped right out at me. Like this:

void setgameboard(char board[7][12])
{
    int i,j,k;
     for (i=0;i<7;i++)
     {//row loop
         if (i==0||i==2||i==4||i==6)
         { //if row isnt barrier
            for (j=0;j<13;j++)
             {//start row that isnt barrier
	         if(j==0||j==4||j==8||j==12)
	         {
		     board [i][j]='O';
	         } //is open space for token
                 if(j==1||j==3||j==5||j==7||j==9||j==11)
	         {
		     board[i][j]=' ';
	         }//is space
                 if(j==2||j==6||j==10)
	         {
		     board[i][j]='|';
	         }//is divider
             }  //close if row isnt barrier loop
             if (i==1||i==3|| i==5)
	     {
	         for (k=0;k<13;k++)
	         {
		     board[i][k]='-';
	         }
             }          //if row is barrier            
        } //close column loop
    }//close row loop
}

Lines 23-29 should be moved below line 30, to be out of the "if" statement on line 6.

You are making it hard on yourself. The board should be 4x4 because those are the playing positions. On output you should add the borders. That would alleviate all the checking to see if you are going to trash a border or not.

You are making it hard on yourself. The board should be 4x4 because those are the playing positions. On output you should add the borders. That would alleviate all the checking to see if you are going to trash a border or not.

i was blind but now i see thanks!! so your saying something like this:

void printboard(char board[4][4]){
    int i,j,k;
    for(i=0;i<4;i++){
    cout<<board[i][0]<<" | ";
    cout<<board[i][1]<<" | ";
    cout<<board[i][2]<<" | ";
    cout<<board[i][3]<<cout<<endl;
    if(i!=3){cout<<"-------------"<<endl;}}

}    
     
     
     
     
     
     void setgameboard(char board[4][4]){
         int i,j; 
          for(i=0;i<4;i++){for(j=0;j<4;j++){board[i][j]='O';}}
        board[1][4]='\0';  }
     int main()
{

       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
char board[4][4];
        //--------Main Program Body----------
    cout << "**** ***"<<endl<<endl; //title
setgameboard(board);
printboard(board);
  cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

but i still get gibberish on the end of each line is something wrong with my null?

Line 19: what's that for? Again, since you are using the index of "4", that's past the end of the array (an array declared "char board[4,4]" will have indexes of 0..3 and 0..3, respectively.

Also, line 7 should be:

cout<<board[i][3]<<endl;

The extra "cout" would cause problems.

Please work on your formatting. Your code is difficult to follow. This should help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.