How to split a float into two variables? (c++)










0














I can't figure out how to take the total number of hours and split it into regular hours and overtime hours.



What I would like to achieve is to: add a module that splits the number of hours worked into regular hours and overtime hours. This module must have one ‘in’ parameter and two ‘out’ parameters.



What I could come up with was:



if (total_hours >= 0 || total_hours <= 40) 
reg_hours = total_hours;


if (total_hours >= 41 || total_hours <= 60)
ovt_hours = (total_hours - 40);



total_hours is my input. And reg_hours and ovt_hours are my outputs. If I put in 35.6 for total_hours, it'll compile and give me -4.40. It does fine with an input of 40 or higher, but when it comes to anything less than that it always produces a negative ovt_hours value. I'm really confused on how to stop it from giving me a negative number. I've been searching everywhere to find a solution, but I couldn't find anything that could help me understand what I am doing wrong. It would help me greatly if anyone could explain this in the simplest way possible.










share|improve this question























  • Your OR (||) should be AND (&&)...
    – Roger Lipscombe
    Nov 13 '18 at 8:24










  • I tried that but it gives me an even larger negative value
    – Zaeries
    Nov 13 '18 at 9:00










  • Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
    – Igor Tandetnik
    Nov 13 '18 at 15:01















0














I can't figure out how to take the total number of hours and split it into regular hours and overtime hours.



What I would like to achieve is to: add a module that splits the number of hours worked into regular hours and overtime hours. This module must have one ‘in’ parameter and two ‘out’ parameters.



What I could come up with was:



if (total_hours >= 0 || total_hours <= 40) 
reg_hours = total_hours;


if (total_hours >= 41 || total_hours <= 60)
ovt_hours = (total_hours - 40);



total_hours is my input. And reg_hours and ovt_hours are my outputs. If I put in 35.6 for total_hours, it'll compile and give me -4.40. It does fine with an input of 40 or higher, but when it comes to anything less than that it always produces a negative ovt_hours value. I'm really confused on how to stop it from giving me a negative number. I've been searching everywhere to find a solution, but I couldn't find anything that could help me understand what I am doing wrong. It would help me greatly if anyone could explain this in the simplest way possible.










share|improve this question























  • Your OR (||) should be AND (&&)...
    – Roger Lipscombe
    Nov 13 '18 at 8:24










  • I tried that but it gives me an even larger negative value
    – Zaeries
    Nov 13 '18 at 9:00










  • Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
    – Igor Tandetnik
    Nov 13 '18 at 15:01













0












0








0







I can't figure out how to take the total number of hours and split it into regular hours and overtime hours.



What I would like to achieve is to: add a module that splits the number of hours worked into regular hours and overtime hours. This module must have one ‘in’ parameter and two ‘out’ parameters.



What I could come up with was:



if (total_hours >= 0 || total_hours <= 40) 
reg_hours = total_hours;


if (total_hours >= 41 || total_hours <= 60)
ovt_hours = (total_hours - 40);



total_hours is my input. And reg_hours and ovt_hours are my outputs. If I put in 35.6 for total_hours, it'll compile and give me -4.40. It does fine with an input of 40 or higher, but when it comes to anything less than that it always produces a negative ovt_hours value. I'm really confused on how to stop it from giving me a negative number. I've been searching everywhere to find a solution, but I couldn't find anything that could help me understand what I am doing wrong. It would help me greatly if anyone could explain this in the simplest way possible.










share|improve this question















I can't figure out how to take the total number of hours and split it into regular hours and overtime hours.



What I would like to achieve is to: add a module that splits the number of hours worked into regular hours and overtime hours. This module must have one ‘in’ parameter and two ‘out’ parameters.



What I could come up with was:



if (total_hours >= 0 || total_hours <= 40) 
reg_hours = total_hours;


if (total_hours >= 41 || total_hours <= 60)
ovt_hours = (total_hours - 40);



total_hours is my input. And reg_hours and ovt_hours are my outputs. If I put in 35.6 for total_hours, it'll compile and give me -4.40. It does fine with an input of 40 or higher, but when it comes to anything less than that it always produces a negative ovt_hours value. I'm really confused on how to stop it from giving me a negative number. I've been searching everywhere to find a solution, but I couldn't find anything that could help me understand what I am doing wrong. It would help me greatly if anyone could explain this in the simplest way possible.







visual-c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 8:21







Zaeries

















asked Nov 13 '18 at 8:12









ZaeriesZaeries

63




