Search code examples
cwhile-loopasciibreakenter

how I can I stop the program from reading 'Enter' as an input?


Hi everyone I just finished my program which reads any single character and prints the ASCII value of that character however as it loops it starts to keep reading in enter as well as the other character.

My other problem is I want my program to stop when reading '#' and print it as invalid which I can't seem to do The code looks like this:

#include <stdio.h>
int main() {
  char input;
  while (input != '#') {
    printf("\nEnter character: \n");
    scanf("%c*c", & input);
    printf("The ASCII value is: %d", (int) input);
    if (input == '#') break;
  }
  printf("\n# is invalid");
  return (0);
}

Solution

  • You tried to suppress the newline with

    scanf("%c*c", & input);
    

    but you forgot the % in the suppression,

    scanf("%c%*c", & input);
    

    should do what you want.