-6

I'm trying to write a piece of code that asks for user input to type in their name. It's then supposed to read each character of their name and format it however I choose (in the code below, it adds a new line after each character).

The problem I'm having is when someone types in their name, it doesn't print the first letter but prints out the rest.

For example, if I were to type in Sneek, it only displays neek.

Now I'm a beginner at programming and even more so with C so I was wondering if there is a problem with my scanf statement or the loop.

Also, if i type in Sneek it displays neek but if I type in Sneek again, it displays it as Sneek so I'm assuming theres nothing wrong with the loop?

I've searched for quite a while on this issue but I can't seem to find any answers, any help would be much appreciated.

char ch;
printf("Please enter name:  ");
ch = scanf("%c", &ch);

while ((ch = getchar()) != EOF) {
    printf("%c\n", ch);

}
return 0;
Sneek
  • 21
  • 5
  • 1
    `getchar` intentionally returns an `int`, not a `char`! `ch = scanf(..., &ch)` is plain wrong. Read the documentation of functions you use! – too honest for this site Oct 06 '16 at 14:04
  • Does this answer your question? [Difference between int and char in getchar/fgetc and putchar/fputc?](https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc) – Trenton McKinney Sep 29 '20 at 18:24
  • The accepted answer is the same as that in the duplicate. Therefore this question should be closed as a duplicate. – Trenton McKinney Sep 29 '20 at 18:25

4 Answers4

2

Comment out the ch = scanf("%c", &ch);. You are consuming the first character there and never print it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
robert
  • 3,539
  • 3
  • 35
  • 56
2

There are multiple issues in the code fragment:

  • The ch = scanf("%c", &ch); serves no purpose and does not even properly read a byte from the file into ch. Note that scanf does store the byte into ch, but it is immediately overwritten as you store the return value into ch as well. Just remove this line.

  • while ((ch = getchar()) != EOF) { printf("%c\n", ch); } is correct for your purpose but the type of ch must be int instead of char to accommodate all values of unsigned char and the special value EOF. As currenly written, your code will fail to stop at end of file on platforms where char is an unsigned type.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I agree that the `scanf()` is not helpful, but what's with the "does not even properly read a byte from the file" issue? It looks good to me, unless you're complaining that it doesn't check whether a character was read successfully, which should be characterized differently (as "you don't check that the I/O operation worked"). – Jonathan Leffler Oct 06 '16 at 14:25
  • @JonathanLeffler: the offending `scanf()` reads a byte from the file, stores it into `ch` and then returns 1 if successful, which in turn gets stored into the same variable `ch`. For argument's sake, the byte is properly read from the stream, but its value is not properly stored for further use ;-) – chqrlie Oct 06 '16 at 21:39
  • Ah; I missed the assignment to `ch` (as well as using it in the argument list). Yes; that's appalling — and "does not properly read" is accurate. I should have spotted that abuse, but maybe you could point out how bad it is (it may not be obvious to everyone). – Jonathan Leffler Oct 06 '16 at 21:59
0

This statement reads a character from standard input via scanf.

ch = scanf("%c", &ch);

This loop reads characters from standard input via getchar.

while ((ch = getchar()) != EOF) {
    printf("%c\n", ch);

}

What happens to the first character after it is read with scanf? It is overwritten with the result of getchar.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
0

I don't see the problem. You get the first character. After that, you get and print characters until EOF.

It's not printing the first character because you didn't tell it to.

hymie
  • 1,982
  • 1
  • 13
  • 18