445,918 Members | 2,246 Online Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

# Fractions in C++

 P: n/a 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 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 Replies

 P: n/a On 28 Sep 2006 20:32:56 -0700 in comp.lang.c++, "fa**********@gmail.com"

 P: n/a 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

 P: n/a 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 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 #include #include 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

 P: n/a 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

 P: n/a #include 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

 P: n/a Howard wrote: [..] 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

 P: n/a "Victor Bazarov" >[..] long double height; long double weight; Those are integer types. How did you define them when you triedentering "fractions"? And what did you type that caused the programto 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

 P: n/a 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

 P: n/a 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

 P: n/a 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"

 P: n/a 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. 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

 P: n/a Hey, thanks for pointing that out. I never paid attention to the logic thing i.e., the equation. Howard wrote: 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

 P: n/a 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

 P: n/a 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 #include // 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

 P: n/a On 29 Sep 2006 17:59:03 -0700 in comp.lang.c++, "fa**********@gmail.com" Well, that's what I was confused on. This was my first C++ class, andwe were told to make changes to the code so that we can enter fractionsinstead 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 discussion thread is closed

Replies have been disabled for this discussion. 