I'm writing a program and suppose to have a int as in input, but if someone enters a float, I want it to test if it is a float and then tell the user to re-enter the value, how do I do that?
I'm writing a program and suppose to have a int as in input, but if someone enters a float, I want it to test if it is a float and then tell the user to re-enter the value, how do I do that?
Read it as a string. Use strchr to see if it has a decimal point.Originally posted by UnknownImage
I'm writing a program and suppose to have a int as in input, but if someone enters a float, I want it to test if it is a float and then tell the user to re-enter the value, how do I do that?
Quzah.
Hope is the first step on the road to disappointment.
Is there any other ways to test if a number is a floating point besides reading it into a string?
Hmmm, well let's see, you can use scanf() and check the return value. Also, atof(fgets()) is effective, but won't tell you if alpha chars screwed up the input, in which case you just get 0.0. You can also read it into a buffer one by one, just try out different things...Originally posted by UnknownImage
Is there any other ways to test if a number is a floating point besides reading it into a string?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
On a side note, we generally like to keep the same exact topic in the same thread. You'll want to keep that in mind for future reference.
Quzah.
Hope is the first step on the road to disappointment.
:-)
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int is_float(char *src, size_t len) { double s_val; char *end; s_val = strtod(src, &end); if (s_val == 0 || s_val == HUGE_VAL) { return 0; } if (end < src + len) { return 0; } return 1; } int main(void) { char *p; char string[20]; printf("Enter a number: "); if (fgets(string, sizeof(string), stdin) == 0) { return 1; } if ((p = strrchr(string, '\n')) != 0) { *p = '\0'; } if (is_float(string, strlen(string))) { printf("It is floating point\n"); } return 0; }
*Cela*
Dude, you're not forgetting string is a reserved name, are you?! Please don't use it
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>>Dude, you're not forgetting string is a reserved name, are you?!
Since when? :-) The ANSI standard says only that function names beginning with str and a lower case letter are reserved, it also stands to reason that other global names would be too, but local variables are out of the implementation's juristiction :-) Unless we're looking at different parts of the standard, I'm looking at section 7.26.11 of ISO/IEC 9899:1999, so you don't have to look, it says this :-)
I'm probably missing another section and you're right, if you could point it out to me so I don't make the same mistake again that'd be great :-)7.26.11 String handling <string.h>
1 Function names that begin with str, mem, or wcs and a lowercase letter may be added
to the declarations in the <string.h> header.
*Cela*
Strictly speaking, I believe you are correct...... but, to help clarify: from here:
Personally, I agree with the last sentence of the quote, it's a bad idea to use a name that is sooooo close to being reserved On a board like this, it's too easy for a newbie to take example code and mangle it into something that appears to work, but quickly breaks the rules. For example, they take your well written example code, and try to expand it, and in the process they make the string variable global. oops!ext: always reserved for use with external linkage (7.1.3/4.1.2.1). This means that, even if you don't include the indicated header file, you still must never create global variables or non-static functions with these names. Strictly speaking, you can create local variables, static functions, typedefs, or macros with those names (depending on which headers you include, and other circumstances, you may need to #undef them first), but it's a bad idea because even if the compiler gets it right you'll confuse any humans who look at your program.
>>I'm probably missing another section and you're right
No more sections, and lets not debate too long eh!
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
That's a good point, I'll remember it :-) But first, one nitpick about that quote
>>This means that, even if you don't include the indicated header file
The ANSI standard says that they're reserved no matter what headers are included
>>and lets not debate too long eh!All external names described below are reserved no matter what headers are included by the program.
Agreed :-)
*Cela*
[nitpicks]
If you're going to check for HUGE_VAL, you might as well check for -HUGE_VAL too.
Providing a value of "0.0" to is_float will return 0.
[/nitpicks]
>>If you're going to check for HUGE_VAL, you might as well check for -HUGE_VAL too.
Good point, but now that I think about it there's no reason to check for HUGE_VAL at all, since errno is set for both underflow and overflow I can just check that for ERANGE and then I don't have to worry about unsafe floating point comparisons. Thanks for catching that :-)
I also realized that I don't even have to test, or even have the s_val variable since all of my testing can be done more safely and thoroughly with just src, len, end, and errno :-)
Code:#include <stdlib.h> #include <errno.h> int is_float(char *src, size_t len) { char *end; errno = 0; strtod(src, &end); if (errno == ERANGE) /* Under/Overflow */ { return 0; } if (end == src || end < src + len) /* No conversion/left over string */ { return 0; } return 1; }
*Cela*