two dimensional array data input

Hi all, I have a program I am writing which requires input of data into a two dimensional array. Specifically, I have to display a chess board, and then based on user input allow for a move of a piece into an empty space, and then display the board again. I keep trying different things but I think I am not understanding the concept. What am I doing wrong here? How do I fix this?

Here is what I have right now, it is very rough code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  

#include <iostream>
#include <iomanip>
#include <cctype>

//function definitions
void cmove(char (&)[8][8], int, int, char);
void displayboard(char (&) [8][8]);

int main()
{
    char pieces[8][8] = {{'r', 'n', 'b', 'k','q', 'b', 'n','r'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'r', 'n', 'b', 'k','q', 'b', 'n','r'}} ;

   
      displayboard(pieces);

      cmove(pieces, 8, 8);

      std::cout << "Your move\n" << std::flush;

      displayboard(pieces);


    return(0);
}

void cmove (char (&eightby)[8][8], int a, int b)
{

    for (int a=0; a<8; a++)
    {
        for (int b=0; b<8; b++)
    {
        std::cout << "Enter rows and cols: " << std::flush;
        std::cin >> eightby;
    }
    }

    std::cout << "\nchanged\n" << std::endl;

}



void displayboard(char (&byeight)[8][8])
{
    for (int i=0; i<8; i++)
    {
        for (int j=0; j<8; j++)
    {
        std::cout << std::setw(2) << byeight[i][j] << std::flush;
    }
    std::cout << std::endl;
    }
}
some updated code, still rough:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include <iostream>
#include <iomanip>
#include <cctype>

//function definitions
void cmove(char (&)[8][8], int, int, char);
void displayboard(char (&) [8][8]);
void showboard(char (&) [8][8]);

int main()
{
    char pieces[8][8] = {{'r', 'n', 'b', 'k','q', 'b', 'n','r'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'r', 'n', 'b', 'k','q', 'b', 'n','r'}} ;

   char choice = ' ';
do
{

 std::cout << "Enter a choice: " << std::flush;
 std::cin >> choice;
 choice = toupper(choice);

 while (choice != 'n' && choice != 'b' && choice != 'r' && choice != 'k' && choice != 'q' && choice != 'p')
{
   std::cout << "Please enter a valid move: " << std::flush;
   std::cin >> choice;
   choice = toupper(choice);
}

 if (choice == 'n')
    {

      displayboard(pieces);
      cmove(pieces, 8, 8, 'n');
      displayboard(pieces);
}
  else if (choice == 'b')
      {
      displayboard(pieces);
      cmove(pieces, 8, 8, 'b');
      displayboard(pieces);
      }

 else if (choice == 'r' )

{
      displayboard(pieces);
      cmove(pieces, 8, 8, 'r');
      displayboard(pieces);
      }

      else if (choice =='k')
        {
      displayboard(pieces);
      cmove(pieces, 8, 8, 'k');
      displayboard(pieces);
      }

      else if (choice == 'q')
        {
      displayboard(pieces);
      cmove(pieces, 8, 8, 'q');
      displayboard(pieces);
      }
      else if (choice == 'p')
        {
      displayboard(pieces);
      cmove(pieces, 8, 8, 'p');
      displayboard(pieces);
      }
}(while choice != 's');

    return(0);
}

void cmove (char (&eightby)[8][8], int a, int b, char c)
{

   for (a= 0; a<1; a++)
   {
       for (b=0; b<1; b++)
       {std::cout << "enter row: " << std::flush;
        std::cin >> a;
        std::cout << "\nenter column: " << std::flush;
        std::cin >> b;
        a-=1;
b-=2;
       }
       eightby[a][b]=c;
   }



    std::cout << "\nchanged\n" << std::endl;

}



void displayboard(char (&byeight)[8][8])
{
    for (int i=0; i<8; i++)
    {
        for (int j=0; j<8; j++)
    {
        std::cout << std::setw(2) << byeight[i][j] << std::flush;
    }
    std::cout << std::endl;
    }
}
Line 77 should look like this }while (choice != 's');

When you run the program it says enter a choice: You don't tell the user what they are choosing, so how can they make a choice?

This line makes choice uppercase choice = toupper(choice);
therefore this line will never execute if (choice == 'n')

You ask the user to enter a row and column but the rows and columns are not labled, so how does the user know what a valid entry is?

You don't need parentheses around the &byeight void displayboard(char (&byeight)[8][8])
Last edited on
Topic archived. No new replies allowed.