How to do the following in C (ANSI)

  • Thread starter Thread starter brad sue
  • Start date Start date
AI Thread Summary
The discussion focuses on reading card data from a file in ANSI C, specifically how to count occurrences of the words "six" and "heart" while also handling lines that may contain extraneous data. Participants suggest using `fscanf` to read card values and suits, and employing `strcmp` for comparison to increment counters. There are challenges noted regarding skipping the first line of data and correctly storing card weights in an array. Suggestions include using a loop to read lines and conditionally storing weights based on card values. The conversation emphasizes the importance of proper file handling and string comparison techniques in C programming.
brad sue
Messages
270
Reaction score
0
Hi , I need suggestion on how to do the following in C (ANSI):

I have a set of 5 cards like:

king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts


I need read the 5 lines (strings) from a file, and count how many,"six" and "heart" I have in this set.

If I read the line with fgets, how can I discriminate the words"six" and "heart" from the whole line, for example?

I suspect pointers and the function strcmp but I don't know how to use them..
Please Can I have some indications?
Thank you for your help.
B
 
Technology news on Phys.org
How about this:

fscanf(file_pointer, "%s of %s", &value, &suit) ;

Then use strcmp on value and suit with "six" and "heart" respectively. If you get a match then increment the counter.
 
dduardo said:
How about this:

fscanf(file_pointer, "%s of %s", &value, &suit) ;

Then use strcmp on value and suit with "six" and "heart" respectively. If you get a match then increment the counter.

Thank you very much. I have an issue with the file reading
for example we have the same ste of cards but before it, there is a int like

12
king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts

If I use fgets and fputs to read and print the file, what can I do so that I star reading and the second line( skip the line with 12)

I called each line LINE and set a while loop

while( 1 ) {/*while begins*/

fgets(LINE, sizeof(LINE), fin);

if ( feof(fin) ) /* check for EOF right after fgets() */
break;
fputs(LINE, fout);}


I don't succeed to skip the first line I tried LINE+1 but it is an horizontoal displacement..
Can I have some suuggestions?
 
dduardo said:
How about this:

fscanf(file_pointer, "%s of %s", &value, &suit) ;

Then use strcmp on value and suit with "six" and "heart" respectively. If you get a match then increment the counter.

Hi ,
please I need help to extract some value and made them in an array.

I get the value for each card with my function convrt,
now I don't succeed to extract those values to create an array.
where in my code should I place my loop to create an array hand[].

Code:
while( 1 ) {/*while begins*/

fgets(line, sizeof(line), fin);

if ( feof(fin) ) /* check for EOF right after fgets() */
break;
fputs(line,fout);
rv=convrt(line);/* find the card value for each line*/
count++;

if(count%5==0){/* determine each one hand*/
/* below start the checking for sequence and other...

when I try to place some instruction, it gave me weird numbers( perhaps memory locations) or infinite loop.
for example

king of clubs
12 (value of card king)
six of clubs
5(value of card five)

four of diamonds
3 (value of card three)

six of hearts
5 (value of card five)

four of hearts
3 (value of card four)

the array should be 12 5 3 5 3.

B.
 
card.c
Code:
#include <stdio.h>

int main( void ) {

  FILE *fp ;
  char value[15], suit[15] ;

  fp = fopen("card.dat","r") ;
  if( !fp ) {
    printf("Error Opening File\n") ;
  } else {
    while( !feof(fp) ) {
      fscanf(fp, "%s of %s\n", value, suit) ;
      printf("%s is of suit %s\n", value, suit) ;
    }
  }

  fclose(fp) ;
  return 0 ;
}

card.dat
Code:
king of clubs
six of clubs
four of diamonds

Output
Code:
king is of suit clubs
six is of suit clubs
four is of suit diamonds

If you want to weigh each card then put a bunch of if statements in the while loop after the scanf statement like this:

Code:
if(strcmp(value, "king")) {
        printf("I'm King of the World!\n") ;
        weight = 12 ;
      }

make sure to include string.h
 
Last edited:
dduardo said:
card.c
Code:
#include <stdio.h>

int main( void ) {

  FILE *fp ;
  char value[15], suit[15] ;

  fp = fopen("card.dat","r") ;
  if( !fp ) {
    printf("Error Opening File\n") ;
  } else {
    while( !feof(fp) ) {
      fscanf(fp, "%s of %s\n", value, suit) ;
      printf("%s is of suit %s\n", value, suit) ;
    }
  }

  fclose(fp) ;
  return 0 ;
}

card.dat
Code:
king of clubs
six of clubs
four of diamonds

Output
Code:
king is of suit clubs
six is of suit clubs
four is of suit diamonds

If you want to weigh each card then put a bunch of if statements in the while loop after the scanf statement like this:

Code:
if(strcmp(value, "king")) {
        printf("I'm King of the World!\n") ;
        weight = 12 ;
      }

make sure to include string.h

Thank you. I understand this. But my problem is this:

for example I have ten cards and every 5 cards I want to
put the weight of the five cards in ONE ARRAY.
When you write
Code:
if(strcmp(value, "king")) {
        printf("I'm King of the World!\n") ;
        weight = 12 ;
      }

the weight is not place in a cell of an array say hand[0].

this is what I want to do: find the weigth and to it in an array.
I hope that you understand what I waht to do.
B
 
Code:
while (!feof(fp)) 
{      
      n = 0;
      while (n < 5 && !feof(fp))
      {
	  fscanf(fp, "%s of %s\n", value, suit);

	  if (!strcmp(value, "king")) 
		   weight = 12;
	  else if (!strcmp(value, "queen"))
		   weight = 11;
	  else if (!strcmp(value, "jack"))
		   weight = 10;
	  else if
		etc...
	      
	  weight_array[n++] = weight;
      }
	  
	  do something with weight_array
}
note that strcmp() returns zero if the strings are the same, so you should use: !strcmp(value, "king")
 
gerben said:
Code:
while (!feof(fp)) 
{      
      n = 0;
      while (n < 5 && !feof(fp))
      {
	  fscanf(fp, "%s of %s\n", value, suit);

	  if (!strcmp(value, "king")) 
		   weight = 12;
	  else if (!strcmp(value, "queen"))
		   weight = 11;
	  else if (!strcmp(value, "jack"))
		   weight = 10;
	  else if
		etc...
	      
	  weight_array[n++] = weight;
      }
	  
	  do something with weight_array
}
note that strcmp() returns zero if the strings are the same, so you should use: !strcmp(value, "king")


Thank you very much gerben!
 

Similar threads

Replies
14
Views
2K
Replies
40
Views
4K
Replies
4
Views
1K
Replies
5
Views
3K
Replies
14
Views
6K
Back
Top