Thread: Output for a "new low" then not again during a loop

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    Output for a "new low" then not again during a loop

    I am creating a while loop that runs for two values of i and j. If the value of j is a new low, I output 20 million dollars. I'm running into a problem because the loop can bring j back up and then back to that same number and I don't know how to stop it from outputting 20 million again.

    Thank you guys.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    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

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    Code:
    // Determine new credit rating via inverse transform.
             U = MTUniform (0);
             // This can be made more efficient!!
             int jmin;
             jmin = (i >= 1 ? i-1 : 0);
             for (j = jmin; j <= 8; j++) {
                if (U <= P[i][j]) {
                   break;
                }
                if {j<i}
                payout+=20,000,000
             }
    It's a pretty convoluted code. I am supposed to add a scenario where the bond rating drops to a new low and the agency pays out 20,000,000. I know that when j < i, we've that the bond rating when down. But the loop can run again twice and go from j>i back to j < i and we're at the same rating, but it went down so the program wants to payout 20million

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your code snippet certainly is not compilable.

    The solution sounds simple: you could use a flag, or maybe you could check for some value. I am afraid that I can only be vague because you only have a vague code snippet to examine.
    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. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    Vague is actually what I want because the solution is complicated numerically so it requires me to know how to do this. How would I use a "flag"? Could you give an example?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Connor Joseph
    How would I use a "flag"? Could you give an example?
    A flag might work like this, in pseudocode:
    Code:
    payout_flag = false
    if (whatever && !payout_flag)
    {
        make payout
        payout_flag = true
    }
    However, thinking more carefully about your problem, this probably doesn't apply. You would probably use the "check some value" solution, in this case perhaps the lowest value, i.e.,
    Code:
    if (new low < lowest value)
    {
        make payout
        lowest value = new low
    }
    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: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  3. Problem with ">", "<" in a for loop
    By Isolda_ in forum C Programming
    Replies: 6
    Last Post: 09-11-2007, 01:56 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread