Scanf is changing my results!

This is a discussion on Scanf is changing my results! within the C Programming forums, part of the General Programming Boards category; Could anyone offer any insight here please? I have a complete working program with the parameters for use hard-coded in. ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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,308
    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,438
    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,438
    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
    Katy, Texas
    Posts
    2,309
    I'll bite. I bet you are leaving a newline in the input buffer.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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, 09: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

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