Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

loop condition statement

Hello. I am trying to write a loop that executes under the condition that one of my input variables is an integer. An example of what I am trying to do is

#include<iostream>
using namespace std;

int main()
{

float num;

cout << "Please enter a number: ";
cin >> num:

while(num is an integer)
{
.
.
.
}

return 0;
}

How would I code the "num is an integer" condition?

Thanks
[573 byte] By [PrimitiveProg] at [2007-11-11 10:25:03]
# 1 Re: loop condition statement
It's tricky an ddangerous. You could try something like this:

if (num== (int)num)
{
//..
}
The problem is that comparisons of floating point types are prone to hardware limitations, and as a result, they don't always work. You can read more about this problem here: http://www.dev-archive.com/cplus/10MinuteSolution/30891

One solution to this problem is to read put as a string and then check whether it has a decimal point followed by digits other than zero. It requires more work (and validations) but it's much safer.
Danny at 2007-11-11 20:58:50 >