![]() |
| ||
| a program about fractions Hey guys, I was wondering if you could help me fix my program. I'm witing a program in C++ that adds, subtracts, multiplies and divides fractions. I'm suppose to put in the cosntructor that the denominator cannot be 0 or a negative number and i also I have to reduce the fraction. I have no problem adding, subtracting , etc. When i run it saids that there's no appropriate default constructor available for x, a, and b. Well take a look at my code so, you can see what i mean. #include <iostream.h> |
| ||
| Re: a program about fractions Your only constructor takes two parameters. // ConstructorBut you instantiate some objects without supplying parameters. RationalNumbers x;1. You need to define a constructor that takes no parameters. // Constructor2. Or supply defaults for your constructor that takes two parameters. // Constructor3. Or instantiate your objects using your existing constructor. RationalNumbers x(0.0,1.0); |
| ||
| Re: a program about fractions Thank you, The Other Dave. I fixed my problem. Now is running perfectly. Now my program has a constructor that prevents a 0 denominator in a fraction and reduces fractions that are not in reduce form and avoids negative numbers. I overloaded the addition, multiplication, subtraction and division operators and the relational and equality operators too. Here is how it looks now. Feel free to give any comments. #include <iostream.h> I wanted to put a while loop so the program keeps asking the user to enter another fraction but i didn't know how to stop the loop. I wanted to do something like this: int main() |
| ||
| Re: a program about fractions >I wanted to put a while loop so the program keeps asking the user to enter another fraction but i didn't know how to stop the loop. Here is one way. int main()Something you should know about checking for floating point equality: http://www.eskimo.com/~scs/C-faq/q14.5.html You're not using anything from <math.h> The linter also tells me this. [Info 1739] Binary operator 'RationalNumbers::operator+(const RationalNumbers &) const' should be non-member functionI'm not exactly sure what it is trying to tell me, though. Here are the explanations of the diagnostic messages. Quote:
|
| ||
| Re: a program about fractions I've gotten to like a particular style, and I'm not pushing it here. Style can make code easier to read and thus easier to debug. Just for the sake of comparison, here is much the same code done with shorter names and more whitespace between operators. #include <iostream>I also shortened a few of the functions. And I moved the endl out of the extraction operator. But notice in your constructor how you are using initializer syntax to assign to the denominator, then in the body you check for greater than zero to see whether to assign it -- it may have already been assigned. Also, how do you "Try again" on a constructor? |
| ||
| Re: a program about fractions oops, I forgot to erase num( n ), den( d ) when i made the restrictions for the denominator. I didn't notice since it didn't give me a error message. Thanks for pointing that out. The Other Dave, your program doesn't read a fraction, it only reads two intergers. |
| ||
| Re: a program about fractions >The Other Dave, your program doesn't read a fraction, it only reads two intergers. Sure it does. I just chose to use whitespace as the delimiter instead of a '/'. Add in more code to ignore the '/' if you wish. |
| All times are GMT -4. The time now is 2:13 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC