< previous page page_a59 next page >

Page A59
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
{
    return sqrt(side1*side1 + side2*side2);
}
13. Below, the type of costPerOunce and the function return type are float so that cost can be expressed in terms of dollars and cents (e.g., 1.23 means $1.23).
These types could be integer if cost is expressed in terms of cents only (e.g., 123 means $1.23).
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
float Postage( /* in */ int   pounds,
               /* in */ int   ounces,
               /* in */ float costPerOunce )

// Precondition:
//     pounds >= 0 && ounces >= 0 && costPerOunce >= 0.0
// Postcondition:
//     Function value == (pounds * 16 + ounces) * costPerOunce

{
    return (pounds * 16 + ounces) * costPerOunce;
}
Chapter 8
Case Study Follow-Up
3. Change the body of GetData so that it begins as follows:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
{
    Boolean badData;   // True if an input value is invalid

    badData = TRUE;
    while (badData)
    {
        cout << Enter the number of crew (1 or 2). << endl;
        cin >> crew;
        badData = (crew <CH:160>1 || crew > 2);
        if (badData)
            cout << Invalid number of crew members. << endl;
    }

    badData = TRUE;
    while (badData)
    {
        cout << Enter the number of passengers (0 through 8).
             << endl;
        cin >> passengers;
        badData = (passengers <CH:160>0 || passengers > 8);
        if (badData)
            cout << Invalid number of passengers. << endl;
    }

 
< previous page page_a59 next page >