Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a little code here that I'm writing as part of a class project in an intro to C course. The code should scan a character and then print the ascii value of the character. Here's my source code.
#include <stdio.h>
int main (int argc, char *argv[])
{
        printf("Welcome to Ascii\n");
        char  usernumber='1';

        while (usernumber != '0') {
                scanf("%c", &usernumber);
                printf("ascii: %d \n", usernumber);
        }
        return 0;
}


Which, given what I know about java and while loop logic, looks really fine to me.

My output looks like this though!

Welcome to Ascii
k
ascii: 107
ascii: 10
l
ascii: 108
ascii: 10
m
ascii: 109
ascii: 10


Is it interpreting the enter keypress as another char to evaluate? How can I stop it from doing that? It's bugging me out.
Posted
Comments
Vedat Ozan Oner 17-Feb-14 13:51pm    
scanf("%c\n", &usernumber);
Lextard 17-Feb-14 14:03pm    
That makes the loop only print the value on its next iteration, which is weird.
Like if I input k, I will get no print, then if I input l, it will print the ascii value for k. Woo.
Lextard 17-Feb-14 14:11pm    
Got it! Giving "%c" a leading space (" %c") will ignore the last in, (the line feed (ascii 10) of the previous scanf input). Which does it. Thanks for taking the time to help me out!

ENTER is a character: 10 Decimal, or 0A Hex.
If you don't want it to print, then check for it, and specifically exclude it:
C
while (usernumber != '0') {
        scanf("%c", &usernumber);
        if (usernumber != '\x0a') 
             printf("ascii: %d \n", usernumber);
 
Share this answer
 
Comments
Lextard 17-Feb-14 14:38pm    
This would work! Thanks :)
OriginalGriff 17-Feb-14 15:02pm    
You're welcome!
try this:

$ cat 2.c
#include <stdio.h>
int main (int argc, char *argv[])
{
        printf("Welcome to Ascii\n");
        char  usernumber='1';

        while (usernumber != '0') {
                scanf("%1[^\n]%*c", &usernumber);
                printf("ascii: %d \n", usernumber);
        }
        return 0;
}


http://www.cplusplus.com/reference/cstdio/scanf/[^]
 
Share this answer
 
Comments
Lextard 17-Feb-14 14:50pm    
scanf("%1[^\n]%*c", &usernumber)

What exactly does this say? I read it as "scan a char of any length, not including the 1 char /n (line feed || acii 10)". The second % sign is still part of the same statement? Or are we specifying two things scanf should be "aware" of, %*c (any amount of chars) and %1[^/n] (not the line feed char).

I think I understand it, but if you could give it to me in your words I'd appreciate it!
Vedat Ozan Oner 17-Feb-14 15:17pm    
it means read 1 char from stdin except '\n', than skip next one (which is \n).
Thanks for the help everyone! Here's my final code.

The bit I posted before has been nested into an if/else function so the user can input as many chars as they want while the program runs ('0' will end the program) or they can call the program with chars as arguments and the program will print out the asciis and terminate. Thanks everyone for taking the time to help!

        printf("Welcome to Ascii\n");
        char  userchoice='1';

        while (userchoice != '0') {
                scanf(" %c", &userchoice);
                printf("ascii: %d \n", userchoice);
           }
    
        return 0;
}
 
Share this answer
 
v3
scanf always reads an entire line of input, i. e. anything up to - and including - the point where you press enter.

If you want to immediately get and interpret a pressed key, without having to also press enter, use the function getchar()[^] or getc()[^] or fgetc()[^]. The following calls are equivalent:
C++
#include "stdio.h"
char c1 = getchar();
char c2 = getc(stdin);
char c3 = fgetc(stdin);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900