Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

How do you make an algorithm to validate if a floating point value is a floating point value in C?

OpenStudy (anonymous):

So far I've tried using isalpha(<variable name>) but it does not seem to work. Does this work for floating point values?

OpenStudy (anonymous):

try the math library, particularly the ceilf() and floorf() functions, or maybe even the modf() function

OpenStudy (anonymous):

http://ideone.com/q6FJu

OpenStudy (anonymous):

Is it possible to accomplish this using only the libraries string.h and stdio.h?

OpenStudy (anonymous):

So you are trying to test string representation of numbers?

OpenStudy (anonymous):

I think it is possible.

OpenStudy (anonymous):

let me try :-P I will only include <string.h> and <stdio.h>

OpenStudy (anonymous):

Well, I'm asking the user to input a floating point value, and check if the input is indeed a floating point.

OpenStudy (anonymous):

prompting the user? then it is very possible.

OpenStudy (anonymous):

Using scanf, more specifically.

OpenStudy (anonymous):

http://ideone.com/DpEMx The idea is to get input as a string and parse it to test if it is a float.

OpenStudy (anonymous):

I still think that using the math.h functions to test the float value itself is far better than using the string and stdio functions the way I just did :-P

OpenStudy (anonymous):

This is what I've done so far, but it doesn't seem to be working well. Basically, I'm using strchr to see if the string AmtInput contains the characters ".0123456789", and assigning variables for later use. Where am I going wrong? int i, FloatPoint; char AmtInput[99]; int main() { scanf("%s", AmtInput); getchar(); // Check if user input is a floating point value for (i = 0; i <= strlen(AmtInput); i++) { if (strchr(".0123456789", AmtInput[i]) != 0) { printf("Not floating pt\n"); // User input IS a floating point value FloatPoint = 1; i = strlen(AmtInput) + 1; } } }

OpenStudy (anonymous):

Well it may be easier, but the problem is as of now, I don't know much about functions outside string.h and stdio.h. I may just end up stabbing in the dark if I use other libraries.

OpenStudy (anonymous):

You won't be stabbing in the dark if you use anything out of the C standard library, though :-D Go ahead, try out the nice math library so that you can test it in one line instead of a complicated loop.

OpenStudy (anonymous):

try posting your code on a site like http://ideone.com with input to see how it works. Right now I think your code's logic is a bit messed up. in particular, the if block contains two different things, one saying the input is not a float and another saying your input is a float. What happens is the first character (which is always a digit or a decimal point whether it is a whole number or a fractional number) is passed to strchr, returning a non-null pointer and therefore executing the code in the ifblock, saying that your number is "Not floating pt"

OpenStudy (anonymous):

Yes. You can do it only using <string.h> and <stdio.h> header file. Try strtok() library function. I think it could help .

OpenStudy (anonymous):

Just think that, Floating point number can be divide into token.( By split ".") :D , I think you got my idea.

OpenStudy (anonymous):

or just do this #include <math.h> int is_float(double num) { return ceil(num) != floor(num) }

OpenStudy (anonymous):

I forgot to say that I don't need to check if it's a floating point per se; I just need the algorithm to recognize if it contains characters aside from . 0 1 2 3 4 5 6 7 8 9. Here's my take: AmtInput[99] FloatSymbols[11] = {".0123456789"} // Check if user input contains characters aside from ".0123456789" for (i = 0; i <= strlen(AmtInput); i++) { for (j = 0; j < 11; j++) { if (FloatSymbols[j] == AmtInput[i]) { FCount++; } } } // Check for validity of input if(FCount != strlen(AmtInput)) { MsgInvalidInput(); } There's more to my source code that's not relevant to the question, so I'm leaving them out. For my simple purposes, this works.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!