Fractions in C++

Discussion in 'C++' started by farah727rash@gmail.com, Sep 29, 2006.

  1. Guest

    Hi everyone, I have this problem and I don't know what's wrong with my
    program. I am trying to enter my two variables height and weight as
    fraction numbers. I declared them as float and also as double, but the
    program aborts when I input a fraction instead of decimal/integer
    number. Could someone tell me where the problem is, and what I need to
    do to correct my code? Thanks a lot in advance, Farah.


    #include <iostream>
    using namespace std;

    int main(void)
    {
    const double INCHES_PER_METER = 39.37;
    const double POUNDS_PER_KG = 2.24;

    long double height;
    long double weight;

    cout << "METRIC CONVERTER" << endl << endl ;
    cout << "Enter your height in inches " ;
    cin >> height;

    cout << "Enter your weight in pounds" ;
    cin >> weight;
    cout << endl ;

    double metric_height = height*INCHES_PER_METER;

    double metric_weight = weight/POUNDS_PER_KG;


    cout << "Your height is " << metric_height << " meters." <<
    endl;
    cout << "Your weight is " << metric_weight << " kilograms." <<
    endl;

    return 0;
    }
    , Sep 29, 2006
    #1
    1. Advertising

  2. David Harmon Guest

    On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
    "" <> wrote,
    > I am trying to enter my two variables height and weight as
    >fraction numbers. I declared them as float and also as double, but the
    >program aborts when I input a fraction instead of decimal/integer
    >number.


    Please tell us what book lied to you and told you that you could
    enter numbers as fractions?
    David Harmon, Sep 29, 2006
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi everyone, I have this problem and I don't know what's wrong with my
    > program. I am trying to enter my two variables height and weight as
    > fraction numbers. I declared them as float and also as double, but the
    > program aborts when I input a fraction instead of decimal/integer
    > number. Could someone tell me where the problem is, and what I need to
    > do to correct my code? Thanks a lot in advance, Farah.
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(void)
    > {
    > const double INCHES_PER_METER = 39.37;
    > const double POUNDS_PER_KG = 2.24;
    >
    > long double height;
    > long double weight;
    >
    > cout << "METRIC CONVERTER" << endl << endl ;
    > cout << "Enter your height in inches " ;
    > cin >> height;
    >
    > cout << "Enter your weight in pounds" ;
    > cin >> weight;
    > cout << endl ;
    >
    > double metric_height = height*INCHES_PER_METER;
    >
    > double metric_weight = weight/POUNDS_PER_KG;
    >
    >
    > cout << "Your height is " << metric_height << " meters." <<
    > endl;
    > cout << "Your weight is " << metric_weight << " kilograms." <<
    > endl;
    >
    > return 0;
    > }


    Fractions, such as 1/2 2/3 etc.. are not stored in doubles or floats or
    ints. They are actually formulas. The values you can store are 0.5 and
    0.6666 however.

    If you want to be able to put in fractions, such as
    12 1/2
    then you will need to parse the string and do the math yourself. If I
    wanted to do this I would look for a space in the string. If I found a
    space I would take everything before the space as the whole number. Then
    I'd look for the /. I would take everthing from the space to the / as the
    operator. Everything after the / as the devisior. Then simply:

    double Height = Whole + operator / devisor;
    Jim Langston, Sep 29, 2006
    #3
  4. kwikius Guest

    wrote:
    > Hi everyone, I have this problem and I don't know what's wrong with my
    > program. I am trying to enter my two variables height and weight as
    > fraction numbers. I declared them as float and also as double, but the
    > program aborts when I input a fraction instead of decimal/integer
    > number. Could someone tell me where the problem is, and what I need to
    > do to correct my code? Thanks a lot in advance, Farah.
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(void)
    > {
    > const double INCHES_PER_METER = 39.37;
    > const double POUNDS_PER_KG = 2.24;
    >
    > long double height;
    > long double weight;
    >
    > cout << "METRIC CONVERTER" << endl << endl ;
    > cout << "Enter your height in inches " ;
    > cin >> height;
    >
    > cout << "Enter your weight in pounds" ;
    > cin >> weight;
    > cout << endl ;
    >
    > double metric_height = height*INCHES_PER_METER;
    >
    > double metric_weight = weight/POUNDS_PER_KG;
    >
    >
    > cout << "Your height is " << metric_height << " meters." <<
    > endl;
    > cout << "Your weight is " << metric_weight << " kilograms." <<
    > endl;
    >
    > return 0;
    > }


    BTW You could check my Quan library for this stuff:

    http://quan.sourceforge.net/quan_matters/doc/html/index.html

    Tested on VC7.1, VC8.0 and gcc4

    In Quan the code might look like this:

    #include <quan/out/length.hpp>
    #include <quan/out/mass.hpp>
    #include <quan/fixed_quantity/io/input.hpp>

    int main(void)
    {
    std::cout << "METRIC CONVERTER"
    << std::endl << std::endl ;

    quan::length::in height;
    quan::mass::lb weight;

    std::cout << "Enter your height in inches " ;
    std::cin >> height.reference_numeric_value<
    quan::length::in
    >();


    std::cout << "Enter your weight in pounds" ;
    std::cin >> weight.reference_numeric_value<
    quan::mass::lb
    >();

    std::cout << std::endl ;

    quan::length::m metric_height = height;
    quan::mass::kg metric_weight = weight;

    std::cout << "Your height is "
    << metric_height << std::endl;

    std::cout << "Your weight is "
    << metric_weight << std::endl;
    }

    regards
    Andy Little
    kwikius, Sep 29, 2006
    #4
  5. osmium Guest

    <> wrote:

    > Hi everyone, I have this problem and I don't know what's wrong with my
    > program. I am trying to enter my two variables height and weight as
    > fraction numbers. I declared them as float and also as double, but the
    > program aborts when I input a fraction instead of decimal/integer
    > number. Could someone tell me where the problem is, and what I need to
    > do to correct my code? Thanks a lot in advance, Farah.
    >
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(void)
    > {
    > const double INCHES_PER_METER = 39.37;
    > const double POUNDS_PER_KG = 2.24;
    >
    > long double height;
    > long double weight;
    >
    > cout << "METRIC CONVERTER" << endl << endl ;
    > cout << "Enter your height in inches " ;
    > cin >> height;
    >
    > cout << "Enter your weight in pounds" ;
    > cin >> weight;
    > cout << endl ;
    >
    > double metric_height = height*INCHES_PER_METER;
    >
    > double metric_weight = weight/POUNDS_PER_KG;
    >
    >
    > cout << "Your height is " << metric_height << " meters." <<
    > endl;
    > cout << "Your weight is " << metric_weight << " kilograms." <<
    > endl;
    >
    > return 0;
    > }


    The easiest fix would be to change the first message to "Enter your height
    as a decimal number" Then make the corresponding change for weight. If
    you think this reduces the usability of the program (I don't) or your
    instructor requires fractional input, others have proposed solutions for
    that.
    osmium, Sep 29, 2006
    #5
  6. Howard Guest

    <> wrote in message
    news:...
    > Hi everyone, I have this problem and I don't know what's wrong with my
    > program. I am trying to enter my two variables height and weight as
    > fraction numbers. I declared them as float and also as double, but the
    > program aborts when I input a fraction instead of decimal/integer
    > number. Could someone tell me where the problem is, and what I need to
    > do to correct my code? Thanks a lot in advance, Farah.
    >


    How are you defining "fraction"? It sounds like you're referring to a float
    or a double, which are "real" types. A "fraction" is something of a form
    like "1/2", or "31/365". (A "mixed" number might be "50 3/4", where there
    is both a whole number part and a fractional part.) But a real number would
    be entered as something like "58.75". So, what are you really talking
    about?

    >
    > #include <iostream>
    > using namespace std;
    >
    > int main(void)
    > {
    > const double INCHES_PER_METER = 39.37;
    > const double POUNDS_PER_KG = 2.24;
    >
    > long double height;
    > long double weight;


    Those are integer types. How did you define them when you tried entering
    "fractions"? And what did you type that caused the program to abort?

    >
    > cout << "METRIC CONVERTER" << endl << endl ;
    > cout << "Enter your height in inches " ;
    > cin >> height;
    >
    > cout << "Enter your weight in pounds" ;
    > cin >> weight;
    > cout << endl ;
    >
    > double metric_height = height*INCHES_PER_METER;


    Hmmm. Better check your math. If I'm 60 inches tall, then this math says
    I'm over 200 meters tall! I don't think so....

    >
    > double metric_weight = weight/POUNDS_PER_KG;
    >
    >
    > cout << "Your height is " << metric_height << " meters." <<
    > endl;
    > cout << "Your weight is " << metric_weight << " kilograms." <<
    > endl;
    >
    > return 0;
    > }
    >


    -Howard
    Howard, Sep 29, 2006
    #6
  7. Howard wrote:
    > <> wrote in message
    >> [..]
    >> long double height;
    >> long double weight;

    >
    > Those are integer types. How did you define them when you tried
    > entering "fractions"? And what did you type that caused the program
    > to abort?


    No, the 'long double' type is a floating point type.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 29, 2006
    #7
  8. Howard Guest

    "Victor Bazarov" <> wrote in message
    news:efjkd0$6rj$...
    > Howard wrote:
    >> <> wrote in message
    >>> [..]
    >>> long double height;
    >>> long double weight;

    >>
    >> Those are integer types. How did you define them when you tried
    >> entering "fractions"? And what did you type that caused the program
    >> to abort?

    >
    > No, the 'long double' type is a floating point type.
    >


    D'oh! I saw the word "long", and stopped reading right there. Time for a
    break, I think.

    -Howard
    Howard, Sep 29, 2006
    #8
  9. Gianni Mariani, Sep 29, 2006
    #9
  10. Noah Roberts Guest

    kwikius wrote:

    > BTW You could check my Quan library for this stuff:
    >
    > http://quan.sourceforge.net/quan_matters/doc/html/index.html


    Can the developer easily define new units of a particular dimension?
    Can the user define new units (non-static) that work with your
    dimensional quantities?

    Because the answer seems to be no on both of those Quan doesn't work
    for my, or many other, needs. It also seems overly complex for the
    task.

    It is cute, I'll give it that. I liked the rational number metaprogram
    but then that actually comes from a different library that tries to do
    the same thing. On the other hand I think its coolness factor
    outweighs its usefulness.

    It is good for study but I'm still working on my own.
    Noah Roberts, Sep 30, 2006
    #10
  11. Guest

    Well, that's what I was confused on. This was my first C++ class, and
    we were told to make changes to the code so that we can enter fractions
    instead of decimal numbers. I guess since we are just starting to
    learn, I can simply ask the user for numerator and denominator
    separately. Thanks for your help, Farah.


    David Harmon wrote:
    > On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
    > "" <> wrote,
    > > I am trying to enter my two variables height and weight as
    > >fraction numbers. I declared them as float and also as double, but the
    > >program aborts when I input a fraction instead of decimal/integer
    > >number.

    >
    > Please tell us what book lied to you and told you that you could
    > enter numbers as fractions?
    , Sep 30, 2006
    #11
  12. Guest

    Thanks for the idea. That's what I would do, but this was our first
    class in C++. So, I wasn't expecting them to make us think about using
    if...else statements, and all that stuff. But, that's the only way, I
    guess.

    > <> wrote in message
    > news:...
    > > Hi everyone, I have this problem and I don't know what's wrong with my
    > > program. I am trying to enter my two variables height and weight as
    > > fraction numbers. I declared them as float and also as double, but the
    > > program aborts when I input a fraction instead of decimal/integer
    > > number. Could someone tell me where the problem is, and what I need to
    > > do to correct my code? Thanks a lot in advance, Farah.
    > >
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main(void)
    > > {
    > > const double INCHES_PER_METER = 39.37;
    > > const double POUNDS_PER_KG = 2.24;
    > >
    > > long double height;
    > > long double weight;
    > >
    > > cout << "METRIC CONVERTER" << endl << endl ;
    > > cout << "Enter your height in inches " ;
    > > cin >> height;
    > >
    > > cout << "Enter your weight in pounds" ;
    > > cin >> weight;
    > > cout << endl ;
    > >
    > > double metric_height = height*INCHES_PER_METER;
    > >
    > > double metric_weight = weight/POUNDS_PER_KG;
    > >
    > >
    > > cout << "Your height is " << metric_height << " meters." <<
    > > endl;
    > > cout << "Your weight is " << metric_weight << " kilograms." <<
    > > endl;
    > >
    > > return 0;
    > > }

    >
    > Fractions, such as 1/2 2/3 etc.. are not stored in doubles or floats or
    > ints. They are actually formulas. The values you can store are 0.5 and
    > 0.6666 however.
    >
    > If you want to be able to put in fractions, such as
    > 12 1/2
    > then you will need to parse the string and do the math yourself. If I
    > wanted to do this I would look for a space in the string. If I found a
    > space I would take everything before the space as the whole number. Then
    > I'd look for the /. I would take everthing from the space to the / as the
    > operator. Everything after the / as the devisior. Then simply:
    >
    > double Height = Whole + operator / devisor;
    , Sep 30, 2006
    #12
  13. Guest

    Hey, thanks for pointing that out. I never paid attention to the logic
    thing i.e., the equation.


    Howard wrote:
    > <> wrote in message
    > news:...
    > > Hi everyone, I have this problem and I don't know what's wrong with my
    > > program. I am trying to enter my two variables height and weight as
    > > fraction numbers. I declared them as float and also as double, but the
    > > program aborts when I input a fraction instead of decimal/integer
    > > number. Could someone tell me where the problem is, and what I need to
    > > do to correct my code? Thanks a lot in advance, Farah.
    > >

    >
    > How are you defining "fraction"? It sounds like you're referring to a float
    > or a double, which are "real" types. A "fraction" is something of a form
    > like "1/2", or "31/365". (A "mixed" number might be "50 3/4", where there
    > is both a whole number part and a fractional part.) But a real number would
    > be entered as something like "58.75". So, what are you really talking
    > about?
    >
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main(void)
    > > {
    > > const double INCHES_PER_METER = 39.37;
    > > const double POUNDS_PER_KG = 2.24;
    > >
    > > long double height;
    > > long double weight;

    >
    > Those are integer types. How did you define them when you tried entering
    > "fractions"? And what did you type that caused the program to abort?
    >
    > >
    > > cout << "METRIC CONVERTER" << endl << endl ;
    > > cout << "Enter your height in inches " ;
    > > cin >> height;
    > >
    > > cout << "Enter your weight in pounds" ;
    > > cin >> weight;
    > > cout << endl ;
    > >
    > > double metric_height = height*INCHES_PER_METER;

    >
    > Hmmm. Better check your math. If I'm 60 inches tall, then this math says
    > I'm over 200 meters tall! I don't think so....
    >
    > >
    > > double metric_weight = weight/POUNDS_PER_KG;
    > >
    > >
    > > cout << "Your height is " << metric_height << " meters." <<
    > > endl;
    > > cout << "Your weight is " << metric_weight << " kilograms." <<
    > > endl;
    > >
    > > return 0;
    > > }
    > >

    >
    > -Howard
    , Sep 30, 2006
    #13
  14. kwikius Guest

    kwikius, Sep 30, 2006
    #14
  15. kwikius Guest

    Noah Roberts wrote:
    > kwikius wrote:
    >
    > > BTW You could check my Quan library for this stuff:
    > >
    > > http://quan.sourceforge.net/quan_matters/doc/html/index.html

    >
    > Can the developer easily define new units of a particular dimension?


    It depends how you quantify easy. Its easy for me because its my
    library I guess, and I could document it better. Anyway heres one way:

    // header for si lengths etc with stream output
    #include <quan/out/length.hpp>
    #include <quan/out/area.hpp>

    // Create a custom conversion factor.
    // First rational is the exponent.
    // Second is the multiplier,
    // so the scaling factor to (e.g) meters
    // is 10^ exponent * multiplier.
    // Here we create a half a meter type
    typedef quan::meta::conversion_factor<
    quan::meta::rational<0>,
    quan::meta::rational<5,10>
    > half_si_unit;


    // Now create a fixed_quantity
    // of required dimension
    // with the half a meter conversion factor
    typedef quan::fixed_quantity<
    quan::meta::unit<
    quan::meta::components::
    of_length::abstract_quantity,
    half_si_unit
    >,

    double
    > half_a_meter;


    // half_a_meter is a non SI unit
    // so requires its own overload
    // if stream output is required...
    namespace quan{namespace meta{

    inline std::eek:stream& operator <<(
    std::eek:stream & os,
    unit<
    components::eek:f_length
    ::abstract_quantity,
    half_si_unit
    >

    )
    {
    return os << "half meter";
    }

    }}//quan::meta

    // now use ...
    int main()
    {
    // create a variable of
    // a Quan predefined length type
    // for comparison
    quan::length::m si_length(10);

    // create a variable of
    // user define half a meter length type...
    half_a_meter odd_length = si_length;

    // and use..
    std::cout << "SI length of: " << si_length
    << " = " << odd_length << ".\n";

    std::cout << "Ratio of SI length to odd length = "
    << si_length / odd_length << ".\n";

    // do some calcs...
    quan::area::m2 area = quan::pow<2>(odd_length);

    std::cout << "Area of a square of side " << odd_length
    << " = " << area << ".\n";

    }
    /*output:
    SI length of: 10 m = 20 half meter.
    Ratio of SI length to odd length = 1.
    Area of a square of side 20 half meter = 100 m2.
    */

    > Can the user define new units (non-static) that work with your
    > dimensional quantities?


    Currently I have only defined the fixed_quantity type where the units
    are fixed.
    The goal of fixed_quantity is to compete with double in performance.Its
    a competetion I dont think I will ever actually win in, but at least
    try to match them So far it seems to match doubles in a lot of cases ,
    except where UDT contants are used, in place of e.g ints. Whether I can
    eliminate that I don't know.

    I had planned two other types, one where the unit can be runtime
    modified and one where the unit and dimension could be, but so far I
    havent implemented those as I am currently figuring how to get matrices
    to work so that you can directly use physical quantities in matrix
    calcs.

    You can see the work in progress here. I'm using static doubles here ,
    but Quan quantities will work equally as well, as long as you get the
    dimensional analysis correct!

    http://tinyurl.com/qp56p

    The other thing I want is some graphics output to show off Quan, so I
    am working on that too.

    > Because the answer seems to be no on both of those Quan doesn't work
    > for my, or many other, needs. It also seems overly complex for the
    > task.


    Maybe, but it has a lot of functionality, and you will find that the
    subject is more complicated than you think. Much of the bulk is due to
    having predefined quantities:

    http://tinyurl.com/ox3aa

    There are a lot, but you only need to include the ones you need so they
    only take disk space unless you actually use them.

    > It is cute, I'll give it that. I liked the rational number metaprogram
    > but then that actually comes from a different library that tries to do
    > the same thing.


    Well, I'm not quite sure what you mean about 'different library'? The
    quan::meta::rational is very handy:

    http://tinyurl.com/olwfj

    It was originally written by Matthias Schabel for use in his mcsunits
    library, but it has had a lot of mods since. Is that what you mean?
    Last I heard he was quite keen on some of the Quan stuff and was
    considering moving his library to a similar approach, but you would
    need to ask him about that.

    > On the other hand I think its coolness factor
    > outweighs its usefulness.


    Well, at least its cool :). And of course I think its useful too. It
    makes coding a lot easier.

    > It is good for study but I'm still working on my own.


    Sure... Go for it :)

    regards
    Andy Little
    kwikius, Sep 30, 2006
    #15
  16. David Harmon Guest

    On 29 Sep 2006 17:59:03 -0700 in comp.lang.c++,
    "" <> wrote,
    >Well, that's what I was confused on. This was my first C++ class, and
    >we were told to make changes to the code so that we can enter fractions
    >instead of decimal numbers.


    Well then, I suggest reading the "calculator" example code running
    through several chapters of "The C++ programming language" by
    Stroustrup.
    David Harmon, Sep 30, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,612
    Eoin Mcloughlin
    Feb 13, 2004
  2. Lee Jackson

    XSL-FO...Fractions?

    Lee Jackson, Dec 17, 2004, in forum: XML
    Replies:
    3
    Views:
    692
    David Carlisle
    Dec 17, 2004
  3. karp

    Adding two fractions

    karp, Nov 22, 2003, in forum: C++
    Replies:
    2
    Views:
    5,242
    Gary Labowitz
    Nov 22, 2003
  4. =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

    How do I get the fractions of the visible part of a canvas?

    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Jul 2, 2003, in forum: Python
    Replies:
    1
    Views:
    312
    Eric Brunel
    Jul 2, 2003
  5. Matthew Wilson

    Need help with list of fractions

    Matthew Wilson, Oct 10, 2003, in forum: Python
    Replies:
    0
    Views:
    284
    Matthew Wilson
    Oct 10, 2003
Loading...

Share This Page