1

I am trying to fill a simple array with some characters, my loop looks likes this

for(i = 0; i <= 6; i++) {
    printf("Enter each value of the plate number:");
    scanf("%c", &arr[i]);
    printf("i = %d\n", i);
}

When I execute the loop, I this is what happens,

Enter each value of the plate number:A
i = 0
Enter each value of the plate number:i = 1
Enter each value of the plate number:B
i = 2
Enter each value of the plate number:i = 3
Enter each value of the plate number:C
i = 4
Enter each value of the plate number:i = 5
Enter each value of the plate number:D
i = 6

I don't know why I am skipping every 2nd chance to get the user input.

Clayton D
  • 37
  • 7
  • 2
    At the first iteration, you entered `A` followed by `\n`. That's two characters. – r3mainer Oct 11 '18 at 22:35
  • What squeamish sadi. Try adding `printf("arr[i] = %d\n", (int) arr[i]);` into the loop to see the numeric codes for the characters you actually got. You'll probably see "10" after each entry (the code in most character sets for `\n`). – aschepler Oct 11 '18 at 22:37
  • Thanks! both of you I get whats wrong with it! – Clayton D Oct 11 '18 at 22:38

1 Answers1

0

Try this scanf(" %c", &arr[i]); (space before %c)

Or scanf("%c\n", &arr[i]);

You have this problem because scanf keep the enter in the input buffer.

Sky
  • 36
  • 3