Tuesday, July 28, 2009

In C++ How Do You...?

Work with The Number after the decimal point?





Example: 1.4 Hours is 1 Hour





4 * 6 =24


1 hour 24 minutes





I can get 1.4 to turn into 14 without a decimal point.


example





float NumA=1.4; Is the floating point number I'm working with.


float NumB=NumA * 10; Leaves me with 14


float NumC=NumB -10; Leaves me with 4, good








But I cant do that with 2.4 because you cant subtract 10.








float NumA=2.4; Is the floating point number I'm working with.


float NumB=NumA * 10; Leaves me with 24


float NumC=NumB -10; Leaves me with 14, bad.





Got any Ideas...?





Maybe an if statement, or for statement....





If the number the user enters is larger than 1.9, then what...?





Ok, well Ive gotten this far, and it was just basic Math...





Is there an operator or something that will calculate the numeral after the decimal point...?





Ive gotten pritty far, can you help me a little more please...





So far i've built programs to tell how many seconds are in a given time, cause you know how hard it is to add time...

In C++ How Do You...?
In 'C' or 'C++'


hours = (int) input


mins = (int) ((input - hours) * 60)


where input is double or float defined.
Reply:Already answered. There is no other way to overlaod + operator.
Reply:float decHours;


int hours;


int minutes;





input decHours;





hours = floor(decHours);


minutes = decHours % 60





You will probably have to include the math.h header here, I believe (it has been a little while).


floor() will give you the integer of a float, rounded down.


% will give you the integer remainder.


No comments:

Post a Comment