Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Odd scanf() issue in C

  1. #1
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Odd scanf() issue in C

    Here is my program goal, get 2 characters and show the ascii values. however the program isnt acting right. It will take the first input but skip over the second so I always have a ASCII 10 for the second variable. What did I do wrong?

    Here is my code :

    Code:
    /****************************
     *  Chapter 3, Excercise 4  *
     *  Andrew Schott  12-11-4  *
     ****************************/
     
    void main(){
        char   keypress1,
            keypress2,
             difference;
             
       printf("\nPlease enter the first character :");
       scanf("%c", &keypress1);
       
       printf("\nPlease enter the second character :");
       scanf("%c", &keypress2);
       
       printf("\nThe ASCII code for the first is %d",keypress1);
       printf("\nThe ASCII code for the second is %d",keypress2);
       
       if (keypress1 > keypress2) 
          difference = keypress1 - keypress2;
       else difference = keypress2 - keypress1;
          
       printf("\nThe difference in their ascii codes are %d\n", difference);
    }

  2. #2

    Re:Odd scanf() issue in C

    The reason that is happening is because the '\n' character is left in the input queue. Therefore, add these lines after taking in the first character:
    Code:
    char tmp;
    do
    {
    scanf("%c", &tmp);
    } while (tmp != '\n');

  3. #3
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Odd scanf() issue in C

    Thanks.

    That did the trick.

  4. #4
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Odd scanf() issue in C

    Well, for some reason, the stored character is ascii 10, the linefeed.

    here is what I have at the moment:

    Code:
    /****************************
     *  Chapter 3, Excercise 4  *
     *  Andrew Schott  12-11-4  *
     ****************************/
    
    #include <stdio.h>
     
    void main(){
     charkeypress1,
    keypress2,
    difference;
    
    printf("\nPlease enter the first character : ");
    do {scanf("%c", &keypress1);} while (keypress1 != '\n');
    
    printf("\nPlease enter the second character : ");
    do {scanf("%c", &keypress2);} while (keypress2 != '\n');
    
    printf("\nThe ASCII code for the first is %d",keypress1);
    printf("\nThe ASCII code for the second is %d",keypress2);
    
    if (keypress1 > keypress2) 
    difference = keypress1 - keypress2;
    else difference = keypress2 - keypress1;
    
    printf("\nThe difference in their ascii codes are %d\n", difference);
    }

  5. #5

    Re:Odd scanf() issue in C

    You are welcome!

    [quote author=Schotty link=board=9;threadid=10060;start=0#msg91111 date=1102811713]
    Well, for some reason, the stored character is ascii 10, the linefeed.

    here is what I have at the moment:

    Code:
    /****************************
     *  Chapter 3, Excercise 4  *
     *  Andrew Schott  12-11-4  *
     ****************************/
    
    #include <stdio.h>
     
    void main(){
        char   keypress1,
             keypress2,
             difference;
    
       printf("\nPlease enter the first character : ");
       do {scanf("%c", &keypress1);} while (keypress1 != '\n');   
       
       printf("\nPlease enter the second character : ");
       do {scanf("%c", &keypress2);} while (keypress2 != '\n');
       
       printf("\nThe ASCII code for the first is %d",keypress1);
       printf("\nThe ASCII code for the second is %d",keypress2);
       
       if (keypress1 > keypress2) 
          difference = keypress1 - keypress2;
       else difference = keypress2 - keypress1;
          
       printf("\nThe difference in their ascii codes are %d\n", difference);
    }
    [/quote]

    In the above, you run the loop and make your input variables store '\n'. i.e.:
    Code:
       printf("\nPlease enter the first character : ");
    /* Assign character to keypress1 until keypress1 holds '\n' */
       do {scanf("%c", &keypress1);} while (keypress1 != '\n');
    That is why in my code snuppet, I decided to create a temporary variable to hold the newline character.

  6. #6
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Odd scanf() issue in C

    I am confused. In my book (albiet a shitty one), I am not supposed to need all of this. I still cannot figure out how to take one character from the line and store it correctly, other commands included. I tried getchar() as well to similar results.

  7. #7

    Re:Odd scanf() issue in C

    It seems to me that from the behavior you are getting, the following is happening:
    1) You enter some character(s) and press Enter.
    2) All the characters (including the newline character) you enter are stored in the input queue.
    3) The function call scanf("%c", <address_of_char_variable&gt removes the first character in the input queue and stores it in the char variable.
    4) In the case where you enter 1 character followed by '\n', the first scanf() function call reads the character you provided and the second call reads the '\n' character. Hence, you need to loop to get rid of extra characters in the input queue.

    The above may be wrong, but from what I have seen with this program, and the solution I provided based on the above works, it seems that it is right.

    P.S. In case you are looking for a good book, try taking a look at C++ Primer Plus by Stephen Prata. I am currently using this book and it is very good. Also, there there is a C Primer Plus (by the same author IIRC), which I have not read.

  8. #8
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Odd scanf() issue in C

    I ended up getting this figured out. I ended up using getchar() and adding one after the first section. My instructor suggested trying that.

    Thanks for the help!

  9. #9

    Re:Odd scanf() issue in C

    [quote author=Schotty link=board=9;threadid=10060;start=0#msg91229 date=1103164587]
    I ended up getting this figured out. I ended up using getchar() and adding one after the first section. My instructor suggested trying that.

    Thanks for the help!
    [/quote]

    You are welcome! Good luck with learning C!

  10. #10
    Moderator
    Good Guru
    Schotty's Avatar
    Join Date
    Jul 2001
    Location
    Milwaukee, WI
    Posts
    5,760

    Re:Odd scanf() issue in C

    Thanks. Its different enough from C++ that I get stuck. Usually with scanf crap. I hate how scanf works.

Similar Threads

  1. ssh issue
    By pigpenstud01 in forum Redhat / Fedora
    Replies: 1
    Last Post: 05-02-2008, 09:42 PM
  2. Ogg to MP3 issue
    By Schotty in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 06-17-2005, 05:00 AM
  3. Setiathome Issue
    By GhostDawg in forum General Chat
    Replies: 2
    Last Post: 01-19-2004, 04:11 PM
  4. .\configure issue
    By mla in forum Linux - General Topics
    Replies: 5
    Last Post: 10-13-2003, 05:21 PM
  5. GLO IRC issue
    By babbing in forum General Chat
    Replies: 3
    Last Post: 11-14-2002, 08:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •