c++ Multidimensional array wont initialize - c++

I have been trying to get this to work correctly; however it seems I can not figure this out. I am trying to get a game board to initialize correctly but it keeps saying that <error reading characters of string>.
using namespace std;
int main()
{
board show;
show.init();
show.printing();
}
class board {
public:
void init(){
string Board[8][9] = {
{ "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
};
}
void printing(){
string character = "*";
int position[2] = { 2, 2 };
// Draw the grid once
for (int i = 0; i < 8; i++){
for (int j = 0; j < 9; j++){
if (i == position[0] && j == position[1])
cout << character;
else
cout << Board[8][9];
cout << " ";
}
cout << endl;
}
}
private:
string Board[8][9];
};

it's so easy: you have the half solution:
just use for loop to copy values
void A::init()
{
string Board[8][9] = {
{ "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 ", " 0 " },
{ "C56", "C15", "C21", "C62", "C11", "C62", "C21", "C15", "C56" },
};
for(int i(0); i < 8; i++)
{
for(int j(0); j < 9; j++)
this->Board[i][j] = Board[i][j];
}
}

In the init method you created a new, local variable. You are not referring to your instance variable, therefore in printing method the variable is not initialized.
Instead of
string Board[8][9] = // ... initialization code
you should write
this->Board = // ... initialization code
or just
Board = // ... initialization code
You can read more about variable types here.

Related

Dont know how to fix: expected primary-expression before '.' token

I'm currently just starting off coding in c++, the current project I'm working on is making an in-console chess engine type thing. The function below is supposed to check if a particular square has a piece on it, and if so, if the piece is white. Board.grid is a 2D array of strings where " " would be an empty square, "wR1" one of the white rooks, "bQ1" the black queen, etcetera. I got the error expected primary-expression before '.' token on both if statements inside the functions and think it's got to do with the .at() function called on the 'square string', but have no clue why this would give an error or what could be done to resolve it.
I would appreciate it greatly if anyone could help me with this. Thanks in advance!
#include <iostream>
using namespace std;
struct Board {
string Grid[8][8] =
{
{"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
{"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
{"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
};
};
class Piece
{
public:
bool getSquare (int square[2])
{
bool isOccupied = false;
bool isWhite = false;
if (Board.grid[square[1]][square[0]].at(0) != ' '){isOccupied = true;};
if (Board.grid[square[1]][square[0]].at(0) == 'w'){isWhite = true;};
bool arr[2] = {isOccupied, isWhite};
return arr;
};
};
The symbol Board is a type not an instance. In C++ you can't use the . operator to access type members, only members of objects.
The simple solution is to create a Board objects:
struct /* Anonymous structure */ {
string Grid[8][8] =
{
{"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
{"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
{"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
};
} Board; // Define the Board structure object instance
Or possibly define Board as a namespace, and use the scope operator:
namespace Board {
string Grid[8][8] =
{
{"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
{"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " "},
{"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
{"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
};
}
// ...
Board::grid[square[1]][square[0]].at(0)
// ^^
// Note use of scope operator :: here

Setting the value of similarly named variables C++

So, I have some variables declared as follows:
int rect1Color;
int rect2Color;
int rect3Color;
...
int rect63Color;
int rect64Color;
I need to change each of these variables based on a loop that looks like this:
for (int i = 0; i < sizeof(playPos) / sizeof(char*); ++i) {
const char* TEMP = playPos[i];
if (TEMP != " x" && TEMP != " o" && TEMP != "xx" && TEMP != "oo") {
if (TEMP == " p") {
rect[i+1]Color = 1;
}
else {
rect[i+1]Color = 2;
}
}
else if (TEMP == " o" || TEMP == "oo") {
rect[i+1]Color = 3;
}
else if (TEMP == " x" || TEMP == "xx") {
rect[i+1]Color = 4;
}
}
That draws from this data set:
const char *playPos[64] {
" ", " o", " ", " o", " ", " o", " ", " o",
" o", " ", " o", " ", " o", " ", " o", " ",
" ", " o", " ", " o", " ", " o", " ", " o",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
" x", " ", " x", " ", " x", " ", " x", " ",
" ", " x", " ", " x", " ", " x", " ", " x",
" x", " ", " x", " ", " x", " ", " x", " "
};
The data set and logic all work, I just can't find a simple way to set the values of the variables.
The solution to my problem was turning my long list of ints into one vector/array.
So instead of:
int rect1Color;
int rect2Color;
int rect3Color;
...
int rect63Color;
int rect64Color;
I now have:
int rectColor1[64];
User "cigien":
Just use a vector<int> rectColors;. Then the index i corresponds to the ith colour.
User "user4581301":
Side note: if you have a fixed number of variables known at compile time, consider using std::array as well. Because the size is fixed there is less overhead than what's needed by the dynamically sized std::vector

Printing Ascii Art letters in console

i have some problems with making this program. What i need to do is for a project in my class. what i need to do is similar to this: http://patorjk.com/software/taag/
I can print one letter a time but the problem is i cannot print somethong like "ABC" it prints them all in the same place. So if any of you can help me and show me the way i should do the printing would be so nice.
#include <iostream>
#include <string.h>
#include<windows.h>
#include <stdio.h>
using namespace std;
char A[4][12]={
" // ",
" // // ",
" /////// ",
"// //"};
char B[5][12]={
" ///// ",
" // // ",
" ///// ",
" // // ",
" ///// "};
char C[5][12]={
" ///// ",
" // ",
" // ",
" // ",
" ///// "};
char D[5][12]={
" ///// ",
" // // ",
" // // ",
" // // ",
" ///// "};
char E[5][12]={
" ///// ",
" // ",
" //// ",
" // ",
" ///// "};
char F[5][12]={
" ///// ",
" // ",
" //// ",
" // ",
" // "};
char G[5][12]={
" ///// ",
" // ",
"// /// ",
" // // ",
" ///// "};
char H[5][12]={
" // // ",
" // // ",
" /////// ",
" // // ",
" // // "};
char I[5][12]={
" ** ",
" // ",
" // ",
" // ",
" // "};
char J[5][12]={
" // ",
" // ",
" // ",
" // // ",
" //// "};
char K[5][12]={
" // // ",
" // // ",
" /// ",
" // // ",
" // // "};
int main()
{
int x, k = 0;
char a[999];
cin.get(a, 999);
x = strlen(a);
while (k < 6)
{
for (int i = 0; i <= x; i++)
{
if (a[i] == 'A')
cout << A[k] << endl;
if (a[i] == 'B')
cout << B[k] << endl;
if (a[i] == 'C')
cout << C[k] << endl;
if (a[i] == 'D')
cout << D[k] << endl;
if (a[i] == 'E')
cout << E[k] << endl;
if (a[i] == 'F')
cout << F[k] << endl;
if (a[i] == 'G')
cout << G[k] << endl;
if (a[i] == 'H')
cout << H[k] << endl;
if (a[i] == 'I')
cout << I[k] << endl;
if (a[i] == 'J')
cout << J[k] << endl;
if (a[i] == 'K')
cout << K[k] << endl;
}
k = k + 1;
}
return 0;
}
I've written a very similar program to print integers in large size by printing hashes.
Here is the program:
/**
Author:- Mayukh Datta
**/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define H 7
#define W 8 //one extra room in the char array is required for storing '\0'
void main()
{
char num[11]; //here too one extra room is needed for the '\0'
char c; //for option
int i, j, k;
//declaring char 2D arrays and initializing with hash-printed digits
char zero[H][W]={" ##### ", //H=0
" # # ", //H=1
" # # ", //H=2
" # # ", //H=3
" # # ", //H=4
" # # ", //H=5
" ##### "},//H=6
one[H][W]={" # ",
" ## ",
" # ",
" # ",
" # ",
" # ",
" ##### "},
two[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
three[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
four[H][W]={" # ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "},
five[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
six[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # # ",
" # # ",
" ##### "},
seven[H][W]={" ##### ",
" # ",
" # ",
" #### ",
" # ",
" # ",
" # "},
eight[H][W]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # # ",
" # # ",
" ##### "},
nine[H][W]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
do
{
printf("Enter a number upto 10 digits:- ");
fflush(stdin);
gets(num);
if(strlen(num)>10)
printf("\nYou must enter a number upto 10 digits.\nTry again!\n");
else
{
printf("\n");
k=1;
j=0; //controls H of each digit
while(k<=7) //controls height
{
for(i=0;i<strlen(num);i++) //reads each digit
{
if(num[i]=='0')
printf("%s", zero[j]);
else if(num[i]=='1')
printf("%s", one[j]);
else if(num[i]=='2')
printf("%s", two[j]);
else if(num[i]=='3')
printf("%s", three[j]);
else if(num[i]=='4')
printf("%s", four[j]);
else if(num[i]=='5')
printf("%s", five[j]);
else if(num[i]=='6')
printf("%s", six[j]);
else if(num[i]=='7')
printf("%s", seven[j]);
else if(num[i]=='8')
printf("%s", eight[j]);
else if(num[i]=='9')
printf("%s", nine[j]);
}
printf("\n");
k++;
j++;
}
}
printf("\nEnter Y to continue:- ");
fflush(stdin);
scanf("%c", &c);
}while(c=='Y'||c=='y');
}
Understand how the loop works in my program.
Link: http://www.thecoducer.com/2017/08/printing-the-number-in-large-size.html

Multidimensional arrays replacing values

When I print my field it works but when I change a value the array seems to get reset. I think I'm declaring my string veld[10][11] on the wrong place but I'm not shure.
Also got veld as an attribute of my class speelveld.h
thanks
#include "speelveld.h"
#include "Schip.h"
void spelbord::printVeld(){
//spelbord::zetBoot();
string veld[10][11]= {
{ "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
};
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
for (int i = 0; i < 10; i++){
cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
for (int j = 0; j < 11; j++){
cout << veld[i][j] << "|" << flush;
}
cout << endl;
}
cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
}
void spelbord::zetBoot(){
string veld[10][11];
cout << "Wat is de eerste coordinaat van je vliegdekschip? (bv: a1) " << flush;
cin >> vliegdekschip1;
y = vliegdekschip1[0];
x = vliegdekschip1[1];
cout << y << endl;
cout << x << endl;
spelbord::checkpos();
}
void spelbord::checkpos(){
if (y == 97){
if (x == 49){
veld[0][1] = "O";
spelbord::printVeld();
}
{
{
For the rest of my answer, I assume that your class spelbord has an attribute veld of type string.
The problem
The problem is that you use a local variable in your spelbord::printVeld function:
void spelbord::printVeld()
{
/* Initialize a 'new' local variable each time you pass here. */
string veld[10][11] = {
/* Initialization value. */
}
/* Print header. */
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 11; ++j)
/* Refers to the variable just initialized. */
cout << veld[i][j] << "|" << flush;
cout << endl;
}
/* Print footer. */
}
void spelbord::checkpos()
{
if (y == 97)
if (x == 49)
{
/* Refers to the attribute 'veld' of the 'spelbord' object */
veld[0][1] = "O";
spelbord::printVeld();
}
}
To sum-up You are always displaying a variable newly initialized. Not the one you are modifying in spelbord::checkpos.
A possible solution
/* Constructor. Used to initialize members. */
spelbord::spelbord()
{
/* I don't use the constructor's prefered way of initialization because your
initialization value is huge. */
veld = { /* Initialization. */ };
}
/* No local variable this time. */
void spelbord::printVeld()
{
/* Print header. */
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 11; ++j)
/* Refers to the member of the object. */
cout << veld[i][j] << "|" << flush;
cout << endl;
}
/* Print footer. */
}
void spelbord::checkpos()
{
/* Same as before. */
}
This time the member is initialized only once, when the object is built, and you modify and display this member.

GLM multiplication order

I am a little amazed. I have been debugging my code for hours now, and GLM seems to be giving up on me. I am struggling with the following 2 instances:
....
cout << "multiplying A:" << endl;
displayMatrix(node->wMatrix);
cout << "and B:" << endl;
displayMatrix((node->children)[i]->wMatrix);
//switch order!
mat4 temp = (node->children)[i]->wMatrix * node->wMatrix;
cout << "Get result as:" << endl;
displayMatrix(temp);
...
The displayMatrix method is as follows:
void displayMatrix(mat4 &m)
{
cout << m[0][0] << " " << m[0][1] << " " << m[0][2] << " " << m[0][3] << endl;
cout << m[1][0] << " " << m[1][1] << " " << m[1][2] << " " << m[1][3] << endl;
cout << m[2][0] << " " << m[2][1] << " " << m[2][2] << " " << m[2][3] << endl;
cout << m[3][0] << " " << m[3][1] << " " << m[3][2] << " " << m[3][3] << endl;
}
Here is the output I get:
multiplying A:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
and B:
0.540302 -0.841471 0 0
0.841471 0.540302 0 -0.5
0 0 1 0
0 0 0 1
Get result as:
0.540302 -0.841471 0 0
0.841471 0.540302 0 0
0 0 1 0
0 0 0 1
NOTICE that in the code above, the matrix multiplication order is the reverse of what you would write on paper. In other words, the code says B * A. I was very thrown off by this.
The second instance:
cout << "temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "binding matrix inverse: " << endl;
displayMatrix(bindingInvs.at(jIndex));
temp = bindingInvs.at(jIndex) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "joint world matrix: " << endl;
displayMatrix(joints.at(jIndex)->wMatrix);
temp = (joints.at(jIndex)->wMatrix) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "weight: " << jWeight << endl;
temp = jWeight * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
The output that I get now is:
temp:
0.087 0 -0.05 1
binding matrix inverse:
1 -0 0 -0
-0 1 -0 0
0 -0 1 -0
-0 0 -0 1
now temp:
0.087 0 -0.05 1
joint world matrix:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
now temp:
0.087 0 -0.05 1
weight: 1
now temp:
0.087 0 -0.05 1
Temp is never getting changed for some reason. I don't know what to do, or why this is happening. My programs compiles and runs (I am pasting from output above). Of course, this is not the entire program. This is only the steps for debugging. But I feel confident that this much should be enough to tell what's going on.
Your displayMatrix function is confusing you, since you print the matrices transposed to what you would expect on paper. GLM uses column major ordering, so the addressing is m[col][row].
Now with that in mind, the operation A*B is actually what you should expect.
For the temp vector, the same problem arises: the first matrix you multiply it by is identity, so it is unchanged. The second matrix is identity, except the last row is 0 0.5 0 1, so x, y and z will be unchanged and the new w' will be 0.5 * y + w. Since y is 0 to begin with, nothing is changed here,too.