Thread: Scanf is changing my results!

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Angry Scanf is changing my results!

    Could anyone offer any insight here please? I have a complete working program with the parameters for use hard-coded in. This program is 'encapuslated' if you will, within a control loop.

    yesterday i added a function call before the main loop starts so that the user can input the run parameters themselves. the function calls are in a switch block and the function body is written after the end of main.

    the function uses scanf to read five integer parameters into the program.

    I tested their values after the switch block and they are correct as inputted.

    However my problem is that now when i allow the program to go on and start its loop the output results are different than if i had left the values hard coded.

    I have tested this with known data and the results are well out,

    If i comment out the additional function, the results return to normal.

    I have heard that scanf is pants?? i am pretty certain the code is ok, so could it be my choice of input reading that is letting the side down.??

    As i say, once the program loop starts the values input are static. so i cant figure it out!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Without some example code, it's hard to say.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I have heard that scanf is pants?? i am pretty certain the code is ok, so could it be my choice of input reading that is letting the side down.??
    Yes, I'm certain the standard library, written by experts, reviewed by thousands, and tested by millions, is buggy, and your code must be bug-free.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm afraid you've spent all that time on a long description of what you think the problem is, rather than simply showing us the code and having us point out the real problem for you.

    Would you rather have a list of suggestions of what to look for to try and fix it yourself?:
    • Buffer overrun
    • Uninitialised variables
    • Wrong format specifier


    You could just as easily say that screwdrivers are "pants" if you happen to be using one that is 5x too small or 5x too big.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha, i consider myself suitably chastised, thanks for the help,i know it was lazy not putting the code in, anyway i noticed that i had saved these files as c++ instead of C, i will change that then ahve a proper look at the rest.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by rogster001 View Post
    Could anyone offer any insight here please? I have a complete working program with the parameters for use hard-coded in. This program is 'encapuslated' if you will, within a control loop.

    yesterday i added a function call before the main loop starts so that the user can input the run parameters themselves. the function calls are in a switch block and the function body is written after the end of main.

    the function uses scanf to read five integer parameters into the program.

    I tested their values after the switch block and they are correct as inputted.

    However my problem is that now when i allow the program to go on and start its loop the output results are different than if i had left the values hard coded.

    I have tested this with known data and the results are well out,

    If i comment out the additional function, the results return to normal.

    I have heard that scanf is pants?? i am pretty certain the code is ok, so could it be my choice of input reading that is letting the side down.??

    As i say, once the program loop starts the values input are static. so i cant figure it out!
    A code is worth more than a 1000 lines of description about code :-). Thats what i felt after reading through your post.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    is too true, but wot with my indentation and commenting being all over the show i was somewhat reluctant to post the code hee

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cyberfish View Post
    Yes, I'm certain the standard library, written by experts, reviewed by thousands, and tested by millions, is buggy, and your code must be bug-free.
    Indeed, and even without these unreliable hack libraries, most C compilers are known to sabotage beginner code for no good reason beyond the glaring and obvious design flaws of the compiler itself.

    If your program is really important to you, consider a less highly experimental, more mainstream and industry tested programming language.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'll bite. I bet you are leaving a newline in the input buffer.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  2. input row of integers via scanf into an array...
    By noodles355 in forum C Programming
    Replies: 1
    Last Post: 11-20-2006, 10:12 AM
  3. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM