What's wrong with my code (HELP)

This is a discussion on What's wrong with my code (HELP) within the C Programming forums, part of the General Programming Boards category; >>but isn't that a problem with scanf then? Yes, it is a problem with scanf. But it is not unique ...

  1. #16
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>but isn't that a problem with scanf then?
    Yes, it is a problem with scanf. But it is not unique to scanf. The same problem can occur with any input function that reads a single character and does not treat whitespace specially.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I disagree. It isn't a problem "with scanf", but rather with the way people use scanf. Most people don't use it correctly. When you tell scanf to read one character, it does. That's all. Nothing more. The fact remains that your input us two characters, and you aren't using it right.

    If you use scanf correctly, it can be used for input without problems. However, as it's been stated, most people don't use it right. It, scanf, is simply a tool. Most people don't understand how it works, so they get odd results from using this tool.

    In other words, it's not the tool's fault you aren't using it correctly. It's yours.

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

  3. #18
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Yes, you are right. I should have been more specific so as to avoid bashing the tool. However if the design of a function does not lend itself to being used correctly then it most certainly is a problem with the function in my opinion. scanf has too many subtle pitfalls for me to not consider it a problematic function, and many people seem to agree.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Princeton
    Yes, you are right. I should have been more specific so as to avoid bashing the tool. However if the design of a function does not lend itself to being used correctly then it most certainly is a problem with the function in my opinion. scanf has too many subtle pitfalls for me to not consider it a problematic function, and many people seem to agree.
    I agree, but everyone around here likes to post with "-pedantic", and I didn't want to feel left out.

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

  5. #20
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by quzah
    I disagree. It isn't a problem "with scanf", but rather with the way people use scanf. Most people don't use it correctly. When you tell scanf to read one character, it does. That's all. Nothing more. The fact remains that your input us two characters, and you aren't using it right.

    If you use scanf correctly, it can be used for input without problems. However, as it's been stated, most people don't use it right. It, scanf, is simply a tool. Most people don't understand how it works, so they get odd results from using this tool.

    In other words, it's not the tool's fault you aren't using it correctly. It's yours.

    Quzah.


    I think agree there. Many ppl use C today and forget that it was desgned quite some time ago, and the functions were not necessarily desgined to be used as one might think. Scanf is excellent for doing what its name implies scan formated input. So your previous statement is on point. if you use it as its meant to be used then there is no problem. Unfourtunetly until the way C is taught changes many ppl will still be using it for early input until they get to more "user freindly" functions, and it will continue to be an initiation period for n00bies in learning its subtleties. Not necessarily a bad expereince just hair pulling.
    @princton: I don't think scanf is flawed or was designed incorrectly. What we expect from it is incorrect, the majority of the time. Functions only do what you tell them to. its our fault if we expect other wise and use them unlike how they were meant to be.
    my $0.02

    cheers.

  6. #21
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    hi again. just to update, i found out a solution to my problem. all i had to do was add a space before the %c in the scanf statement.

    Code:
    scanf(" %c", &x);
    this makes the scanf read all the white space before it gets to the character. thanks for the help guys.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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