Thread: I cant figure out why my simple program is not working!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tomwa
    On the concept of initialization, initializing a variable to an incorrect value is a typographical mistake or a logical one that can be debugged and the behavior will be the same each time
    If there is no correct initial value at that point, then there is no correct initialisation (or any initialisation, or no initialisation, is correct). The appropriate solution is to declare the variable near to first use, or in such cases, near to where they will be provided with an appropriate initial value. Just initialising the variable for the sake of initialisation will not cut it, and in fact can mislead the reader into thinking that the initialisation is appropriate.

    EDIT:
    Quote Originally Posted by Tomwa
    it'd be like if i did
    Consider this code:
    Code:
    int i = 0;
    for (i = 2; i < 10; ++i)
    {
        printf("%d\n", i);
    }
    Is the assignment of 2 to i a bug or was it intentional? If it was intentional, why was i initialised to 0 instead of 2? Now consider this code:
    Code:
    int i = 0;
    scanf("%d", &i);
    printf("%d\n", i);
    When run with certain user input, the program prints "2". Was the initialisation of i with 0 a bug or was it intentional? If it was intentional, why was i not used before the scanf?
    Last edited by laserlight; 01-06-2013 at 12:56 AM.
    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. Why is this simple c program not working ?
    By TexasKid in forum C Programming
    Replies: 5
    Last Post: 04-23-2012, 06:27 AM
  2. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 01:56 PM
  3. Simple program not working
    By oobootsy1 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2005, 02:20 AM
  4. can't figure out simple program
    By blight2c in forum C++ Programming
    Replies: 4
    Last Post: 03-16-2002, 05:17 PM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM

Tags for this Thread