1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reading character input with scanf

  1. May 12, 2012 #1
    Code (Text):
    printf("\nSpecify ascending ['a'] or descending 'd' >> ");scanf("%c",&pref);flushInputBuffer();
        extract_array(s,n,crit,array1);extract_array(s,n,2,array2);
        if (pref == '\n')pref = 'a';
        printf("\n%c\n",pref);
    So the above is a snippet of code from one of the programs I am writing. My problem is that, even if I enter 'd' for my input, the program seems to decide ALWAYS that I mean to enter 'a'. I only want the computer to interpret my input as an 'a' if I just press the return key when prompted for input. Can anyone help me see the problem here?
     
  2. jcsd
  3. May 12, 2012 #2

    I like Serena

    User Avatar
    Homework Helper

    It depends on what comes before.

    What is the type of pref?
    What is the initial value of pref?
    What is the last thing you've read from input successfully?
    Is there still input lying around in the input buffer (a new line perhaps)?
     
  4. May 12, 2012 #3
    pref is type char, which matches with the %c. Pref is initially unassigned I just have
    Code (Text):
    char pref;
    The last successfully read character could have been almost anything, since the output of the program, a piece of which is above, has multiple functions depending on the last read character. However, there should not be any input lying around in the buffer -- the flushInputBuffer function should ensure that. In particular, I have
    Code (Text):
    void flushInputBuffer()
    {
        char c;
        do {
            scanf("%c",&c);
        } while (c != '\n');
    }
    Since I cleared the buffer at the last character scan, too, I'm not sure what the problem is.
     
  5. May 12, 2012 #4

    I like Serena

    User Avatar
    Homework Helper

    So what happens if you copy and paste these couple of lines to the beginning of your main() function?
    You should see that if you type 'd' followed by Enter, that your programs reads 'd', after which the new line is flushed.

    Just for fun, copy those lines a second time after the first, and see if you can type 'd' successfully again...
     
  6. May 12, 2012 #5
    EDIT: Let me try adding the conditional if statement. I'll let you know if it changes something.
    Code (Text):
    #include <stdio.h>
    void flushInputBuffer();

    int main(void)
    {
        char pref;
        int j;
        for (j=0;j<10;j++)
        {
            printf("\nSpecify ascending ['a'] or descending 'd' >> ");
            scanf("%c",&pref);flushInputBuffer();
            printf("\nYou entered character: %c\n",pref);
        }
        return (0);
    }


    void flushInputBuffer()
    {
        char c;
        do {
            scanf("%c",&c);
        } while (c != '\n');
    }
     
    Output:
    So it seems to be working, which is why I am confused.
     
  7. May 12, 2012 #6
    So as it turns out, it still works with the if (pref == '\n')pref = 'a';

    However, I have to press the return key twice in order to get it to tell me that I secretly entered the letter 'a'. It is not immediate.
    Output:


    Specify ascending ['a'] or descending 'd' >>


    You entered character: a

    Specify ascending ['a'] or descending 'd' >> a

    You entered character: a

    Specify ascending ['a'] or descending 'd' >> d

    You entered character: d

    Specify ascending ['a'] or descending 'd' >> a

    You entered character: a

    Specify ascending ['a'] or descending 'd' >>


    You entered character: a

    Specify ascending ['a'] or descending 'd' >>


    You entered character: a

    Specify ascending ['a'] or descending 'd' >>
     
  8. May 12, 2012 #7
    Here's the code again:

    Code (Text):
    /*
     * test.c
     *
     *  Created on: 25. apr. 2012
     *      Author: James
     */

    #include <stdio.h>
    void flushInputBuffer();

    int main(void)
    {
        char pref;
        int j;
        for (j=0;j<10;j++)
        {
            printf("\nSpecify ascending ['a'] or descending 'd' >> ");
            scanf("%c",&pref);flushInputBuffer();
            if (pref == '\n')pref = 'a';
            printf("\nYou entered character: %c\n",pref);
        }
        return (0);
    }


    void flushInputBuffer()
    {
        char c;
        do {
            scanf("%c",&c);
        } while (c != '\n');
    }
     
     
  9. May 12, 2012 #8

    I like Serena

    User Avatar
    Homework Helper

    So either you have old input floating around, or your program does decide to pick choice 'd', but you're mistaken in your conclusion that it picked 'a'.

    You did say that your program "seemed" to always pick 'a'...

    I do not understand what you mean.
    Where and how do you need to press Enter twice?

    Btw, does your extract_array() function do anything that may interfere?
     
  10. May 12, 2012 #9

    I like Serena

    User Avatar
    Homework Helper

    Oh, if you press Enter instead of a letter, your flushInputBuffer() function will force you to press Enter a second time.
     
  11. May 12, 2012 #10
    This is what the extract_array function is:
    Code (Text):
    void extract_array(student_t s[], int n, int crit, aux_t array[])
    {
        int j;
        char temp[STRSZ];

        for (j=0; j<n; j++) {
            switch (crit) {
                case 1: strcpy(temp, s[j].first);
                    break;
                case 2: strcpy(temp, s[j].last);
                    break;
                case 3: strcpy(temp, s[j].food);
            }
            strcpy(array[j].string, temp);
        }
    }
    I don't think this should be interfering.

    I am confident that it is picking 'a' because I asked it to print out what it thought pref was after it had entered the if statement. It always returned 'a', even when I had entered 'd' as my input.
     
  12. May 12, 2012 #11
    Here's some sample output to show the point (just to specify, I changed the prompt output slightly, which is why it's not the same as before; the user input part is still all the same though):


    Choose to sort in ascending ['a'] or descending 'd' order >> d

    a
    Where do you want to print file?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook