Fraction trouble

This is a discussion on Fraction trouble within the C Programming forums, part of the General Programming Boards category; Hey guys, i can't seem to find a way to store a fraction. if i ask for an input, and ...

  1. #1
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48

    Fraction trouble

    Hey guys, i can't seem to find a way to store a fraction. if i ask for an input, and they give me a fraction, then my program screws up. i'm kind of new to C, and i bet this is a really dumb question, but i've looked all over my books and can't find a thing. any help would be apreciated.

    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    scanf("%d/%d",&numerator,&denominator);

    Better yet, since error recovery is much easier
    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    if ( sscanf( buff, "%d/%d", &numerator, &denominator ) == 2 ) {
      // success
    } else {
      // oops
    }

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    By fraction, do you mean "3/4", or do you mean "102.233"?

    In C, you use "float" or "double" to store non-integer values. They're not exactly fractions, as some (many) fractions cannot actually be represented exactly in floating point variables.
    Insert obnoxious but pithy remark here

  4. #4
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    wow thanks guys! i think i got it all worked out! you rock

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 11:38 AM
  2. A tragedy!
    By Aakash Datt in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2003, 06:57 PM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 08:20 PM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 07:56 PM

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