3

I want to check if the user input is purely integer or a float. I attempted to do this by using floor and ceilfand comparing the values to the original x value in a function. However, this seems to be a tad bit problematic as the function keeps returning 0 instead of 1 for certain numbers like 5.5, when floor(5.5)!=5.5 and ceilf(5.5)!=5.5. This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x)//in a separate file
{

    if (floor(x)==x && ceilf(x)==x)
    {
        //printf("%f",floor(x));
        return 0;
    }
    else {
                return 1;
    }

}
int main()
{
    char line[] = " +----+----+----+----+----+----+----+----+----+----+---+";
    char numbers[] = " 0    5    10   15   20   25   30   35   40   45   50";
    float balls,slots;
    int slot[9];

    printf("==========================================================\nGalton Box Simulation Machine\n==========================================================\n");
    printf("Enter the number of balls [5-100]: ");
    scanf("%f",& balls);
    if (balls>100 || balls<5){
        printf("/nInput is not within the range. Please try again.");
    }

    else if (intchecker(balls)==1){
        printf("/nInput is not an integer. Please try again.");
    }
    else {
            printf("    This is an integer.");
            //some more code here
    }

}

I tried placing just the intchecker code in another project, which seems to work fine without any bugs, unlike in the previous project, where when I used a printf statement to check if the floor(x) value was correct, it kept showing different answers, e.g. "-2.000000" when input was 5.2. This is my code for the second project:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
    float  x;
    scanf("%f",&x);

    if (floor(x)==x && ceilf(x)==x){
        printf("Integer");
        return 0;
    }
    else {
        printf("Non-Integer");
        return 1;
    }


}

How is it possible that the second code works perfectly when the first code does not? Is something wrong with my way of writing/calling the function?(I am relatively new to functions-only 2 weeks of exposure so far)

I searched online and have seen many answers to check if input is integer or float, even on stackoverflow.com itself, but my wish is not to find out other ways to check if input is integer or float (for if I wished to do that, I could just google it, and there are many such questions on stackoverflow.com as well), but to comprehend why my first code does not work, for, as far as I know, it ought to work well without any of the bugs it is currently facing.

Any help is greatly appreciated!:)

11
  • If you require an integer value then why do you scan a float from the input? Also either floorf(x) == x or ceilf(x) == x is sufficient; you don't need both. Commented Sep 9, 2015 at 0:57
  • "I used a printf statement to check if the floor(x) value was correct, it kept showing different answers, e.g. "-2.000000" when input was 5.2." You need to show the exact code that exhibits that behavior, because nothing in the code you posted would do that. My best guess is that your real code has a buffer overrun bug, or other undefined behavior. Commented Sep 9, 2015 at 1:08
  • are you giving the exact same input to both programs? scanf("%f",&myfloat) of "5" may actually read in 5.0000000000000001, and "4" may be exactly 4.0. it all depends on the implementation of scanf(). Commented Sep 9, 2015 at 1:18
  • 1
    Code interestingly uses ceilf(float) and floor(double), yet not floorf(float). I suspect FLT_EVAL_METHOD is 1 or 2 and the main() code was optimized to double. Does the difference occur if you code with floorf()? What is printf("%d\n", FLT_EVAL_METHOD);? Commented Sep 9, 2015 at 1:37
  • 1
    So does main.c have a prototype of int intchecker(float x);? If not, then that is likely the issue. Commented Sep 9, 2015 at 1:40

1 Answer 1

4

Assuming a missing function declaration:

main.c is missing the prototype for int intchecker(float x) so main.c assumes the old-school prototype of int intchecker(int x) and the code exhibits undefined behavior. Anything could happen.

Add prototype in main.c or put it in separate.h and include that header file here and in separate.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>

int intchecker(float x);

int main(void) {
   ...
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. The code works perfectly fine now after adding the prototype. I am not that familiar with functions and hence assumed that separate files would be read first automatically without any need for declaration. It seems like that was the error after all.
The header .h, containing int intchecker(float x); is commonly included in the .c implementation and all usage .c files. The .h also contains defines, typdefs, global variable declarations. All sorts of things that by themselves do not generate any code. Like <math.h>, you will find lots of these things but no implementations. Good luck on the journey.
@JaimeAlcaraz you should have received a compiler warning at least - pay attention to those messages!
Does it mean that if the prototype is not included, the code in the function is not read at all?
@JaimeAlcaraz the code in the function is not "read" by main.c. All main.c "reads" is in main.c and the various included files like stdio.h and separate.h. All separate.c "reads" is in separate.c and its various included files like math.h and separate.h. These 2 compilations (which could have occurs days apart) are then linked together as a final step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.