C style strings in structs

I am currently learning structs and am having issues in reading from a text file into a array of structs. I am trying to create a deck of cards. The text file is in the format, " heart 12 diamond 10 spade 1 club 5 etc...". For some reason I am getting an error about a char to char incompatibility. I have to read the suits from the text file as a c style string. Any reasons why its not compiling?

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

//Card struct
struct cards{
char suit[30];
int rank;
char color;
};


//Display Main Menu
void mainMenu();

//Prompt for user selection
char menuSelection();

void createCards(cards[52]);

void displayCards(cards[7][7], cards[23]);


int main()
{

char selection;
bool holdScreen = true;

cards deck[52];
cards tableau[7][7];
cards wastePile[23];

int i = 0;
int j = 0;
int card = 0;

createCards(deck);
while(holdScreen == true)
	{

	mainMenu();
	cout << endl;
	selection = menuSelection();
	cout << endl;

		switch( selection )
			{
				case 'n':
				case 'N':
				break;

				case 'p':
				case 'P':
				break;

				case 'q':
				case 'Q':
				holdScreen = false;
				break;

			}
	}


return 0;
}


void mainMenu()
{
	// Menu Selections
	char first[] = "Main Menu";
	char second[] = "---------";
	char third[] = "1. <N>ew Game";
	char fourth[] = "2. <P>revious Game";
	char fifth[] = "3. <Q>uit Game";
	char sixth[] = "4. ENTER SELECTION";

	//Print Selections
	cout << first << endl;
	cout << second << endl;
	cout << third << endl;
	cout << fourth << endl;
	cout << fifth << endl;
	cout << sixth << endl;
}

char menuSelection()
{

	char selection;
	cin >> selection;

	return selection;
}

void createCards(cards deck[52]){

	char temp[30];
	int tempRank;
	ifstream fin;
	int i = 0;

	fin.clear();

	fin.open("cards.txt");

	if(!fin.good()){

		cout << "Error opening file" << endl;
	}

	while(fin.good()){

			fin >> temp >> tempRank;

			while( temp[i] != '\0' ){

				deck[i].suit = temp[i];
				i++;
			}

			deck[i].rank = tempRank;
                        i++;


	}

	fin.close();


}
Last edited on
For future posts:

1) please don't paraphrase the error. Especially if you don't understand it. It's better to post the full error.

2) Please tell us what line the error occurs on. It is given to you in the error message. And in most IDE's you can just double-click on the error message and it will jump you to the line.


That said... the error is here:

 
deck[i].suit = temp[i];


'temp[i]' is a single character (char). 'suit' is an array of 30 characters (char[30]). You cannot assign a single character to an entire array of characters. That is nonsense.

You probably meant to do this:

 
deck[i].suit[i] = temp[i];


But even that is wrong because you're using the same index (i) for both 'deck' and 'suit'.


Let me ask you this.... why do you need tempRank and temp? Why can't you just read into your deck directly? It would certainly simplify things.
Last edited on
Thanks will be more precise in future postings.

Yeah I was the under the impression it would have been better to make temp variables then load them into the arrays, but you're question made me realize it's an unnecessary step.

Thanks
Actually, could you clarify on reading them into the deck directly? I am trying to implement the suggestion but a little confused.
You are reading from the file into temp
then trying to copy temp into deck[i].suit

Why not just get rid of temp and read from the file into deck[i].suit.

1
2
3
fin >> temp >> tempRank;  // <- why this?

fin >> deck[i].suit >> deck[i].rank;  // <- when you could just do this? 
Oh man, for some reason I was too focused that I had to increment the array.... but this work a lot better. Thanks!!!
Topic archived. No new replies allowed.