Thursday, July 9, 2009

C++ program problems once again...?

#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;








class fraction


public :


{ int num;


int den;


fraction ();


fraction (int n, int d);


fraction add (fraction f2);


fraction mult (fraction f2);


bool equal (fraction f2);


};





fraction fraction:: add(fraction f2)


bool fraction:equal (fraction f2)


(num*f2.den)+(den*fraction)+(den*f2.de...


{fraction temp;


temp.num =(num*f2.den)+(den*f2.den);


temp.den = (den*f2.den)


return temp;


}


{


return =(num* f2.den== den*f2.num);


}





void main ()





{


ofstream out;


out.open("output.txt");


fraction f1 (1,3) , f2 (1,2, f3);


f3=f1.add(f2);


cout %26lt;%26lt; "the sum is "%26lt;%26lt;f3.num%26lt;%26lt;"/"%26lt;%26lt;f3.den%26lt;%26lt;endl;





if (f1.equal (f2)


cout %26lt;%26lt; "the fractions are equal"%26lt;%26lt;endl;


else cout %26lt;%26lt; "the fraction are not equal"%26lt;%26lt;endl;





int n, d;





cout %26lt;%26lt;"Imput the first fraction"%26lt;%26lt;endl;


cin%26gt;%26gt;n%26gt;%26gt;d;


fraction f1 (n,d)





out.close();


}





i need to get it to output the imputed fractions and the answers


and yet i cant figure out that code %26amp; it wont work neways

C++ program problems once again...?
Well, the first thing I noticed is that you've only defined two constructors, including the default and one that takes two int arguments, but you're declaring f2 with three arguments. I think you put a parenthesis in the wrong place.





You're missing a parenthesis in the initial decision structure:





if (f1.equal(f2))





You never actually use the output stream out.





You're missing semicolons on a number of lines.





All in all, I doubt this will compile. Is that what you mean when you say "it won't work"? Or is that just an artifact of transferring the code to YA? If you've gotten it to compile, it would help to know what the program does that you don't want it to do, or what it fails to do that you want it to.





Edit: Well, luv, the only suggestion I have is to fix the compile errors. I assume you're using something like VS or Borland or some other commercial package. You'd do well to read the error messages in some detail and learn exactly what they mean. I've noticed that beginners often just read them as "There's an error someplace" without learning your IDE's way of communicating exactly what error it found. If it's pointing out an error and you don't understand what it's saying, refer to the compiler documentation. With VS, at least, it's not unusal for the short error message to be cryptic at best. Usually the more detailed explanations in the compiler documentation are a lot more clear.





I'd also suggest that, initially at least, you stick to output to the default display with cout. Once you have the output to your liking, you can go back and add in writing the output to a file. When I was learning to handle file I/O, I found it useful to send the output to cout first. When it looked good, I added lines to send the output to a file. When what I got in the file looked like what I was getting on the screen, I went back and took out the cout lines.





Edit2: Jorge is right in that overloading the operators would give you much cleaner, easier code. However, I assumed you hadn't covered that as yet, as what you're doing is the usual dodge beginners use before they've learned the joys of overloading. Here, I'd suggest you continue with the code you have until you have it working. If you're supposed to overload things like the equality operator, you can add that in later on. Small steps are usually best for learning a language.





Edit3: Well, I was going to email you since these edits are getting tiresome, but you don't allow emails. I'm still not clear about whether you need to overload the operators or not, but my recommendation from earlier stands. Get it working with the functions you're using, THEN overload the operators. I think this is particularly important when you're trying to overload the insertion operator, %26lt;%26lt;. That one (and the extraction operator) are a bit harder to do than the others.





I'm also still unclear if your problems are compile errors, logic errors, or simple bugs. Feel free to email me to continue this discussion - preferable before we get a violation notice from Yahoo for "chatting".


No comments:

Post a Comment