Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Fractions in C++

Reply
Thread Tools

Fractions in C++

 
 
farah727rash@gmail.com
Guest
Posts: n/a
 
      09-29-2006
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;
}

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      09-29-2006
On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++,
"(E-Mail Removed)" <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      09-29-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;


 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      09-29-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-29-2006
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-29-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-29-2006
Howard wrote:
> <(E-Mail Removed)> 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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-29-2006

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:efjkd0$6rj$(E-Mail Removed)...
> Howard wrote:
>> <(E-Mail Removed)> 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


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-29-2006
kwikius wrote:
....
> BTW You could check my Quan library for this stuff:
>
> http://quan.sourceforge.net/quan_mat...tml/index.html


Very cool.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-29-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Font with fractions JP Computer Support 3 02-08-2009 02:15 PM
XSL-FO...Fractions? Lee Jackson XML 3 12-17-2004 12:18 PM
fractions - entering your own numbers in Word? John Computer Information 3 04-29-2004 11:50 PM
Decimal places and huge fractions. Eoin Mcloughlin Perl 5 02-13-2004 08:33 PM
Adding two fractions karp C++ 2 11-22-2003 06:47 AM



Advertisments