I'm very new to C and am working on a card game. I've got my cards and shuffle down, but I want each card to have a name.

I made my cards inside an array of structs:

struct card
{
int value;
int suits;
int points;
char name[20];
};

int main()
{
struct card deck[52];
..
..
..
..

Now I'm just trying to find a way to print out the name strings which I have defined later like this:

deck[0].name=="2 of Diamonds";
deck[1].name=="3 of Diamonds";
..
..

and now I want to print out the name of the card. This is what I have, it compiles, but doesn't print. Am I defining the string wrong? Defining the names wrong? Using the printf wrong?

printf("%s", deck[0].name);

Why won't they print right? Thanks in advance.
Greg

Recommended Answers

All 10 Replies

Member Avatar for iamthwee

>deck[0].name=="2 of Diamonds";

1) using == is the wrong operator to assign variables
2) It wouldn't work with c-style strings (i think)

I would be safer to use string copy function instead, to copy it to the variable.

Member Avatar for dmachop

using == operator is for comparisons especially for datatypes other than strings. Therefore, there are specialized in-built functions to copy strings. In your case, what you have to do is:

1. Type #include<string.h>
which is the header required to perform the operation below.

2. Then instead of
deck[0].name=="2 of Diamonds";
type
strcpy(deck[0].name,"2 of Diamonds");
which copies the content to deck[0].name.

Now, I think the problem is over.

Any doubts???
SNIP

Beautiful. Thank you so much!

Member Avatar for dmachop

Mark it as solved if it's solved...................

hey use = instead of ==
as = to assign
and == is to compare

Beautiful. Thank you so much!

Make a program to calculate square numbers. If, for example, write square 9, has the answer 81.please help me with this program

Make a program to calculate square numbers. If, for example, write square 9, has the answer 81.please help me with this program

Panhandlers are not allowed in this forum

I recommend that you
will use strcpy() to assign/copy strings in a variable.

Rather than

deck[0].name=="2 of Diamonds";

use

strcpy(deck[0].name, "2 of Diamonds")

Always use strcpy() to assign a string to a
variable. strcpy is found in string.h

Why are you guys still trying to help someone who posted

Beautiful. Thank you so much!

He got his answer!

Member Avatar for dmachop

Why are you guys still trying to help someone who posted

He got his answer!

You're the moderator.........Don't you have authority to close the thread???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.