How to do the following in C (ANSI)

  • Thread starter brad sue
  • Start date
In summary, the conversation discusses using C (ANSI) to read a set of 5 cards from a file and count the number of "six" and "heart" occurrences. The suggestion is to use fscanf and strcmp to compare the values and suits of each card, and increment a counter if there is a match. The conversation also delves into extracting values from the cards and placing them in an array, with a suggested solution of using a while loop to read each line and a series of if statements to assign weights to each card.
  • #1
brad sue
281
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
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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:
  • #6
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
 
  • #7
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")
 
  • #8
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!
 

FAQ: How to do the following in C (ANSI)

1. What is the difference between "printf" and "fprintf" in C?

The main difference between "printf" and "fprintf" in C is that "printf" outputs data to the standard output stream (usually the display screen), while "fprintf" outputs data to the specified stream (such as a file or a printer).

2. How do I declare and initialize an array in C?

To declare and initialize an array in C, you can use the following syntax:
type array_name[size] = {initialization values};
For example, to declare and initialize an array of 5 integers, you can use:
int numbers[5] = {1, 2, 3, 4, 5};
Note that the size of the array must be specified and the initialization values must match the data type of the array.

3. How do I use conditional statements (if/else) in C?

To use conditional statements in C, you can use the following syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Note that the "else" statement is optional and can be omitted if not needed.

4. What is the purpose of "typedef" in C?

The "typedef" keyword in C is used to create an alias or an alternative name for a data type. This can be useful for simplifying complex data types, improving code readability, or for creating portable code that can be easily changed in the future.

5. How do I allocate memory dynamically in C?

To allocate memory dynamically in C, you can use the "malloc" function from the "stdlib.h" library. The syntax is as follows:
pointer_variable = (cast-type*) malloc(size);
For example, to allocate memory for an array of 10 integers, you can use:
int *numbers;
numbers = (int*) malloc(10 * sizeof(int));
Remember to always free the allocated memory using the "free" function when it is no longer needed to avoid memory leaks.

Similar threads

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