A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 4 years ago
How do you make an algorithm to validate if a floating point value is a floating point value in C?
anonymous
 4 years ago
How do you make an algorithm to validate if a floating point value is a floating point value in C?

This Question is Closed

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0So far I've tried using isalpha(<variable name>) but it does not seem to work. Does this work for floating point values?

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0try the math library, particularly the ceilf() and floorf() functions, or maybe even the modf() function

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Is it possible to accomplish this using only the libraries string.h and stdio.h?

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0So you are trying to test string representation of numbers?

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0I think it is possible.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0let me try :P I will only include <string.h> and <stdio.h>

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Well, I'm asking the user to input a floating point value, and check if the input is indeed a floating point.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0prompting the user? then it is very possible.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Using scanf, more specifically.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0http://ideone.com/DpEMx The idea is to get input as a string and parse it to test if it is a float.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0I 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

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0This 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; } } }

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Well 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.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0You 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.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0try 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 nonnull pointer and therefore executing the code in the ifblock, saying that your number is "Not floating pt"

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Yes. You can do it only using <string.h> and <stdio.h> header file. Try strtok() library function. I think it could help .

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Just think that, Floating point number can be divide into token.( By split ".") :D , I think you got my idea.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0or just do this #include <math.h> int is_float(double num) { return ceil(num) != floor(num) }

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0I 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.