Related
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
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
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
I have used stackoverflow for a long time and never had to post a question before, but this one has me EXTREMELY frustrated.
I have this piece of code
array<array<string, 10>, 10> cShots =
{
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
};
outside of any methods at a global level. What I'm trying to do is create a 2d array 10 by 10 full of blank space strings. What happens instead is that I get an error on the first bracket of the third line saying "too many initializer values". Every solution I have found to this error says that what I am doing is correct. I have tried doing it multiple other ways.
string** cShots =
{
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
};
string cShots[10][10] =
{
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
};
but everything I try has the same result.
PLEASE HELP!
EDIT: well...I figured out that I just needed more brackets...and the error went away...but now when I run my code visual studio says that it was a successful build, but then freezes forever...
Things are a little easier to understand when you see that there is a special case for the syntax here. std::array is an aggregate, it's a struct that contains a single data member which is the underlying raw array. To initialize one of these you need to uniform-initialize the array (first braces) then initialize the underlying raw array member stored in the std::array (second braces);
So when you have something such as:
array<string, 3> test = {{"a", "b", "c"}};
The outermost braces initialize the array itself then the inner part initializes the underlying array. In this case it's clear that this is the way it is working and a special case was made (See the standard: section 8.5 for more) that allows the compiler to accept the other syntax of:
array<string, 3> test = {"a", "b", "c"} ;
However in the case of the question it's not of the special-case-form specified in the standard so you need the extra braces (this too caused me confusion when I first encountered it). There is a defect report about this actually http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3367.html#1270
In more recent c++ standards (see Can we omit the double-braces for std::array in C++14?) this requirement is relaxed.
Because the code in the question does not benefit for the special case of the syntax we need both braces. In this case your initializer is however not of length one:
array<array<string, 10>, 10> cShots =
{
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }, //extra initializer elements but only one underlying raw array
//snip
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
};
To the compiler it looks like you are trying to initialize multiple members of the std::array class but the problem is that there is only one underlying storage array. Hence the compiler error message you get here. To get this to work you need to be more explicit and add the extra pair of braces to get an initializer of length one passed to the outer std::array. You then initialize the underlying raw array which contains the other arrays:
array<array<string, 10>, 10> cShots =
{{
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
//snip
{ " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
}};
For a proof of concept see this: http://coliru.stacked-crooked.com/a/4c57a7193629931f
(Note the nested arrays here do benefit from the syntatical special case)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I've created a general tree based on a Node class that has 2 pointers: next points to the node's first son , bros points to the next brother of the node.
Each node has a capacity (int) and each leaf is considered one unit of demand , the goal is to say weather or not the tree supports the demand (meaning each branch of the tree has enough capacity to supply all of the demand).
The tree building part works quite nicely but it seems there's a bug i'm missing in my supplier and recursion function. general explanation:
isSupplier(node) - checks that the node has enough capacity to support all leafs under its branch.
canDemandBeAnswered(node) - recursive function that is supposed to call isSupplier for all nodes starting with the leafs going upward.
problem is that after the recursion encounters the first leaf it gets stuck on an unknown node (at height 1 with zero sons which is impossible because if the node is a leaf the recursion isn't called!)
Hopefully someone can find something i missed , thank you!
// This method checks if this node can supply all of its leafs.
bool isSupplier()
{
if ( this->isLeaf() ) { return 1;}
else
{
this->num_of_leafs = this->Count_Leafs();
Node* iter = this->next;
while ( (iter != NULL) )
{
if ( iter->isLeaf() == 0 )
{ this->num_of_leafs += iter->num_of_leafs; }
iter = iter->bros;
}
}
if (this->capacity < this->num_of_leafs) { return 0; }
else { return 1; }
}
bool canDemandBeAnswered(Node* root)
{
cout << "Height: " << root->getHeight() << " , sons: " << root->Count_Sons() << " ,bros: " << root->getNumBros() << " ,leafs: " << root->getNumLeafs() << " \n";
if ( root->isLeaf() ) { return 1; }
Node* iter = root->next;
while ( iter != NULL )
{
canDemandBeAnswered(iter);
iter->getNextBro();
}
return root->isSupplier();
};
The tree creation and recursive call:
Node* s8 = new Node(8);
Node* s5 = new Node(5);
Node* s6 = new Node(6);
for(int i=0; i < 2 ; i++){
s6->addChild(new Node());
}
Node* s7 = new Node(7);
Node* s2 = new Node(2);
for(int i=0; i < 3 ; i++){
s2->addChild(new Node());
}
Node* s3 = new Node(3);
Node* s2_2 = new Node(2);
s2_2->addChild(new Node());
Node* s4 = new Node(4);
for(int i=0; i < 5 ; i++){
s4->addChild(new Node());
}
Node* s1 = new Node(1);
for(int i=0; i < 2 ; i++){
s1->addChild(new Node());
}
Node* s2_3 = new Node(2);
for(int i=0; i < 4 ; i++){
s2_3->addChild(new Node());
}
Node* s2_4 = new Node(2);
for(int i=0; i < 3 ; i++){
s2_4->addChild(new Node());
}
s8->addChild(s5);
s8->addChild(s6);
s5->addChild(s7);
s5->addChild(s2);
s6->addChild(s3);
s6->addChild(s2_2);
s7->addChild(s4);
s7->addChild(s1);
s3->addChild(s2_3);
s3->addChild(s2_4);
cout << "s8 height: " << s8->getHeight() << " , sons: " << s8->Count_Sons() << " , bros: " << s8->getNumBros() << " , num of leaf: " << s8->getNumLeafs() << " \n";
cout << "s5 height: " << s5->getHeight() << " , sons: " << s5->Count_Sons() << " , bros: " << s5->getNumBros() << " , num of leaf: " << s5->getNumLeafs() << " \n";
cout << "s6 height: " << s6->getHeight() << " , sons: " << s6->Count_Sons() << " , bros: " << s6->getNumBros() << " , num of leaf: " << s6->getNumLeafs() << " \n";
cout << "s7 height: " << s7->getHeight() << " , sons: " << s7->Count_Sons() << " , bros: " << s7->getNumBros() << " , num of leaf: " << s7->getNumLeafs() << " \n";
cout << "s2 height: " << s2->getHeight() << " , sons: " << s2->Count_Sons() << " , bros: " << s2->getNumBros() << " , num of leaf: " << s2->getNumLeafs() << " \n";
cout << "s3 height: " << s3->getHeight() << " , sons: " << s3->Count_Sons() << " , bros: " << s3->getNumBros() << " , num of leaf: " << s3->getNumLeafs() << " \n";
cout << "s2_2 height: " << s2_2->getHeight() << " , sons: " << s2_2->Count_Sons() << " , bros: " << s2_2->getNumBros() << " , num of leaf: " << s2_2->getNumLeafs() << " \n";
cout << "s4 height: " << s4->getHeight() << " , sons: " << s4->Count_Sons() << " , bros: " << s4->getNumBros() << " , num of leaf: " << s4->getNumLeafs() << " \n";
cout << "s1 height: " << s1->getHeight() << " , sons: " << s1->Count_Sons() << " , bros: " << s1->getNumBros() << " , num of leaf: " << s1->getNumLeafs() << " \n";
cout << "s2_3 height: " << s2_3->getHeight() << " , sons: " << s2_3->Count_Sons() << " , bros: " << s2_3->getNumBros() << " , num of leaf: " << s2_3->getNumLeafs() << " \n";
cout << "s2_4 height: " << s2_4->getHeight() << " , sons: " << s2_4->Count_Sons() << " , bros: " << s2_4->getNumBros() << " , num of leaf: " << s2_4->getNumLeafs() << " \n";
bool ans = hw4->canDemandBeAnswered(s8);
and the big finale , my debug output:
Your loop
while (iter != NULL)
{
canDemandBeAnswered(iter);
iter->getNextBro();
}
doesn't do anything since you never modify iter.
I suspect you meant to say
iter = iter->getNextBro();
You're also ignoring the return value of the recursive call, which is probably not what you intended, but it's not entirely clear what it's supposed to do.