Problem with my output

This is a discussion on Problem with my output within the C Programming forums, part of the General Programming Boards category; Okay. I am extremely new to C and am teaching myself ( with the help of all of you here ...

  1. #16
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Okay. I am extremely new to C and am teaching myself ( with the help of all of you here ). I will do some research about scanf so that I better understand what you mean and I will try to read up on getchar also.

    A little at a time. I'll get there.

    I have seen many people seem to suggest staying away from scanf and even read Elysia's FAQ devoted to it. I must admit it still isn't clear to me, but as I slowly gain more technical knowledge of the language I am sure it will all make sense. Is scanf pretty much obsolete? If not, then where is it appropriate?

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Dogmasur View Post
    Is scanf pretty much obsolete? If not, then where is it appropriate?
    It was mostly designed for formatted input that adheres to reasonably strict patterns. As far as free-form "people hammering on the keyboard" input goes, it does ok on numbers, not so ok with letters/strings, and doesn't handle errors at all (i.e., if you try to read a number, and the user types 'b', then the read will fail, and the 'b' will still be in the input buffer to annoy you in the future).

  3. #18
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by ssharish2005 View Post
    Code:
    printf("Enter a number greater than two:\t");
         scanf("%d", &n);
         getchar ();
    Perhaps, you should write a proper function to clear off the input buffer, but not just using the getchar function to clear it. Since there might be more than one char until you reach EOF char, which needs to be cleared. Use the following function to clear the input buffer.

    Code:
    void Clear_Buffer( void )
    {
         int ch;
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    And, try avoiding using scanf function.

    ssharish
    So, if I want the user to input some information and then wait for them to press the enter key and then want them to input more information again, I should clear the input buffer like you have shown above everytime after receiving input? I ask, because I am starting a new program dealing with Fibonacci sequences. It will ask the user to input four separate pieces of information. After each input, the value is stored in a variable. I should then clear the buffer as you have stated?

    Also, how should I appropriately ask for input? If scanf is truly only good for formatted input ( which, I think, is rarely to be expected to be inputted by the average user ) then I should use fgets? Or something else? If fgets, how is it used correctly?
    Last edited by Dogmasur; 08-07-2008 at 08:13 PM. Reason: more details and typos

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

Similar Threads

  1. Hash Table Problem.
    By penance in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 01:03 PM
  2. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  3. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 02:09 PM
  4. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 06:36 AM
  5. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 07:32 PM

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