How to do the following in C (ANSI)

  • Thread starter Thread starter brad sue
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around reading a set of card strings from a file in C (ANSI) and counting specific occurrences of words such as "six" and "heart". Participants explore various methods for reading lines, parsing strings, and managing arrays to store card values.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on using fgets to read lines and count occurrences of specific words within those lines.
  • Another participant suggests using fscanf to read formatted strings and then applying strcmp to compare values and suits against "six" and "heart".
  • There are multiple mentions of issues with reading from a file, particularly how to skip the first line if it contains an integer.
  • Participants discuss the need to extract card values into an array and the challenges faced when attempting to implement this in their code.
  • Some participants propose using if statements within a loop to assign weights to cards based on their names, but express confusion about storing these weights in an array.
  • There are repeated suggestions to ensure correct usage of strcmp for string comparison, emphasizing the need to check for equality properly.

Areas of Agreement / Disagreement

Participants generally agree on the methods for reading and parsing strings but express differing opinions on the best way to manage file input and array storage. The discussion remains unresolved regarding the optimal implementation details.

Contextual Notes

Participants mention issues with file reading and the handling of strings, indicating potential limitations in their current understanding of C programming concepts such as pointers and memory management.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in file I/O, string manipulation, and array management in the context of card games or similar applications.

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
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K