3

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);
}
MikO
  • 18,243
  • 12
  • 77
  • 109

3 Answers3

3

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
0

You can add a white space after the first format specifier.It causes the program to read any white space(including Enter) but not store it.

scanf("%c ", & input);

This is clearly explained in the following page.Refer to the "Parameter" section and go to the paragraph captioned "Whitespace Character".

http://www.cplusplus.com/reference/cstdio/scanf/

CORRECTION What I meant is that if we use whitespace between two format specifiers in cases like the following, then it "eats up" the Enter or other whitespace.My bad, in the above statement of mine, it would keep on asking for more whitespace as we are using whitespace after all format specifier(s)

scanf("%c %d", &input1,&input2);
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
  • 1
    This does not work, the `scanf` will wait for more whitespace characters. – effeffe Apr 14 '13 at 14:42
  • @effeffe Indeed,it ignores 1 or more "Enters" as input.But once a non-white-space value is entered, it is accepted corresponding the next format specifier after the white-space in the scanf() format string.Go through that link carefully, and you can see what I mean.If in the format string you have a %c and %d separated by a white-space, then after accepting a character,scanf() ignores all white space(Enter included) but when an integer is entered,it is accepted. – Rüppell's Vulture Apr 14 '13 at 14:44
  • We have to read just *one* new line character, not "one or more". That makes the program unusable, have you tried it? And about your edit: in that case the space in the format string is useless since `"%d"` already consume any whitespace character before trying to read the integer. – effeffe Apr 14 '13 at 14:51
  • @effeffe Mate,I consider that linked site "cplusplus" a reliable one.Am I wrong?Do tell me.And here's what mentioned in that site about whitespace in the format string of scanf() ---"Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace)" – Rüppell's Vulture Apr 14 '13 at 14:54
  • @effeffe I personally prefer to do it the way Daniel Fischer mentioned above.But whitespace is another way to do it.Hence I pointed it out. – Rüppell's Vulture Apr 14 '13 at 14:55
  • I also think cplusplus.com is a pretty reliable source, and I know how `*scanf` functions work, but your code is not "another way" to do it, it is really different from the Daniel Fischer's one and it leads to a completely different behavior. But if you like it, no problem, it just doesn't seem to be what the user was looking for, but maybe I'm wrong. – effeffe Apr 14 '13 at 15:02
  • @effeffe Well,actually my code is incomplete and confusing (I just noticed it).You are right that it will wait for more whitespace characters.What I meant is using whitespace is fine if you are using it between two format specifiers like %c and %d.But if you use it after all format specifiers it will keep on asking for more whitespace.Thanks for pointing it out.What I meant is something like scanf("%c %d", &input1,&input2); is totally valid if we are go by what that link says. – Rüppell's Vulture Apr 14 '13 at 15:06
  • As I said in a previous comment, `"%c %d"` and `"%c%d"` are equivalent, in some cases whitespace doesn't do anything. But this is another story and it's not about the question :) it's not explained in the cplusplus.com page, look at the standard, 7.21.6.2/7 and following (C11). – effeffe Apr 14 '13 at 15:16
  • @effeffe I am sure you would agree this line in the above link is confusing.It says "A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).".So I drummed into my head whatever was mentioned in the page.Now you say "%c %d" and "%c%d" are same.And you sound convincing too.I am really confused.What should one do?Is that site containing stuff that goes against the standard?Please check it out,it's indeed written that whitespace in format string "eats out" whitespace in input string. – Rüppell's Vulture Apr 14 '13 at 15:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28195/discussion-between-effeffe-and-sheer-fish) – effeffe Apr 14 '13 at 15:36
  • @effeffe OK.I just saw ur message. – Rüppell's Vulture Apr 14 '13 at 15:40
0

Try fflush(stdin); after scanf