having a problem with types

This is a discussion on having a problem with types within the C Programming forums, part of the General Programming Boards category; I'm looking for the operator input to be differentiated from integer and float. Here is what I have so far ...

  1. #1
    Donn1
    Guest

    having a problem with types

    I'm looking for the operator input to be differentiated from integer and float. Here is what I have so far (which doesn't seem to work)

    #include<stdio.h>
    #include<stdlib.h>

    int num1, num2, num3;
    int main (void)
    {
    printf("Input a number:");
    scanf("%d",&num1);

    num2=(num1)/2; /* outputs an integer number if float is entered */
    num3=num2*2; /* should not equal num1 if float number was entered */

    if (num3==num1)
    printf("\nThis is an integer");
    else
    printf("\nThis is a float");


    return 0;
    }

    Please someone tell me where I'm going wrong...I appreciate the help.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int num1, num2, num3;

    You need to declare num1 and num3 as float or double:
    float num1, num3;

  3. #3
    Donn1
    Guest
    I tried that, but I get the answer "float" all the time. It won't discern the integer numbers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    The best this code will tell you is whether a number is even or odd.

    > scanf("%d",&num1);
    %d will stop scanning at a decimal point, so an input of
    100.3
    or
    100
    will both result in num1 being 100

    > I'm looking for the operator input to be differentiated from integer and float.
    Then you need to read the input as a string, and check that
    - it contains only digits and perhaps a decimal point
    - if there is a '.', then its a float, otherwise its an int
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You could also read it in always as a float, and check to see if the fractional part is zero.

    E.g.:

    double number;
    scanf("%lg",&number);
    if (fmodl(number,1.0) != 0) printf("Float!\n");
    else printf("Int!\n");

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    BTW, fmodl is not ANSI C. fmod, however, is.

    So this will work, using single-precision floats:

    float number;
    scanf("%g",&number);
    if (fmod(number,1.0) != 0) printf("Float!\n");
    else printf("Int!\n");

    This works a little better than simply scanning for deciamls, because:

    2.00, 1.02e4, etc. are actually ints (represented as floating point values). This code will allow you to know if the actual value is an integer or not. If you want to know if the representation is a floating point one (i.e. you want "2" to return int, but "2.0" to return float) you'll need to scan for the decimal point as Salem said.
    Last edited by The V.; 10-31-2001 at 03:58 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    int main (void)
    {
    float num1, num2; 
    printf("Input a number:");
    scanf("%f",&num1);
    num2=(int)(num1*10.)/10;
    if (num2==num1)
       printf("\nThis is an integer"); 
    else 
       printf("\nThis is a float"); 
    return 0;
    }
    Last edited by swoopy; 10-31-2001 at 04:13 PM.

  8. #8
    Donn1
    Guest
    Hey! It works! I understand the use of the cast in the math expression, but what I don't understand is why would you multiply by "10."? (i.e. (int)(num1*10.)/10)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'll try to explain.
    Any real number (float) is always written in base 10. This is the reason for multiplying by 10, instead of 2. We don't really care how the real number is represented internally. So for example:

    5.3 * 10 = 53
    53 / 10 = 5.3
    (int) 5.3 = 5

    5 * 10 = 50
    50 / 10 = 5
    (int) 5 = 5

    This is similar to the nonfunction method of finding the remainder(modulus):
    Using integer arithmetic:
    remainder = number - (number/divisor*divisor)
    remainder = 34 - (34/10*10) = 34 - 30 = 4
    remainder = 34 - (34/2*2) = 0

    Your code looked very similar to this.

    The fmodl() solution posted by The V. is a good way to do it. I generally use library functions when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21