Thread: saving scanf result for characters ?

  1. #1
    Registered User
    Join Date
    Dec 2023
    Posts
    13

    saving scanf result for characters ?

    Hi,

    so creating a menu with the choices of 1-5 (integers) and saving the result into "scanfResult" worked very nicely so Ive tried to implement the same logic to characters but somehow it doesnt work. The code doesnt printout any errors but if you type in a number it will just quit the programm instead of staying in the loop.

    ChatGPT this time also couldnt help me (used it this week and I have to say its an amazing tool if you have questions, but doesnt work always)

    SO does this even work with characters or is something wrong with the code ?

    I was under the impression that you can save the result of scanf whether you use %d / %c / %s or whatever and it will return/save the value 0 (didnt work) or 1 (worked) into like in this example "scanfResult". So if you use for example "%d" in "scanf" and someone enters in a character it will save the result as "0" or am I thinking wrong and it only works with integers ?

    Hope you understand what Iam trying to ask ...

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main(void)
    {
        char choice;
        int scanfResult = 0;
    
        while(true)
        {
            printf("Do you agree ? (y/n): ");
            scanfResult = scanf(" %c", &choice);
    
            if(scanfResult == 1)
            {
                if(choice == 'y' || choice == 'Y')
                    printf("I agree\n");
                else if(choice == 'n' || choice == 'N')
                    printf("I disagree\n");
                break; // Exit the loop if a valid character is entered
            }
            
            else if(scanfResult == 0)
            {
                while (getchar() != '\n'); // Clear the input buffer
                printf("Invalid input. Please enter 'y' or 'n'.\n");
                continue; // Continue the loop to ask again
            }
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    scanf() tries to read whatever you specified. In case of %c it reads the first char from stdin, regardless what character it is. So, if you hit the 0 key it reads char '0' and returns 1 because it successfully copied char '0' into your choice variable. Now you're getting into your "if(scanfResult == 1)" branch. What happens if choice contains '0' there?

  3. #3
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Yea damn so %c also includes numbers, somehow I wasnt aware of it /facepalm.

    And BTW, also einDeutscher (aGerman) :P. Danke für die schnelle Antwort (thanks for the quick reply).

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    so %c also includes numbers
    Nope. "It also includes characters representing a digit." '0' is just for the char with value 48. This can be a little confusing in the firs place

    einDeutscher (aGerman)
    Yup.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to read in whitespace characters
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 05-22-2012, 11:15 AM
  2. Scanf and ordinary characters.
    By Mr.Lnx in forum C Programming
    Replies: 9
    Last Post: 03-04-2012, 01:21 PM
  3. saving name characters to files
    By clonedhero in forum C Programming
    Replies: 5
    Last Post: 01-16-2008, 03:20 PM
  4. saving a double to a file as 4 characters?
    By yahn in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2005, 03:44 PM
  5. Masking Characters In Scanf
    By coolshyam in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 08:55 AM

Tags for this Thread