int main(void)
{
    char faceValue[2];
    char suitValue[2];

    //User enters the value of their hole cards.

    setCardFaceValue(faceValue);
    setCardSuitValue(suitValue);

    for(int i=0;i<2;i++)
        printf("Element %i: %c %c\n",i,faceValue[i],suitValue[i]);

    return 0;
}

void setCardFaceValue(char* face)
{
    char card[2][20] = {"left card:","right card:"};

    for(int i=0;i<2;i++) {
        printf("Enter face value for ");

        for(int x=0;x<19;x++) {
            printf("%c",card[i][x]);

            if(card[i][x] == ':') {
                fgets(&face[i],3,stdin);
            }
        }
    }
}

void setCardSuitValue(char* suit)
{
    char card[2][20] = {"left card:","right card:"};

    for(int i=0;i<2;i++) {
        printf("Enter suit value for ");

        for(int x=0;x<19;x++) {
            printf("%c",card[i][x]);

            if(card[i][x] == ':') {
                fgets(&suit[i],3,stdin);
            }
        }
    }
}

Here is the output:
Click Here

The value entered for the first question should be assigned to element 0 in the faceValue array.
The value entered for the second question should be assigned to element 1 in the faceValue array.

The value entered for the third question should be assigned to element 0 in the suitValue array.
The value entered for the fourth question sholuld be assigned to element 1 in the suitValue array.

This does not seem to happen, they get jumbled up and I know it has somethiing to do with the buffer, but I don't know what, any insight would be helpful.

Edited 4 Years Ago by CharlieBrook: Space between lines

I am somewhat puzzled by the use of this loop to print out the two different strings in card[][]:

     for(int x=0;x<19;x++) {
         printf("%c",card[i][x]);

        if(card[i][x] == ':') {
            fgets(&face[i],3,stdin);
        }
     }

Wouldn't it be just as easy to use

    printf("Enter suit value for %s ", card[i]);
    face[i] = getchar();
    getchar();  /* clear out the trailing newline */

given that the last character of the strings is known to be the colon?

Edited 4 Years Ago by Schol-R-LEA

or maybe something like that to clear input buffer, if you have better solution please tell

/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  char c;
  int iNeededChars = 2;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  do
  {   
     c = getchar();//get needed char
     putchar (c);
     putchar('\n');
     while(c != '\n')
     {
         c = getchar();
         printf("Clearing [%c]", c);
     }
   iNeededChars--;
  }
  while(iNeededChars); 
  return 0;
}

The error is much more serious than just clearning the input buffer. The loop that starts on lines 21 and 41 overflow the buffer by about 20 bytes.

fgets(&suit[i],3,stdin);

The buffer suit only contains 2 bytes of memory, but the i counter counts from 0 to 20. What is it supposed to do with bytes 2 thru 20? Answer: scribble all over your programs memory space then Crash.

This article has been dead for over six months. Start a new discussion instead.