63











  • Your OR (||) should be AND (&&)...
    – Roger Lipscombe
    Nov 13 '18 at 8:24










  • I tried that but it gives me an even larger negative value
    – Zaeries
    Nov 13 '18 at 9:00










  • Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
    – Igor Tandetnik
    Nov 13 '18 at 15:01
















  • Your OR (||) should be AND (&&)...
    – Roger Lipscombe
    Nov 13 '18 at 8:24










  • I tried that but it gives me an even larger negative value
    – Zaeries
    Nov 13 '18 at 9:00










  • Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
    – Igor Tandetnik
    Nov 13 '18 at 15:01















Your OR (||) should be AND (&&)...
– Roger Lipscombe
Nov 13 '18 at 8:24




Your OR (||) should be AND (&&)...
– Roger Lipscombe
Nov 13 '18 at 8:24












I tried that but it gives me an even larger negative value
– Zaeries
Nov 13 '18 at 9:00




I tried that but it gives me an even larger negative value
– Zaeries
Nov 13 '18 at 9:00












Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
– Igor Tandetnik
Nov 13 '18 at 15:01




Most likely, reg_hours and ovt_hours are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigning ovt_hours = (total_hours - 40) even when total_hours is less than 40 - hence the small negative number. When you change from || to &&, you sometimes don't assign at all, and then you see the initial garbage value, which may very well be a large negative number.
– Igor Tandetnik
Nov 13 '18 at 15:01












2 Answers
2






active

oldest

votes


















1














You can work up to 40 regular hours...



int reg_hours = max(total_hours, 40);


If you've worked more than 40 hours, then any extra hours are overtime hours...



int ovt_hours = 0;
if (total_hours > 40)
ovt_hours = total_hours - 40;






share|improve this answer




























    0














    There is one thing you should understand:
    Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).



    Also, you should notice that in your code there is a logic mistake:



    1. "if (total_hours >= 0 || total_hours <= 40)"

    2. "if (total_hours >= 41 || total_hours <= 60)"

    When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).



    Therefore you MUST use "else if" for your 2nd if statement.



    Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276521%2fhow-to-split-a-float-into-two-variables-c%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can work up to 40 regular hours...



      int reg_hours = max(total_hours, 40);


      If you've worked more than 40 hours, then any extra hours are overtime hours...



      int ovt_hours = 0;
      if (total_hours > 40)
      ovt_hours = total_hours - 40;






      share|improve this answer

























        1














        You can work up to 40 regular hours...



        int reg_hours = max(total_hours, 40);


        If you've worked more than 40 hours, then any extra hours are overtime hours...



        int ovt_hours = 0;
        if (total_hours > 40)
        ovt_hours = total_hours - 40;






        share|improve this answer























          1












          1








          1






          You can work up to 40 regular hours...



          int reg_hours = max(total_hours, 40);


          If you've worked more than 40 hours, then any extra hours are overtime hours...



          int ovt_hours = 0;
          if (total_hours > 40)
          ovt_hours = total_hours - 40;






          share|improve this answer












          You can work up to 40 regular hours...



          int reg_hours = max(total_hours, 40);


          If you've worked more than 40 hours, then any extra hours are overtime hours...



          int ovt_hours = 0;
          if (total_hours > 40)
          ovt_hours = total_hours - 40;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 19:19









          Roger LipscombeRoger Lipscombe

          55.7k42185313




          55.7k42185313























              0














              There is one thing you should understand:
              Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).



              Also, you should notice that in your code there is a logic mistake:



              1. "if (total_hours >= 0 || total_hours <= 40)"

              2. "if (total_hours >= 41 || total_hours <= 60)"

              When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).



              Therefore you MUST use "else if" for your 2nd if statement.



              Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.






              share|improve this answer

























                0














                There is one thing you should understand:
                Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).



                Also, you should notice that in your code there is a logic mistake:



                1. "if (total_hours >= 0 || total_hours <= 40)"

                2. "if (total_hours >= 41 || total_hours <= 60)"

                When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).



                Therefore you MUST use "else if" for your 2nd if statement.



                Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.






                share|improve this answer























                  0












                  0








                  0






                  There is one thing you should understand:
                  Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).



                  Also, you should notice that in your code there is a logic mistake:



                  1. "if (total_hours >= 0 || total_hours <= 40)"

                  2. "if (total_hours >= 41 || total_hours <= 60)"

                  When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).



                  Therefore you MUST use "else if" for your 2nd if statement.



                  Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.






                  share|improve this answer












                  There is one thing you should understand:
                  Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).



                  Also, you should notice that in your code there is a logic mistake:



                  1. "if (total_hours >= 0 || total_hours <= 40)"

                  2. "if (total_hours >= 41 || total_hours <= 60)"

                  When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).



                  Therefore you MUST use "else if" for your 2nd if statement.



                  Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 24 '18 at 1:55









                  LurimaLurima

                  11




                  11



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276521%2fhow-to-split-a-float-into-two-variables-c%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線