hi there
I'm struggling on this problem hope you can help me

here is the question:

Define an enumerated type named Suits that lists the four suits of a card deck plus a constant
meaning \error" or \no suit". Use UPPER CASE for constants in your enumeration.

- Use typedef to define a struct that represents a playing Card. Your type should have three
members, a suit, a rank (represented by a character), and a short int point value.

- Include these global CONSTANT declarations, which define the legal inputs and point values:
const char* suits = "-SHDC";
const char* ranks = "-A23456789TJQK";
const int* points = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

Be sure that you do not use global variables in your code.

3 Functions to implement.

- void printCard( stream )
function with a stream parameter that will print a card's data
members (suit, rank, and pint value) to the given output stream. Print this in a readable
format such as \Q Hearts: 10"

- Card readCard( stream ).
Read a non-whitespace character representing the rank of a card
from the given input stream. Validate it: strchr( input, ranks ) will be between 1 and
13 if the input character is a legal rank. Do the same thing for the suit. If the rank is valid,
use the points array to nd the point value of the card and store it in the card structure.

- int main( void ).
Do the following things in your main program:
Declare several Card variables and call readCard to initialize them.
Print each Card variable after initialization, using your printCard function.
Test both legal and illegal input data and various combinations of suits and ranks.

here is what I've done so far:

#include <stdio.h>
typedef char* string;
typedef FILE *stream;
typedef enum{SPADES,HEARTS,DIAMONDS,CLUBS,ERROR}Suits;
typedef struct{
const char* suit;
const char* rank;
const int* points;
}Card;
void printCard(stream,Card);
Card readCard(stream,Card);

main(){

Card ca;

ca.suit= "-SHDC";

ca.rank = "-A23456789TJQK";
ca.points ="0,1,2,3,4,5,6,7,8,9,10,10,10,10";


stream myfile=fopen("cards.txt","w");

readCard(myfile,ca);
printCard(myfile,ca);
}

void printCard(stream myfile,Card ca){
    int j,k;
    Suits s;
    string label[5]={"SPADES","HEARTS","DIAMONDS","CLUBS","ERROR"};
    for(s=SPADES;s<ERROR;s++){
        for(j=0;j<13;j++){
            for(k=0;k<14;k++){

       fprintf(myfile,"\n %c %s: %i",ca.rank[j],label[s],ca.points[k]);
            }
              }
                }
}
Card readCard(stream myfile,Card ca){

    int i,j;
    Suits k;
    string input_r,input_s;
    printf("\n Enter a rank:");
    scanf("%30[^\n]",input_r);
    printf("\n Enter a suit:");
        scanf("%30[^\n]",input_s);
    for(i=0;i<13;i++){
    if(strchr(input_r,ca.rank[i])!=NULL){
        for(k=SPADES;k<ERROR;k++){
            if(strchr(input_s,ca.suit[k])!=NULL){
            if(ca.rank[i]=='T' ||ca.rank[i]=='J'||ca.rank[i]=='Q'||ca.rank[i]=='K'){
                ca.points=10;}
              else if(ca.rank[i]=='A'){
                 ca.points=1;}

               else if(ca.rank[i]=='2'){
                 ca.points=2;}

               else if(ca.rank[i]=='3'){
                 ca.points=3;}

               else if(ca.rank[i]=='5'){
                 ca.points=5;}

              else if(ca.rank[i]=='6'){
                 ca.points=6;}

             else if(ca.rank[i]=='7'){
                 ca.points=7;}

                 else if(ca.rank[i]=='8'){
                 ca.points=8;}

                 else if(ca.rank[i]=='9'){
                 ca.points=9;}
        }
         }


          }
       }
       return ca;
    }

can you please help me correcting the program
i didn't understand the question completely
i tried my best but no luck..

Take each function, make a note of:

1) What the function does correctly

and

2) What the function does not do correctly

That's the first step to debugging your program, and that's what anyone who wants to help might need to do, right off.

This will help save them a lot of time, and give yourself a way to start trouble shooting this code, as well.

Saying "the program is incorrect" is really not that helpful - we would assume that was the case, or you wouldn't be here.

It's the specifics faults that are needed, for each function or block of code. When you get one function working right, put a big phat OK above the first line of that function.

Nobody knows your code, and your teacher, better than you do. You can do this. :)

thanks man
the problem is, i didn't fully understand the requirements
can you at least help me understand the question properly?
what's an example of an output to this program?

Take each function, make a note of:

1) What the function does correctly

and

2) What the function does not do correctly

That's the first step to debugging your program, and that's what anyone who wants to help might need to do, right off.

This will help save them a lot of time, and give yourself a way to start trouble shooting this code, as well.

Saying "the program is incorrect" is really not that helpful - we would assume that was the case, or you wouldn't be here.

It's the specifics faults that are needed, for each function or block of code. When you get one function working right, put a big phat OK above the first line of that function.

Nobody knows your code, and your teacher, better than you do. You can do this. :)

Edited 6 Years Ago by deeer: n/a

I always love classroom assignments that involve playing cards!

First create your symmetrical enumeration to what ordering you prefer. Sequential #'s per suite. Or sequential interlaced suites.
So technically, mathematically your enumeration maps directly to a card number and suite. This makes it much easier for card shuffling and card handling. You're not moving classes around, only enumerations that refer to those cards. You shuffle the enumerations and the index of those enumerations are used as the lookup to your specific gaming values such a card bitmap, dual card value (remember Ace in some games count as 1 or 11, etc.)

When you manipulate the cards, it doesn't matter what the card is except when calculating a hand. Other then that its a generic object (especially if it has a matching bitmap.) For a multi-deck shoe, that same card index can be used to calculate the enumeration and thus the exact suit, and card number.

You won't need error enumerations as only legitimate cars exist, each assigned an enumeration. since only referred to as an enumeration invalid cards shouldn't exist!

Technically you don't need a master data structure array but if that's the classroom assignment....


Shuffling your cards is an easy algorithm as well and the same used in casino and redemption slot machines.

Edited 6 Years Ago by wildgoose: n/a

thanks man
the problem is, i didn't fully understand the requirements
can you at least help me understand the question properly?

Such as? We don't understand what you are having trouble understanding if you don't tell us. What do you understand, and what don't you understand?

I asked for a simple output of the program to understand it
but i think, just like me, none of you really understood the question

This article has been dead for over six months. Start a new discussion instead.