// John Bent's CS 110 Hand-out 2 // Blood Alcohol Content calculator, Improved // // a simple c++ program to calculate BAC using Widmark's formula // program limitations - can only calculate for one proof of // liquor at a time - (i.e. cannot calculate BAC for a person // who has drank more than one proof of liquor and no error checking for user input #include //allows use of cout & cin #include //allows input & output of external files #include //allows use of setprecision // function prototypes float get_BAC (char, float, float, int, float); // FUNCTION DEFINITION // RETURN TYPE IS FLOAT -- TAKES FIVE PARAMETERS // this function passes by value void get_user_input(char&, int&, float&, float&, float&); // FUNCTION DEFINITION // RETURN TYPE IS VOID -- TAKES FIVE REFERENCE PARAMETERS // this function passes by reference //declare global variables const float MALE_RATE = 0.68, FEMALE_RATE = 0.55; //portion of body weight which holds alcohol //usually constants are defined like this char filename[] = "BAC_data.dat"; // file which holds numerical data int main () { //variables to be input and modified char gender; int weight; float oz_drunk, proof, time_span; // start processing code now get_user_input(gender, weight, oz_drunk, proof, time_span); // FUNCTION CALL // the program can now output the result cout << endl << endl << endl; // skip some spaces cout << "Your BAC after drinking " << oz_drunk << " ounces of " << proof << " proof liquor over a " << time_span << " hour span is: " << endl << endl << " : " << setprecision(3) // show BAC to only 3 decimal places << get_BAC (gender, oz_drunk, proof, weight, time_span) // FUNCTION CALL << " BAC." <> blood_gravity >> metabolism >> oz_to_pounds; // read values from infile if (gen == 'm' || gen == 'M') widmark_rate = MALE_RATE; else widmark_rate = FEMALE_RATE; bac = (ounces * pr * oz_to_pounds * blood_gravity) / (w * widmark_rate) - (time * metabolism); if (bac < 0) return 0; else return bac; } void get_user_input (char& gen, int& w, float& oz_consumed, float& pr, float& time) // FUNCTION HEADER { // get info from the user now cout << "Are you a an or a oman?" << endl << " :"; cin >> gen; cout << "\nWhat is your weight in pounds?" << endl << " :"; cin >> w; cout << "\nHow many ounces of alcohol did you drink?" << endl << " :"; cin >> oz_consumed; cout << "\nWhat was the proof of the alcohol?" << endl << " :"; cin >> pr; cout << "\nHow many hours have passed since your first drink?" << endl << " :"; cin >> time; }