classes/ftest5.cpp

The following code example is taken from the book
Object-Oriented Programming in C++
by Nicolai M. Josuttis, Wiley, 2002
© Copyright Nicolai M. Josuttis 2002


// include standard header files
#include <iostream>
#include <cstdlib>

// include header files for the classes that are being used
#include "frac.hpp"

int main()
{
    const CPPBook::Fraction a(7,3);      // declare fraction constant a
    CPPBook::Fraction x;                 // declare fraction variable x

    // new: output fraction a with stream operator
    std::cout << a << std::endl;

    // new: read fraction x
    std::cout << "enter fraction (numer/denom): ";
    if (! (std::cin >> x)) {
        // input error: exit program with error status
        std::cerr << "Error during input of fraction" << std::endl;
        return EXIT_FAILURE;
    }
    std::cout << "Input was: " << x << std::endl;

    // as long as x is less than 1000
    while (x < CPPBook::Fraction(1000)) {
        // multiply x by a
        x = x * a;
        // new: output with stream operator
        std::cout << x << std::endl;
    }
}