Need help distiguishing between Integer and double, Please Help!

This is a discussion on Need help distiguishing between Integer and double, Please Help! within the C Programming forums, part of the General Programming Boards category; I am taking a class on c at my university and I am on my second project and I have ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Unhappy Need help distiguishing between Integer and double, Please Help!

    I am taking a class on c at my university and I am on my second project and I have reached a problem. I have to write a program with a menu and 6 different math functions and I cant use the math library. That part I did fine, but my professor wants us to distiguish between Interger and double. So if the user enters 2 it will return 2, but if the user enters 2.0 it will return 2.0. I am about to rip my hair out trying to figure this part out. Please help! I am using jgrasp to compile.

    Here is the closest i have got. It works when the user enters a double say 2.3 , it returns a 2.3 and if they enter a single iteger say 2 it returns 2 but it they enter 2.00 it returns 2. Help!

    Code:
    int main() {
    input = 0;
    input2 = 0;
    scanf("%d.%d", &input, &input2);
    if (input2 = 0){
         input = input2 + input
         prinf ("%d", input);
    }
    printf ("%d.%d", input, input2);
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Use fgets() to read the whole line into a buffer
    Use strchr() to see if there is a '.' in the input
    Use the appropriate sscanf() call to read an integer or a float, based on what you found

    See the FAQ.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    We have not been taught fgets(), or strchr() yet. I am not supposed to use the functions he has not gone over yet. Is there an alternative.

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Read input as a string.
    If there is a character ".", assume it's a float. Convert string to float.
    Else assume it's an integer. Convert string to integer.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    if (input2 = 0){
    = is for assignment, == for comparison. That will always be false. You want to use ==.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Maybe i didn't understand correctly, but if you want to distinguish integer and double based on user input i think this code might help you:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int x = 0;
        char buffer[10];
        int i;
        double xd = 0;
        scanf ( "%s", buffer );
        /* distinguish integers and doubles for further processing 
        and store them to appropriate variables */
        for (i = 0; buffer[i] != '.' && buffer[i] != '\0'; i++);
        if (buffer[i] == '\0')
        {
            sscanf (buffer, "%d", &x);
        }    
        else
        {
            sscanf (buffer, "%lf", &xd);
        }
        
        printf("%s", buffer); /*display exactly as entered*/
        return 0;
    }
    It will print as values as entered but it will distinguish integer input from double input.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    So you have to do a project yet use none of the functions that make it easier?

    Ok, I get it.

    Today's assignment is:
    - Print your name 10000 times at random places in a window in MS Windows.
    - You can't use the Win32 API, device contexts, or any other advanced text/graphic function.
    - The window cannot be a console window.
    - You are not allowed to use message loops.
    Last edited by VirtualAce; 06-12-2006 at 04:57 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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