简体   繁体   中英

Multiple printf/scanf affecting output

I have a simple program that consists of 2 parts.

One part asks for your favorite number and then returns what your favorite number is.

The other part asks what your favorite letter is and returns the ASCII code for that character.

Each part works independently of the other, and when the favorite letter part of the code is in front of the favorite number part of the code, both parts execute correctly in order.

However, I'm having a problem when I modify the order of the code so that the favorite number part is before the favorite letter part. The output allows me to type in my favorite number, which it then scans and returns. Then it just prints the rest of the program without allowing me a chance to type in my favorite letter.

Code that works correctly:

#include <stdio.h>

int main() {
    int favorite_number;
    char favorite_letter;
    int favorite_letter_code;

    /* FAVORITE LETTER */
    printf("Enter your favorite letter> \n");
    scanf("%c", &favorite_letter);
    favorite_letter_code = (int)favorite_letter;
    printf("The ASCII code for your favorite letter is is %d\n", 
           favorite_letter_code);

    /* FAVORITE NUMBER */
    printf("Enter your favorite number> \n");
    scanf("%d", &favorite_number);
    printf("Your favorite number is %d!\n", favorite_number); 

    return 0;
}

Code that works incorrectly (just switch favorite letter and favorite number sections):

#include <stdio.h>

int main() {
    int favorite_number;
    char favorite_letter;
    int favorite_letter_code;

    /* FAVORITE NUMBER */
    printf("Enter your favorite number> \n");
    scanf("%d", &favorite_number);
    printf("Your favorite number is %d!\n", favorite_number); 

    /* FAVORITE LETTER */
    printf("Enter your favorite letter> \n");
    scanf("%c", &favorite_letter);
    favorite_letter_code = (int)favorite_letter;
    printf("The ASCII code for your favorite letter is is %d\n", 
           favorite_letter_code);

    return 0;
}

Why should this order affect the output?

After reading favorite_number with scanf , the linefeed you typed is still pending in stdin , you should ignore spaces when reading the favorite_letter in order to skip it:

scanf(" %c", &favorite_letter);

Note the space in front of %c , it tells scanf to ignore any white space.

When you do scanf("%d", ...) , it reads an integer. When you input text on the console, you input an integer and then press enter. That scanf call doesn't consume the newline character generated by the enter key (it stays in the input buffer). When you do scanf("%c", ...) , it sees a character already in the buffer (the newline) and reads it in instead of what you're expecting. You need to explicitly account for that newline character. It's one of the more annoying parts about using scanf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM