Fractions in C++

F

farah727rash

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;
}
 
D

David Harmon

On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
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?
 
J

Jim Langston

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;
 
K

kwikius

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
 
O

osmium

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.
 
H

Howard

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
 
V

Victor Bazarov

Howard said:
[..]
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
 
H

Howard

Victor Bazarov said:
Howard said:
[..]
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
 
N

Noah Roberts

kwikius said:
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.
 
F

farah727rash

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.
 
F

farah727rash

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.
 
F

farah727rash

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

kwikius

Noah said:
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 said:
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
 
D

David Harmon

On 29 Sep 2006 17:59:03 -0700 in comp.lang.c++,
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top