Thread: what am I doing wrong?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    what am I doing wrong?

    I'm writing this program for yet another home work and I'm stuck on a very small part of it. I wrote a short separate program that includes my problem. I added the program and what I get when I run it.
    the problem is with the last number on the second line.
    instead of "1234567" I get "1247038" and I have no idea where that number comes from.
    if I replace the 'i' with a '0' in the end of the "x_i6=..." line then everything works fine, but if I put back the 'i' (which is 0 in the 1st time the "for" runs) then this specific number returns.
    and what's even stranger is if print the 'i' with printf before doing the calculations then it also works fine, even though I don't change anything.
    help anybody?
    Attached Images Attached Images what am I doing wrong?-c-problem-jpg what am I doing wrong?-check-program-jpg 

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem I have is you haven't said WHAT you're trying to do, and what algorithm you're trying to use to get that done. Posting code in plain text with code tags around it, is GREAT. Posting images of your code, is not.

    Declaring variables for your loop, inside the loop, is something I'm not familiar with. Best to declare your variables at the start of the function, or at least, outside a block of code - like a loop.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Declaring variables for your loop, inside the loop, is something I'm not familiar with. Best to declare your variables at the start of the function, or at least, outside a block of code - like a loop.
    Unless there is some efficiency advantage to be had by moving the declaration and initialisation to before the loop, declaring variables to be used only in the loop body in the loop body is fine: it follows the rule of thumb to declare variables near first use.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. what is wrong?
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 01-07-2007, 01:29 AM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM