Thursday, July 9, 2009

C++ fraction program problem?

//This is the program of Dillon Nicholson


//it will use a class to calculate fractions


//addition subtraction multiplication and division








#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;


using namespace std;





struct fraction


{


float num;


float den;


};





class calculate


{


public::


fraction f1;


fraction f2;


char oper;


float fadd();


float fsub();


float fmul();


float fdiv();


int getdate();


double calc();








int main()


{


ofstream out;


out.open("output.txt");


fraction fra;


fraction fra2;


char oper, frac, frac2, ch;


float ans;





do {


cout %26lt;%26lt; "\nEnter the first fraction, then an operator(+,-,*,/), ";


cout %26lt;%26lt;"then your second fraction: \n\n";


cin %26gt;%26gt; fra.num %26gt;%26gt; frac %26gt;%26gt; fra.den %26gt;%26gt; oper %26gt;%26gt; fra2.num %26gt;%26gt; frac2 %26gt;%26gt; fra2.den;





switch (oper)


{


case '+': ans = fadd(fra.num,fra.den,fra2.num,fra2.den); break;


case '-': ans = fsub(fra.num,fra.den,fra2.num,fra2.den); break;


case '*': ans = fmul(fra.num,fra.den,fra2.num,fra2.den); break;

C++ fraction program problem?
I really am sorry, because this is so easy but I don't have the time to go through it all. As far as printing out the input numbers, thats easy. Right after you input the number with cin%26gt;%26gt;.... do this:





out%26lt;%26lt;fra.num%26lt;%26lt;frac%26lt;%26lt;fra.den; // 1st fraction





if you want the whole expression, take the cin%26gt;%26gt; and just put out%26lt;%26lt; with '%26lt;%26lt;' changed for '%26gt;%26gt;'.





out%26lt;%26lt;fra.num %26lt;%26lt;frac %26lt;%26lt;fra.den %26lt;%26lt;oper %26lt;%26lt;fra2.num %26lt;%26lt;frac2 %26lt;%26lt;fra2.den;








Don't try to write the entire program at once and then run it. You should be writing things step by step, and running the partial program to test. It makes it so much easier to know where the mistakes are, rather than having to go back through everything anytime it fails.
Reply:i don't know.


im dumb


No comments:

Post a Comment