Character User-input Either Skipped or not Skipped yet not Stored

49 Views Asked by At

I am currently attempting to obtain a user character input which proceeds a user non-character input, such as integer, float/double, etc. I've read several Stack overflow solutions, and not a single one seems to work for me. Here are the six different ways I attempted to write this code:

#include <stdio.h>
int main(void)
{
  int integer;
  char character;
  scanf("%d", &integer);
  fflush(stdin);
  scanf("%c", &character);
  printf("The ASCII Code of %c is %d", character, character);

This gave an ASCII Code of 10 (\n, i.e. line feed character) implying fflush(stdin) did not flush the line feed whitespace. Then, from this, it would be more relevant and convenient if we looked at lines 7-8. Now, I deleted fflush(stdin) and added a space before %c conversion specifier in scanf(), i.e. scanf(" %c", &character) This also did not work, thus I tried the following: scanf("%c\n", &character) This did allow me to input a value, unlike the previous scenarios, albeit the character was not the same as that inputted because the ASCII code generated was still 10. I also attempted to manipulate the code using getchar() but the ASCII code was either 0 or 10 (space or line feed) and not the actual character input. Thus, I would very much appreciate it if anyone knows a way to resolve this issue. Thank you!

0

There are 0 best solutions below