demo-frac-boolean - cin>> denominator input fraction...

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 >> denominator; // input fraction denom_non_zero = denominator != 0; // test denominator // output decimal equivalent of fraction if(denom_non_zero) // valid case { cout << "Fraction is valid." << endl; cout << "Decimal equivalent of " << numerator << "/" << denominator << " is " << double(numerator) / double(denominator) << endl; } else // invalid case { cout << "Fraction was invalid! Can't have a zero denominator." << endl; } return 0; }...
View Full Document

This document was uploaded on 02/10/2011.

Ask a homework question - tutors are online