473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Fractions in C++

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 '06 #1
15 14580
On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
"fa**********@gmail.com" <fa**********@gmail.comwrote,
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 29 '06 #2
<fa**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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 29 '06 #3

fa**********@gmail.com 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_mat...tml/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

Sep 29 '06 #4
<fa**********@gmail.comwrote:
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.
Sep 29 '06 #5

<fa**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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 29 '06 #6
Howard wrote:
<fa**********@gmail.comwrote 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
Sep 29 '06 #7

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ef**********@news.datemas.de...
Howard wrote:
><fa**********@gmail.comwrote 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
Sep 29 '06 #8
kwikius wrote:
....
BTW You could check my Quan library for this stuff:

http://quan.sourceforge.net/quan_mat...tml/index.html
Very cool.
Sep 29 '06 #9

kwikius wrote:
BTW You could check my Quan library for this stuff:

http://quan.sourceforge.net/quan_mat...tml/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.

Sep 29 '06 #10
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++,
"fa**********@gmail.com" <fa**********@gmail.comwrote,
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 '06 #11
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.
<fa**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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 '06 #12
Hey, thanks for pointing that out. I never paid attention to the logic
thing i.e., the equation.
Howard wrote:
<fa**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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 '06 #13

Gianni Mariani wrote:
kwikius wrote:
...
BTW You could check my Quan library for this stuff:

http://quan.sourceforge.net/quan_mat...tml/index.html

Very cool.
Thanks :-)

regards
Andy Little

Sep 30 '06 #14
Noah Roberts wrote:
kwikius wrote:
BTW You could check my Quan library for this stuff:

http://quan.sourceforge.net/quan_mat...tml/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::ostream& operator <<(
std::ostream & os,
unit<
components::of_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

Sep 30 '06 #15
On 29 Sep 2006 17:59:03 -0700 in comp.lang.c++,
"fa**********@gmail.com" <fa**********@gmail.comwrote,
>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.

Sep 30 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: skip | last post by:
I'm looking for a solution (or ideas about a solution) to the problem that strftime(3) and strptime(3) don't understand time increments of less than one second. Most operating systems can provide...
2
by: karp | last post by:
Lets say I have the following class in order to add two fractions, class Fraction{ public: Fractions::Fraction(int numer = 0, int denom = 1) { valeurNumerateur = numer; valeurDenominateur =...
33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
2
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). ...
2
by: Just Me | last post by:
I need a numerical updown control that displays fractions. If the increment is 1/16 it would display 1/16, 1/32, 3/16, 1/4... I thought I might inherit from a NumericalUpDown control and place...
4
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
5
by: gubbachchi | last post by:
Hi all, How to convert the fractions into decimals and vice versa in php. I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.