| | |
Validating a double type variable to ensure it is an integer
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 1
Reputation:
Solved Threads: 0
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:
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.
C Syntax (Toggle Plain Text)
do { userinputfunction(x,y,z,etc...) } 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.
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) >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
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
If you want to win, you must not loose (Alan Ford)
•
•
•
•
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:
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.
I don't accept change; I don't deserve to live.
If you must use a double...for whatever reason...you can use
C Syntax (Toggle Plain Text)
if (loanYears - static_cast<int>(loanYears) != 0)
![]() |
Similar Threads
- define variable (C)
- how to cast a string type variable to LPCTSTR type (C++)
- variable to handle large integer (C)
- help on data type conversion (Visual Basic 4 / 5 / 6)
- adding totals in listboxes (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: File Pointer in C
- Next Thread: using strtok() to populate and array
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






