Batting Average


Program Listing

BattingAvg.cpp

/****************************************************************************
*                                                                           *
*   Program:    BattingAvg                                                  *
*                                                                           *
*   Purpose:    This program will compute a player's batting average        *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Get the number of times at bat from the user

    cout << "Enter the number of at-bats:  " << flush;
    int AtBats;                         // Number of times at bat
    cin >> AtBats;

//  Get the number of hits from the user

    cout << "Enter the number of hits:  " << flush;
    int Hits;                           // Number of hits
    cin >> Hits;

//  Compute the batter's batting average and report the result

    float BatAvg = (float) Hits/AtBats; // Compute batting average
    cout << endl << "Your batting average is " << BatAvg << endl;
    
    return 0;
}

Sample Run

Enter the number of at-bats:  300
Enter the number of hits:  100

Your batting average is 0.333333



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:28 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/BattingAvg.html

Return to Coms 261 home page