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..