Thread: Finding mix & max of a calculated variable from an input file

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,173
    Simple tip until you're better at C++ such that you can ignore it and know exactly what went wrong: always use braces, even when the body of the if statement etc is only one statement. If you always do this, when the body of the if statement should be two statements, you won't make the mistake of having it apply to only the first statement.
    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

  2. #17
    Registered User
    Join Date
    May 2019
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Simple tip until you're better at C++ such that you can ignore it and know exactly what went wrong: always use braces, even when the body of the if statement etc is only one statement. If you always do this, when the body of the if statement should be two statements, you won't make the mistake of having it apply to only the first statement.
    Thank you laserlight. Simple tip for me to figure out but such an obvious mistake I discovered in my code... it works now excellently. One last question; I initialized my flag variable to be equal to 0 (false). Inside the loop, after everything executes; I set flag = 1 (true). The program only works correctly if I set flag = 1 at the end of the loop; why do I have to set my bool to true for it to work correctly?

  3. #18
    Registered User
    Join Date
    May 2019
    Posts
    209
    Code:
     
            if (flag = 0)
    
            max = index;
    
            min = index;
    This only initializes max conditionally. min will be set to index every time. Put the two in brackets

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,173
    Quote Originally Posted by underpressure View Post
    Thank you laserlight. Simple tip for me to figure out but such an obvious mistake I discovered in my code... it works now excellently. One last question; I initialized my flag variable to be equal to 0 (false). Inside the loop, after everything executes; I set flag = 1 (true). The program only works correctly if I set flag = 1 at the end of the loop; why do I have to set my bool to true for it to work correctly?
    That's why you need to give the flag a descriptive name: what's the flag for, what does true/false for the flag denote? Once you figure that out, it will become obvious why you need to set it somewhere in the loop after you have set the initial values for max and min.
    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

  5. #20
    Registered User
    Join Date
    May 2019
    Posts
    23
    Quote Originally Posted by laserlight View Post
    That's why you need to give the flag a descriptive name: what's the flag for, what does true/false for the flag denote? Once you figure that out, it will become obvious why you need to set it somewhere in the loop after you have set the initial values for max and min.
    Excellet advice again. I totally get it now. Thank you for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-14-2015, 10:05 PM
  2. Renaming a file with a variable as the input name
    By cherryduck in forum C Programming
    Replies: 6
    Last Post: 11-08-2009, 01:00 PM
  3. Finding values in input file
    By ToxicLove in forum C Programming
    Replies: 3
    Last Post: 05-02-2004, 06:19 PM
  4. Replies: 3
    Last Post: 06-25-2003, 04:29 PM
  5. finding the type of a input variable
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 08:40 AM

Tags for this Thread