static_cast<int>(num) to get define decimal???

okay so I'm writing a program that defines a time instead of HH:MM it is formated as HH.MM. User of the program inputs a time in this format (IE: 1.30 for 1:30)
I need to give an error output if user inputs anything over 59 in the MM position. HH.MM is defined as a floating-point variable "num" How do I define the decimal/fractional part of this variable so I can output an error if MM > 59
You shouldn't use floating point to store integers. With floating point, MM may be > 59 even if the user entered 59
even if i stored it as an integer, how can i define MM?
ah, figured it out. wasn't too sure what the static_cast<int> command is for. Just used it to get the integer of "num" then subtract it from "num" to get the fractional part of "num"
static_cast<int> is used to convert to integer without compilers throwing loss of conversion warnings. (well it is in java so I assume it is in C++, so I hope I'm not misleading you)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using std::cout;

int main()
{

    int A=0;
   double B=6432.964;

   A = static_cast<int> (B);

   cout << A;
   return 0;
}


Last edited on
ya I got the general idea, only a beginner tho.
But thanks for your help.
output of the above: 6432
You still have loss of precision but receive no warning.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Wikipedia - Floating Point
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
yeah I got a bit of an inaccuracy defining it as a floating point variable, defined it as a double and voila.
Thanks for the help and the reading material.
Topic archived. No new replies allowed.