demo-frac-boolean - cin >>...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// Demo program for Boolean flags // This program inputs a fraction and tests whether it's valid before outputting // its decimal equaivalent. / #include <iostream.h> # int main() { int numerator; // numerator of a fraction int denominator; // denominator of a fraction bool denom_non_zero = false; // flag for whether denominator is nonzero, // intially assumed false cout << "Input the numerator and denominator of a fraction:\nnumerator: "; cin >> numerator; cout << "denominator: ";
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cin &gt;&gt; denominator; // input fraction denom_non_zero = denominator != 0; // test denominator // output decimal equivalent of fraction if(denom_non_zero) // valid case { cout &lt;&lt; &quot;Fraction is valid.&quot; &lt;&lt; endl; cout &lt;&lt; &quot;Decimal equivalent of &quot; &lt;&lt; numerator &lt;&lt; &quot;/&quot; &lt;&lt; denominator &lt;&lt; &quot; is &quot; &lt;&lt; double(numerator) / double(denominator) &lt;&lt; endl; } else // invalid case { cout &lt;&lt; &quot;Fraction was invalid! Can't have a zero denominator.&quot; &lt;&lt; endl; } return 0; }...
View Full Document

Ask a homework question - tutors are online