0

I was trying to input 5 characters from the keyboard, however my function scans newlines that I did not enter and that are stored in the array I created. I have put some watches, but I can't figure out what I'm doing wrong. Can you help me out please?

Here is part of the code:

#include <stdio.h>
int main(void){
    int a, b, l;
    int ca[10];
    char co[10];
    printf("Insert values.\n");
    for(a=0; a<5; ++a)
    {
        scanf("%d", &ca[a]);
    }
    printf("Insert suits.\n");
    for(b=0; b<5; ++b)
    {
        scanf("%c", &co[b]);
    }
}
  • 1
    Can you provide a sample output? What was expected vs what was received. – Nathan Wride Apr 02 '20 at 23:30
  • 1
    Informed guesswork — Use `" %c"` instead of `"%c"`; that leading space makes all the D in the W (difference in the world). Don't play with trailing spaces in format strings; they're a UX disaster. — I should not be having to guess; you should be showing the input you're supplying. However, if you type 's', return, 'h', return, 'd', return (for spades, hearts, diamonds), your `scanf()` is also reading the return character into your data. – Jonathan Leffler Apr 02 '20 at 23:31
  • 1
    Did you press enter? If so, then you did enter them. If not, then it shouldn't have read them. What *precisely* did you type? – David Schwartz Apr 02 '20 at 23:35
  • I put 'O\nO'(I didnt put the newline is just to say the second input was on another line) then the watches came out like this: co[0]=\n, co[1]=O, co[2]=\n, co[3]=O,co[4]=\n – Ana Ferreira Apr 02 '20 at 23:35
  • 2
    @AnaFerreira Another line *is* a newline. Newlines are what separates lines. The difference between "a" and "b" on another line is the newline between them. Otherwise, it's identical to "ab", which it isn't. – David Schwartz Apr 02 '20 at 23:36
  • The return you type is a perfectly good character as far as `%c` is concerned — it's a newline, `\n`. Unlike the majority of conversion specifications, `%c` does not skip leading white space. (Only two other conversion specifications don't skip white space — `%[…]` (scan sets), and `%n`.) – Jonathan Leffler Apr 02 '20 at 23:37
  • So I shouldnt press enter? – Ana Ferreira Apr 02 '20 at 23:37
  • @AnaFerreira You should press enter if, and only if, your software was designed for you to press enter. What you type and what your program expects have to match. – David Schwartz Apr 02 '20 at 23:38
  • 1
    You could enter the suits on one line. You'd still have the first newline left over from the previous `scanf("%d", …)`. So, the ***`" %c"`*** is what you really need. – Jonathan Leffler Apr 02 '20 at 23:39
  • 1
    You could use `fgets()` to read whole lines, then use `sscanf()` to parse the line. – Barmar Apr 02 '20 at 23:39

1 Answers1

0

Maybe you should use the getchar function if you need to read only character from stdio? scanf is a more complicated function for using it to read one character

This function have some tricks about how it parse the input string. For example, this function not read the space symbols and parse it like delimiter

Evgeniy
  • 303
  • 2
  • 9
  • All except three of the conversion specifiers skip leading white space, whether you want it to or not. The three exceptions are `%c`, `%[…]` (scan sets) and `%n`. – Jonathan Leffler Apr 03 '20 at 02:48