Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to do the following in C (ANSI)

  1. Mar 10, 2006 #1
    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 dont know how to use them..
    Please Can I have some indications?
    Thank you for your help.
    B
     
  2. jcsd
  3. Mar 10, 2006 #2

    dduardo

    User Avatar
    Staff Emeritus

    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.
     
  4. Mar 11, 2006 #3
    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 dont succeed to skip the first line I tried LINE+1 but it is an horizontoal displacement..
    Can I have some suuggestions?
     
  5. Mar 13, 2006 #4
    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 dont succeed to extract those values to create an array.
    where in my code should I place my loop to create an array hand[].

    Code (Text):

    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.
     
  6. Mar 13, 2006 #5

    dduardo

    User Avatar
    Staff Emeritus

    card.c
    Code (Text):

    #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 (Text):

    king of clubs
    six of clubs
    four of diamonds
     
    Output
    Code (Text):

    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 (Text):

    if(strcmp(value, "king")) {
            printf("I'm King of the World!\n") ;
            weight = 12 ;
          }
     
    make sure to include string.h
     
    Last edited: Mar 13, 2006
  7. Mar 13, 2006 #6
    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 (Text):

    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
     
  8. Mar 15, 2006 #7
    Code (Text):
    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")
     
  9. Mar 15, 2006 #8
    Thank you very much gerben!!!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to do the following in C (ANSI)
  1. Is this ANSI C++? (Replies: 15)

  2. How do you compile C? (Replies: 5)

Loading...