Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculator for fractions using classes
    primarykey
    data
    text
    <p>I want to create a four functions (+,-,/,*) calculator for fractions using classes .There is something wrong with the getinf function, whenever i call it it always returns the same value in the main function. I cant figure out the what is causing this.</p> <pre><code>#include&lt;iostream&gt; #include&lt;conio.h&gt; #include&lt;cmath&gt; #include&lt;process.h&gt; using namespace std; class frac{ long num,dum; public: void getinf(){ cout&lt;&lt;"Enter the fraction = "; cin&gt;&gt;num&gt;&gt;dum; if(cin.fail()) { cout&lt;&lt;"Invalid data entered. "&lt;&lt;"\nTry again. ";getch();exit(0); } } frac add(frac f1,frac f2){ frac f3; f3.num=((f1.num*f2.dum)+(f1.dum*f2.dum)); f3.dum=f1.dum*f2.dum; return f3; } frac sub(frac f1,frac f2){ frac f3; f3.num=((f2.num*f2.dum)-(f1.dum*f2.dum)); f3.dum=f1.dum*f2.dum; return f3; } frac mul(frac f1,frac f2){ frac f3; f3.num=f1.num*f2.num; f3.dum=f1.dum*f2.dum; return f3; } frac div(frac f1,frac f2){ frac f3; f3.num=f1.num*f2.dum; f3.dum=f1.dum*f2.num; return f3; } void lowterm(){ long tnum,tdum,temp,gcd; tnum=labs(num); tdum=labs(dum); if(tdum==0) { cout&lt;&lt;"Illegal fraction division by 0";exit(1); } else if(tnum==0) { num=0;dum=1;return; } while(tnum!=0){ if(tnum&lt;tdum) { temp=tnum;tnum=tdum;tdum=temp; } tnum=tnum-tdum; } gcd=tdum; num=num/gcd; dum=dum/gcd; } void showinf(){ cout &lt;&lt;"The result is "&lt;&lt;num&lt;&lt;"/"&lt;&lt;dum;getch(); } }; void main(){ frac s1,s2,s3;char ch = 'a' ; s1.getinf(); s2.getinf(); cout&lt;&lt;"Enter the function you want to perform on the fractions (+,-,*,/): "; cin&gt;&gt;ch; if(cin.fail()){ cout&lt;&lt;"Invalid operation entered. "&lt;&lt;"\nNo such operation exist. " &lt;&lt;"\nTry again. "; getch(); exit(0); } switch(ch){ case'+':s3.add(s1,s2);break; case'-':s3.sub(s1,s2);break; case'*':s3.mul(s1,s2);break; case'/':s3.div(s1,s2);break; default:cout&lt;&lt;"Wrong information entered. "&lt;&lt;"\nTry again. "; } s3.lowterm(); s3.showinf(); getch(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload