I wanted the program to display each piece with its assigned color preceding it (eg a white rook would be 'WR').

#include <iostream>

using namespace std;

struct attrib{
char x;
int y;
char cont[5];
char clr;
};

attrib brd[8][8];

void prntbrd(void)
    {
        for ( int y =0; y<8; y++) //accesses all columns in the array
            {
            for ( int x=0; x<8; x++) //accesses all rows in the array
                {
                    cout<<"|" << brd[x][y].cont[5] << "|";
                };
                cout<<"\n";
            };
    };

void mkbrd(void)
    {
        for ( int y =0; y<8; y++) //accesses all columns in the array
            {
            for ( int x=0; x<8; x++) //accesses all rows in the array
                {
                brd[x][y].x= char (65+x); //assigns columns names A-H
                brd[x][y].y= y+1; //assigns rows numbers 1-8

                if ( y==1 || y==2 )//assigns top two rows to white
                    {
                    brd[x][y].clr='W';
                    }
                else if ( y==7 || y==8 )//assigns bottom two rows to black
                    {
                    brd[x][y].clr='B';
                    }
                else //random. assigns empty squares to " " for aesthetics
                    {
                    brd[x][y].cont[5]='  ';
                    }

                if  ( (x==0 || x==7) && y==0 )//assigns white rooks
                    {
                    brd[x][y].cont[5]='WR';
                    }
                else if ( (x==1 || x==6) && y==0 )//horses
                    {
                    brd[x][y].cont[5]='WH';
                    }
                else if ( (x==2 || x==5) && y==0 )//bishops
                    {
                    brd[x][y].cont[5]='WB';
                    }
                else if ( x==3 && y==0 )//king
                    {
                    brd[x][y].cont[5]='WK';
                    }
                else if ( ( x==4 ) && y==0 )//queen
                    {
                    brd[x][y].cont[5]='WQ';
                    }
                else if ( y==1 )
                    {
                    brd[x][y].cont[5]='WP';
                    }

                else if ( (x==0 || x==7) && y==7 )//assigns black rooks
                    {
                    brd[x][y].cont[5]='BR';
                    }
                else if ( (x==1 || x==6) && y==7 )//horses
                    {
                    brd[x][y].cont[5]='BH';
                    }
                else if ( (x==2 || x==5) && y==7 )//bishops
                    {
                    brd[x][y].cont[5]='BB';
                    }
                else if ( x==3 && y==7 )//king
                    {
                    brd[x][y].cont[5]='BK';
                    }
                else if ( x==4 && y==7 )//queen
                    {
                    brd[x][y].cont[5]='BQ';
                    }
                else if ( y==6 )//pawns
                    {
                    brd[x][y].cont[5]='BP';
                    }
                }
            }
    }

int main()
{
    mkbrd();
    prntbrd();
    return(0);
}

Recommended Answers

All 3 Replies

Hey there. The char cont[5]; member of your struct is an array of characters. When you assign to it in your code, like brd[x][y].cont[5]='BP'; , you need to be assigning only a single char to each spot in the array. To fix this, you could declare char cont[6] then just assign individually, like

brd[x][y].cont[5]='B'; 
brd[x][y].cont[6]='P';

.
Hope this helps!

How can I assign two letters at once?

using strings?

#include <string>

string WR = "WR", BB = "BB";

brd[x]...[6]=BB;
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.