Declaring Char Array in a Structure

Ok, I can't declare a char array(not a string, an array of single chars)in a structure so what I did is this. But the problem is it's not working properly. It doesn't give me errors but when I take a letter out of it, it's bugging.

 
  strcpy(variable.letter, "{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '}"); 
Is your code some sort of these ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>

using namespace std;

struct x {
    char* name;
};

int main()
{
   x x1;
   
   strcpy( x1.name, "John");
   cout << x1.name << endl;
   
   return 0;
}


I'm not sure why your code didn't work, can you post it ?
nope, I've already figured it out, it's like this:

 
strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 


I was hoping for a char array with single chars not string but I guess this is working.
The problem is it's also reading the commas, brackets, and quotation marks.

My problem now is this function. It's not working the way it should
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

int player_tiles(scrabble variable){
	int j, rd;	
	printf("Number of tiles in the tile bag is: %d\n", variable.n);
	printf("Player %d's Tiles: {", variable.p);	
	for(j=1;j<=7; j++){	
		do{					
			rd=rand()%101;
		}while(variable.letter[rd]==0);
		if(variable.players[variable.p].tiles[j]==0){
			if(variable.n>0){
				variable.players[variable.p].tiles[j] = variable.letter[rd];
				variable.n = variable.n - 1;
			}else{
				variable.players[variable.p].tiles[j] = 0;
			}
			printf("%d",rd);
			variable.letter[rd] = 0;			
		}
		if(j!=7){
			printf("%c, ", variable.players[variable.p].tiles[j]);
		}else printf("%c", variable.players[variable.p].tiles[j]);
	}
	printf("}\n");
	return variable.n;	
}


It's not reading this condition properly
variable.players[variable.p].tiles[j]==0
It keeps undergoing this actions even if it's not equal 0 anymore
1
2
3
4
5
6
if(variable.n>0){
				variable.players[variable.p].tiles[j] = variable.letter[rd];
				variable.n = variable.n - 1;
			}else{
				variable.players[variable.p].tiles[j] = 0;
			}
Last edited on
Ok, I'm pretty sure this is the part that is not working properly. I need it to be array of chars not a string. Can somebody help me on how to declare this.
 
strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 
that should work fine.

1
2
char array[5] = "Hello";
char array[6] = "Hello";


Though technically it should be a const char* ( const array )

*sorry it's supposed to be 6 not 5 the last character is a null terminator '\0'
Last edited on
We weren't taught how to use that and the char array is inside a struct so I'm using strcpy. I don't think I can use const.
You can if you initalize

1
2
3
4
5
6
7
8
struct Scrabble
{
    Scrabble( const char *array );
    private:
        const char *array;
};

Scrabble::Scrabble( const char *array ) : array( array ){}


I said we weren't taught about that so I can't use that. I need to use either strcpy or by initializing it like
 
     variable.letter[101] = {'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '};


Though this one doesn't work.
I need it to be array of chars not a string.

Could you explain why? Is this because of some different functionality or for external reasons, such as to keep your instructor happy?
This is a scrabble game, the array of chars are the tiles/letters in the tile bag.
But that doesn't answer the question.
I've got this figured out now. I realized I wasn't calling the function correctly.
What chervil was saying is that a std::string is the same as a char array you can call it by each character in the string

ex
1
2
std::string str = "Hello";
std::cout << str[0] << std::endl; //'H' 
Topic archived. No new replies allowed.