Beginner question-scanf being weird

This is a discussion on Beginner question-scanf being weird within the C Programming forums, part of the General Programming Boards category; I am new to C, and am having a lot of trouble with the scanf function. I am using it ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Beginner question-scanf being weird

    I am new to C, and am having a lot of trouble with the scanf function.
    I am using it to read a single character like this:

    scanf("%c",&character);

    this works fine in the main function, but in any other function, it always skips this line. it executes the line above and after it, without prompting the user for input. Can anyone tell me what might be happening here?
    thx.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What happens when you type a letter? You press enter. So that's actually two characters you've entered, right? Well, you'll need to take care of that enter key there in your read. Or better yet, never use scanf, and avoid this sort of thing. (Read: FAQ )


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    I too had the same problem

    I used to get really frustrated with scanf because the formatting it provides is nice, but when used with getch (posibly for some flow control), it is hectic, and as a beginner I am still slow to catch on to this kind of thing. Perhaps it is better to implement the functionality that scanf provides with functions I design myself. Otherwise, working with cin in C++ (although I know I'm in a C-programming forum) also works out pretty nicely.

  4. #4
    Registered User vddking2's Avatar
    Join Date
    Nov 2006
    Location
    Vietnam
    Posts
    12
    I think in the other function, you don't use "*" to make change.

    Example:

    Code:
    void change(char*character)
    {
              printf("Add a character: "); scanf("%c",&character);
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vddking2
    I think in the other function, you don't use "*" to make change.

    Example:

    Code:
    void change(char*character)
    {
              printf("Add a character: "); scanf("%c",&character);
    }
    The above code, while a seperate issue as my understanding of the OP's issue goes, is incorrect. It would be:
    Code:
    void change( char*character )
    {
              printf("Add a character: "); scanf("%c",character);
    }
    The & would be omitted in this case, since character is already a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Thanks- the problem was, as quzah said, that i did not realize enter was read also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. beginner question
    By k1ll3r in forum C++ Programming
    Replies: 12
    Last Post: 05-21-2006, 01:40 PM
  3. scanf question
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 06:22 AM
  4. Question about dialogs... weird problem
    By Green Fuze in forum Windows Programming
    Replies: 5
    Last Post: 03-06-2005, 09:04 AM
  5. A question about loops and scanf
    By mzkhadir in forum C Programming
    Replies: 3
    Last Post: 10-04-2001, 10:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21