How to split a float into two variables? (c++)
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++
add a comment |
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++
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
andovt_hours
are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigningovt_hours = (total_hours - 40)
even whentotal_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
add a comment |
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++
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++
visual-c++
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
andovt_hours
are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigningovt_hours = (total_hours - 40)
even whentotal_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
add a comment |
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
andovt_hours
are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigningovt_hours = (total_hours - 40)
even whentotal_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
add a comment |
2 Answers
2
active
oldest
votes
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;
add a comment |
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:
- "if (total_hours >= 0 || total_hours <= 40)"
- "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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 17 '18 at 19:19
Roger LipscombeRoger Lipscombe
56.1k43190314
56.1k43190314
add a comment |
add a comment |
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:
- "if (total_hours >= 0 || total_hours <= 40)"
- "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.
add a comment |
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:
- "if (total_hours >= 0 || total_hours <= 40)"
- "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.
add a comment |
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:
- "if (total_hours >= 0 || total_hours <= 40)"
- "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.
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:
- "if (total_hours >= 0 || total_hours <= 40)"
- "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.
answered Dec 24 '18 at 1:55
LurimaLurima
11
11
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
andovt_hours
are uninitialized, initially containing random garbage. With code as written, both conditions are always true, and you are assigningovt_hours = (total_hours - 40)
even whentotal_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