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.

Recommended Answers

All 5 Replies

use getchar instead of fgets Click Here
your face is 2 bytes, you read 3 in it

Yes, i am agree with Sokurenko use

getchar instead of fgets

bcoz face is2 bytes.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.