Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. else does nto return expected value

else does nto return expected value

Scheduled Pinned Locked Moved C / C++ / MFC
c++beta-testingquestionlearning
18 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jharn
    wrote on last edited by
    #1

    Hello everyone, I have recently joined so I am a newbie. I only have taken one C++ course but am mostly self-taught by you wonderful people in the forums. I used forums to teach myself VBE so now I am using them to get better at C++. Just to be clear, my programming can be akin to a caveman putting a square peg in a round hole, i.e. it won't be pretty but eventually it will get there. Question I have is, with the following code I kind of expected a zero value to be passed to x when the else part and the if-else was triggered, but it returns an abnormally huge value. This is a .dll being called from VBE. Please forgive me if I have posted wrong, it is my first post. <pre></pre> #include <iostream> using namespace std; #include <cmath> double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else{ x=0; return(x); } } </pre>

    T M L 3 Replies Last reply
    0
    • J jharn

      Hello everyone, I have recently joined so I am a newbie. I only have taken one C++ course but am mostly self-taught by you wonderful people in the forums. I used forums to teach myself VBE so now I am using them to get better at C++. Just to be clear, my programming can be akin to a caveman putting a square peg in a round hole, i.e. it won't be pretty but eventually it will get there. Question I have is, with the following code I kind of expected a zero value to be passed to x when the else part and the if-else was triggered, but it returns an abnormally huge value. This is a .dll being called from VBE. Please forgive me if I have posted wrong, it is my first post. <pre></pre> #include <iostream> using namespace std; #include <cmath> double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else{ x=0; return(x); } } </pre>

      T Offline
      T Offline
      TheGreatAndPowerfulOz
      wrote on last edited by
      #2

      First, if it's returning a large value, then my suggestion as that 'p' is between 0.000611657 && 22.06495. On the other hand, how do you know that x was assigned '0'? Second, for programming clarity, may I suggest one of the following: (my preference is #2) 1. getting rid of the two returns and have only one return after the closing brace ({) of the else,

      if (a)
      {
      ...
      b = 1
      }
      else
      {
      b = 2
      }
      return b;

      2. OR, remove the else

      if (a)
      {
      b = 1;
      return b;
      }

      b = 2;
      return b;

      "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

      J 1 Reply Last reply
      0
      • J jharn

        Hello everyone, I have recently joined so I am a newbie. I only have taken one C++ course but am mostly self-taught by you wonderful people in the forums. I used forums to teach myself VBE so now I am using them to get better at C++. Just to be clear, my programming can be akin to a caveman putting a square peg in a round hole, i.e. it won't be pretty but eventually it will get there. Question I have is, with the following code I kind of expected a zero value to be passed to x when the else part and the if-else was triggered, but it returns an abnormally huge value. This is a .dll being called from VBE. Please forgive me if I have posted wrong, it is my first post. <pre></pre> #include <iostream> using namespace std; #include <cmath> double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else{ x=0; return(x); } } </pre>

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        (please reformat your code). Have you use the debugger to check the values and the code flow ?

        Watched code never compiles.

        J 1 Reply Last reply
        0
        • T TheGreatAndPowerfulOz

          First, if it's returning a large value, then my suggestion as that 'p' is between 0.000611657 && 22.06495. On the other hand, how do you know that x was assigned '0'? Second, for programming clarity, may I suggest one of the following: (my preference is #2) 1. getting rid of the two returns and have only one return after the closing brace ({) of the else,

          if (a)
          {
          ...
          b = 1
          }
          else
          {
          b = 2
          }
          return b;

          2. OR, remove the else

          if (a)
          {
          b = 1;
          return b;
          }

          b = 2;
          return b;

          "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

          J Offline
          J Offline
          jharn
          wrote on last edited by
          #4

          Thank you for your reply. Actually what the code computes is the steam saturation temperature given pressure. The abnormally large value comes in when the p is not between the 0.000611657 and 22.06495. I had no luck with your modification, I am assumin it is in the syntax but have had no luck thus far.

          T 2 Replies Last reply
          0
          • M Maximilien

            (please reformat your code). Have you use the debugger to check the values and the code flow ?

            Watched code never compiles.

            J Offline
            J Offline
            jharn
            wrote on last edited by
            #5

            When you say please reformat the code I am not sure what you are suggesting. When I try to run the debugger it says that it cannot open the .dll file that was created. The only way I can run it is to open up the excel file that references the .dll and plug in values for p that are either inside or outside of the range of the if statement and view the results.

            T C S 3 Replies Last reply
            0
            • J jharn

              Thank you for your reply. Actually what the code computes is the steam saturation temperature given pressure. The abnormally large value comes in when the p is not between the 0.000611657 and 22.06495. I had no luck with your modification, I am assumin it is in the syntax but have had no luck thus far.

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #6

              you posted

              if(p >= 0.000611657 && p <= 22.06495)

              which fixed up reads:

              if(p >= 0.000611657 && p <= 22.06495)

              so that means p would be between 0.000611657 && 22.06495 if you want p to be outside that range, then change it to read

              if(p < 0.000611657 || p > 22.06495)

              "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

              J 1 Reply Last reply
              0
              • J jharn

                Thank you for your reply. Actually what the code computes is the steam saturation temperature given pressure. The abnormally large value comes in when the p is not between the 0.000611657 and 22.06495. I had no luck with your modification, I am assumin it is in the syntax but have had no luck thus far.

                T Offline
                T Offline
                TheGreatAndPowerfulOz
                wrote on last edited by
                #7

                also, have you tried attaching the code to a debugger and stepping through to see what actually happens? if you can't do that, then try using some tracing statements to see what the values are and where the logic actually flows.

                "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                1 Reply Last reply
                0
                • T TheGreatAndPowerfulOz

                  you posted

                  if(p >= 0.000611657 && p <= 22.06495)

                  which fixed up reads:

                  if(p >= 0.000611657 && p <= 22.06495)

                  so that means p would be between 0.000611657 && 22.06495 if you want p to be outside that range, then change it to read

                  if(p < 0.000611657 || p > 22.06495)

                  "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                  J Offline
                  J Offline
                  jharn
                  wrote on last edited by
                  #8

                  I may not be clear on the code. p must be between these ranges for the calculation to execute, if it is outside of these ranges then the calculation does not apply and I want a 0 or a -1 returned.

                  T 1 Reply Last reply
                  0
                  • J jharn

                    I may not be clear on the code. p must be between these ranges for the calculation to execute, if it is outside of these ranges then the calculation does not apply and I want a 0 or a -1 returned.

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #9

                    ah ok, then my question still is, how do you know that x is being assigned 0? or that p is NOT in the desired range?

                    "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                    J 1 Reply Last reply
                    0
                    • J jharn

                      When you say please reformat the code I am not sure what you are suggesting. When I try to run the debugger it says that it cannot open the .dll file that was created. The only way I can run it is to open up the excel file that references the .dll and plug in values for p that are either inside or outside of the range of the if statement and view the results.

                      T Offline
                      T Offline
                      TheGreatAndPowerfulOz
                      wrote on last edited by
                      #10

                      attach the debugger to excel. and make sure the dll is loaded before the offending code is called (or call it at least once). then set a breakpoint in the offending code file. and cause excel to call the function.

                      "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                      1 Reply Last reply
                      0
                      • T TheGreatAndPowerfulOz

                        ah ok, then my question still is, how do you know that x is being assigned 0? or that p is NOT in the desired range?

                        "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                        J Offline
                        J Offline
                        jharn
                        wrote on last edited by
                        #11

                        Only in that from the Excel spreadsheet where I am entering the variable p into the function I can set it so that it is outside of the range of the if statement parameters. The value is returns when this is so is something like 1.7878e+308.

                        T 1 Reply Last reply
                        0
                        • J jharn

                          Only in that from the Excel spreadsheet where I am entering the variable p into the function I can set it so that it is outside of the range of the if statement parameters. The value is returns when this is so is something like 1.7878e+308.

                          T Offline
                          T Offline
                          TheGreatAndPowerfulOz
                          wrote on last edited by
                          #12

                          Try throwing in some MessageBox's so you can so the values in the method and the logic flow...

                          "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                          J 1 Reply Last reply
                          0
                          • T TheGreatAndPowerfulOz

                            Try throwing in some MessageBox's so you can so the values in the method and the logic flow...

                            "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                            J Offline
                            J Offline
                            jharn
                            wrote on last edited by
                            #13

                            I played with the code, this returns the values I need: double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else { x=-1; } return(x); }

                            T 1 Reply Last reply
                            0
                            • J jharn

                              I played with the code, this returns the values I need: double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else { x=-1; } return(x); }

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #14

                              congratulations! good for you!

                              "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                              J 1 Reply Last reply
                              0
                              • T TheGreatAndPowerfulOz

                                congratulations! good for you!

                                "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams

                                J Offline
                                J Offline
                                jharn
                                wrote on last edited by
                                #15

                                Thank you very much for your help.....in troubleshooting this I realize that I need to figure out how to operate the debugger. I can attach it to Excel and add the breakpoints, however it seems to do nothing. Again, thank you so much...

                                1 Reply Last reply
                                0
                                • J jharn

                                  When you say please reformat the code I am not sure what you are suggesting. When I try to run the debugger it says that it cannot open the .dll file that was created. The only way I can run it is to open up the excel file that references the .dll and plug in values for p that are either inside or outside of the range of the if statement and view the results.

                                  C Offline
                                  C Offline
                                  Cedric Moonen
                                  wrote on last edited by
                                  #16

                                  jharn wrote:

                                  When you say please reformat the code I am not sure what you are suggesting.

                                  Your code snippet is barely readable, since all special characters are escaped and the formatting is not kept. Check the forum guidelines (the message "How to ask a question" on top of this message board). The point nr 7 explains exactly what you have to do when you want to paste a code snippet.

                                  Cédric Moonen Software developer
                                  Charting control [v3.0] OpenGL game tutorial in C++

                                  1 Reply Last reply
                                  0
                                  • J jharn

                                    Hello everyone, I have recently joined so I am a newbie. I only have taken one C++ course but am mostly self-taught by you wonderful people in the forums. I used forums to teach myself VBE so now I am using them to get better at C++. Just to be clear, my programming can be akin to a caveman putting a square peg in a round hole, i.e. it won't be pretty but eventually it will get there. Question I have is, with the following code I kind of expected a zero value to be passed to x when the else part and the if-else was triggered, but it returns an abnormally huge value. This is a .dll being called from VBE. Please forgive me if I have posted wrong, it is my first post. <pre></pre> #include <iostream> using namespace std; #include <cmath> double __stdcall Ts_P(double &arg)//psia input, DegF output { double h_reg_1; double h_reg_2; double h_reg_3; double h_reg_4; double x; double vl_reg1[10] = {1167.0521452767, -724213.16703206, -17.073846940092, 12020.82470247,-3232555.0322333, 14.91510861353, -4823.2657361591, 405113.40542057, -0.23855557567849, 650.17534844798}; double p = (arg * 0.0068947572); //convert psia to MPa double beta = pow(p,0.25); if(p >= 0.000611657 && p <= 22.06495) { h_reg_1 = pow(beta,2) + vl_reg1[2] * beta + vl_reg1[5];//IFC 97 E term h_reg_2 = vl_reg1[0] * pow(beta,2) + vl_reg1[3] * beta + vl_reg1[6];//IFC 97 F term h_reg_3 = vl_reg1[1] * pow(beta,2) + vl_reg1[4] * beta + vl_reg1[7];//IFC 97 G term h_reg_4 = (2 * h_reg_3) / ((-1*h_reg_2) - (pow((pow(h_reg_2,2) - 4 * h_reg_1 * h_reg_3), 0.5)));//IFC 97 D term x = (((vl_reg1[9] + h_reg_4 - pow((pow((vl_reg1[9] + h_reg_4),2) - 4 * (vl_reg1[8] + vl_reg1[9] * h_reg_4)),0.5)) / 2) - 273.15) * (1.8) + 32; return(x); } else{ x=0; return(x); } } </pre>

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    jharn wrote:

                                    double p = (arg * 0.0068947572); //convert psia to MPa

                                    I think the above line should read:

                                    double p = (*arg * 0.0068947572); //convert psia to MPa

                                    I must get a clever new signature for 2011.

                                    1 Reply Last reply
                                    0
                                    • J jharn

                                      When you say please reformat the code I am not sure what you are suggesting. When I try to run the debugger it says that it cannot open the .dll file that was created. The only way I can run it is to open up the excel file that references the .dll and plug in values for p that are either inside or outside of the range of the if statement and view the results.

                                      S Offline
                                      S Offline
                                      Stefan_Lang
                                      wrote on last edited by
                                      #18

                                      I think what he means is that you should use the formatting that this forum provides, either by marking your code block and clicking on the beige-underlined formatting button titled code block, or by manually surrounding your code block with the tags <pre> and </pre>. It should then look something like this in your editor window:

                                      if (a) {
                                      b = 1;
                                      }
                                      else {
                                      b = 0;
                                      }

                                      (that last <pre> should have been </pre> instead actually, but that would have prematurely ended my example since I used the same tags to format it in the first place :) )

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups