-1
#include <stdlib.h>
#include <stdio.h>

struct card
{
   char Top;
   char Bottom;
};

int main()
    {
    int T,i;
    struct card cards[4];
    scanf("%d", &T);
    for(i=0; i<3; i++)
        {
            scanf("%c%c%c", &cards[0].Top, &cards[1].Top, &cards[2].Top);
            printf("%c%c%c\n", cards[0].Top, cards[1].Top, cards[2].Top);
        }
return 0;
}

It does not correctly print the three characters, outside the for loop it scans and prints fine.

richard bent
  • 13
  • 1
  • 3
  • 2
    "does not correctly print" is not a good description. What does it print? What do you want it to print? – klutt Aug 04 '18 at 14:41
  • Short summary of the duplicate: ```%c``` reads the whitespace (a line-break in particular) which concludes the numerical input. Use ```scanf(" %c%c%c", ...);``` – tevemadar Aug 04 '18 at 14:50

1 Answers1

1

Solution to your problem

Change the scanf line to this. (Note the spaces before %c)

scanf(" %c %c %c", &cards[0].Top, &cards[1].Top, &cards[2].Top);

Other comments on your code

ALWAYS check return value from scanf. It should look like this:

if(scanf(" %c %c %c", &cards[0].Top, &cards[1].Top, &cards[2].Top) != 3) {
     // Print error message and exit program or something else
} else {
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
klutt
  • 30,332
  • 17
  • 55
  • 95
  • Besides feeding on a dupe, for the current shape of the post one space is enough in the front (```" %c%c%c"```) – tevemadar Aug 04 '18 at 15:00
  • 1
    @tevemadar If it is enough or not depends. The post does not say so explicitly, but this is better if the user presses enter after each character. – klutt Aug 04 '18 at 15:08
  • @tevemadar And maybe I should not have posted this answer, but I did not find a good duplicate for reading multiple chars. – klutt Aug 04 '18 at 15:08