0

I'm learning C and I created a small program which outputs the ASCII code of the entered character or outputs the character from ASCII Code entered. It first asks the user whether they want to convert Character to ASCII Code or ASCII Code to Character.

I don't know what the problem is in my code. When I run it as a separate file, it works fine.

# include <stdio.h>
int main(void) {
    char character;
    printf("Enter a character: \n");
    scanf("%c", &character);
    printf("The character you entered: %c\n", character);
    printf("ASCII code of %c is %d\n", character, character);
 }

The above code works fine. This is the code which converts character to ASCII Code.

Here's the code which has the option to choose which one to run :

#include <stdio.h>

int main(void) {
    char option;
    printf("-------- MENU --------\n");
    printf("1. Get ASCII code of a character.\n");
    printf("2. Get character from ASCII code.\n");
    printf(":  ");
    scanf("%s", &option);
    if (option == '1') {
        char character;
        printf("Enter a character: \n");
        scanf("%c", &character);
        printf("The character you entered: %c\n", character);
        printf("ASCII code of %c is %d\n", character, character);
    }
    else {
        if (option == '2') {
            int number;
            printf("Enter a Number: \n");
            scanf("%d", &number);
            printf("The number you entered: %d\n\n", number);
            printf("The ASCII character with code %d is %c\n", number, number);
        }
        else {
            printf("Invalid Input !\n");
        }
    }
}

The above code asks the user to type in 1 or 2 to decide to run Convert Character to ASCII Code or Convert ASCII Code to Character. When I run this code, If I choose option 2, it works just fine and gives me a proper result and if I give 3 or any other invalid option, it gives me "Invalid Input !" printed on the screen. But, when I choose option 1, It just prints out this

-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
:  1
Enter a character: 
The character you entered: 

ASCII code of 
 is 10

I don't know what the problem is. Please help me figure it out.

Sarvesh M.D
  • 192
  • 1
  • 11
  • 1
    Change `scanf("%c", &character);` to `scanf(" %c", &character);` The `scanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. It will be read by the *next* `scanf()`. Format specifiers `%d` and `%s` and `%f` automatically filter such leading whitespace characters, but `%c` and `%[]` and `%n` do not. You can instruct `scanf` to do so by adding a space just before the `%`. – Weather Vane Jun 23 '21 at 10:01

1 Answers1

0

the problem is the second scanf and the %c. You have to add a white space like this scanf(" %c").

Full code:

#include <stdio.h>

int main(void) {
    char option;
    printf("-------- MENU --------\n");
    printf("1. Get ASCII code of a character.\n");
    printf("2. Get character from ASCII code.\n");
    printf(":  ");
    scanf("%s", &option);
    if (option == '1') {
        char character;
        printf("Enter a character: \n");
        scanf(" %c", &character);
        printf("The character you entered: %c\n", character);
        printf("ASCII code of %c is %d\n", character, character);
    }
    else {
        if (option == '2') {
            int number;
            printf("Enter a Number: \n");
            scanf("%d", &number);
            printf("The number you entered: %d\n\n", number);
            printf("The ASCII character with code %d is %c\n", number, number);
        }
        else {
            printf("Invalid Input !\n");
        }
    }
}

Output:

-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
:  1
Enter a character: 
a
The character you entered: a
ASCII code of a is 97

Please look at the following question were the reason is explained.

JANO
  • 2,995
  • 2
  • 14
  • 29
  • so `option` is type `char` but you give `"%s"` in the scanf ? What would happen if I input more than one character (don't forget, when you will press 'enter' to validate, it will result in '\n' in the input) ? – Tom's Jun 23 '21 at 12:18