Thread: Why does program jump over first data input line?

  1. #16
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Hi Laserlight, at this point in time I have been messing about for a few days apparently because of stuff dangling about in the buffer. If I manage to overcome the problem at one point in the program, a new bug arises elsewhere. It is truly disheartening. Surely other people will have set up a menu driven application before and have encountered the wall I seem to be up against. Is it is impossible to write a menu driven program in C? In the past I have written in other languages and have never experienced bugs in the language like the ones I am up against now.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Quote Originally Posted by zach
    at this point in time I have been messing about for a few days apparently because of stuff dangling about in the buffer. If I manage to overcome the problem at one point in the program, a new bug arises elsewhere. It is truly disheartening.
    That's probably because you either didn't know how the input functions you were using worked, or you knew but didn't think clearly about how you were using them. As I said, the solution is simple: always read the entire line, then parse that line. You can use fgets with a large buffer (and hope a user doesn't try to break your program), or you might be able to use POSIX getline or write your own version with fgets in a loop, but either way when a user enters interactive input, they are effectively entering a line, so this solves your problem for good.

    Quote Originally Posted by zach
    Surely other people will have set up a menu driven application before and have encountered the wall I seem to be up against.
    Yes, you likely can find plenty of such problems over the years at this forum.

    Quote Originally Posted by zach
    Is it is impossible to write a menu driven program in C?
    Lol. It might be impossible for you right now, but it certainly is possible for me!

    Quote Originally Posted by zach
    In the past I have written in other languages and have never experienced bugs in the language like the ones I am up against now.
    While I think that there *are* bugs in the C programming language, in this case the bugs are in your program. You wrote the bugs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Feb 2019
    Posts
    500
    Quote Originally Posted by zach View Post
    Is it is impossible to write a menu driven program in C?
    Do you believe if I tell you it is possible to write entire opeating systems and GUIs in C?

  4. #19
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Isn't fflush(stdin); all I need?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Quote Originally Posted by zach View Post
    Isn't fflush(stdin); all I need?
    No, it results in undefined behaviour: the behaviour of fflush is only defined for output streams and update streams for which the most recent operation was not input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Thank you Laserlight. Well I obviously don't have the top down C knowledge that enables me to solve the problem I am facing. As far as I understand it, I am leaving stuff dangling that is causing problems and might even be combating one bug by means of a another bug ).

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by rstanley View Post
    The C Programming language was originally "designed" to create a usable version of UNIX, back in 1969/70.

    Once the language was standardized by ANSI through the C89 Standard, and by the ISO, in C90, C99, C11, C17, and future Standards, it is "designed" to be used on ALL O/S's. It is the most universal programming language of all time, BECAUSE of these Standards!

    There are quirks in all programming languages. It is up to the programmer to understand these quirks, and write code that can properly deal with these quirks.

    The issues with getchar(), scanf() and others, are not limited to one or two O/S's! They work the same on ALL O/S / Standards complaint compiler combinations.

    Also, resorting to (Windows based) conio.h functions as a crutch, is NOT the answer!
    And also non-hosted environments

  8. #23
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Re Laserlight. As a student progresses in time his domain of knowledge grows, but that domain in the beginning isn't large enough to solve the problems he encounters. Also when he searches for answers, he hardly understands the langue of the explanations. So now and then he needs some know how and! also some know why in plain English, otherwise he will remain stuck and might give up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program hangs when larger input data
    By 花颂 in forum C Programming
    Replies: 1
    Last Post: 04-03-2013, 09:55 PM
  2. Replies: 2
    Last Post: 03-31-2011, 04:50 PM
  3. reading line by line - input function
    By Lindira-123 in forum C Programming
    Replies: 11
    Last Post: 02-06-2011, 03:34 PM
  4. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  5. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 01:03 PM

Tags for this Thread