943,600 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2795
  • C RSS
Sep 18th, 2006
1

Validating a double type variable to ensure it is an integer

Expand Post »
I have to use a variable of type double to store and manipulate a value for an amount of years. This means I have to ensure that the data input by the user is an integer. The program will accept a non-integer value, but I want it to continue reprompting the user when a non-integer is entered. I thought I had this solved, but it does not work for some reason. Here is the code:

  1. do
  2. {
  3. userinputfunction(x,y,z,etc...)
  4. } while((loanYears <= 1) && (loanYears >= 100) && ((floor(loanYears)) != loanYears));

However, the do ... while loop does not work as planned! Everything but the last part works. [i.e. - ((floor(loanYears)) != loanYears))] The last part is supposed to validate the users input as an integer.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
VinceColeman is offline Offline
1 posts
since Sep 2006
Sep 18th, 2006
1

Re: Validating a double type variable to ensure it is an integer

floating point numbers always have a degree of imprecision. A floating point value will never be equal to a whole number. You should check to see if it is within an acceptable epsilon from a whole number (or fractional value, if such a situation were to arise).
(loanYears <= floor(loanYears) + epsilon)
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 18th, 2006
1

Re: Validating a double type variable to ensure it is an integer

>The program will accept a non-integer value, but I want it to continue reprompting the user when a non-integer is entered.
You can use fgets and then parse trought the string checking is it number or not in a do...while loop. But you need to change the logic of input
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 18th, 2006
1

Re: Validating a double type variable to ensure it is an integer

I have to use a variable of type double to store and manipulate a value for an amount of years. This means I have to ensure that the data input by the user is an integer. The program will accept a non-integer value, but I want it to continue reprompting the user when a non-integer is entered. I thought I had this solved, but it does not work for some reason. Here is the code:
The first step in solving the problem is to properly understand the problem domain and the prog requirements. IF you just want to accept and manipulate a value for amout of years why not just use unsigned long instead of going to the trouble of using a floating point variable which always ditches the programmer when it comes to comparing it with some other floating number.

Do a second check on you logic, as suggested by Mr. Andor. It would definately solve your problem. Otherwise if you still want to stick to floating point types then you have to follow the advice given by Mr. Infarction which involves deciding on a tolerance value or the deviation value by which your two floating point numbers can differ.

HOpe it helped, bye.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 18th, 2006
2

Re: Validating a double type variable to ensure it is an integer

If you must use a double...for whatever reason...you can use

  1. if (loanYears - static_cast<int>(loanYears) != 0)
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: File Pointer in C
Next Thread in C Forum Timeline: using strtok() to populate and array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC