Question
Input will be in a file, and output will be to a file.
input: The first input on a line is a person's ID, which consists of a letter followed by three digits. the ID for a female begins with E,F, or G (or e,f, g). The ID for a male begins with L, M, or N (or l, m, n). the ID for a female is followed by five decimal values representing in order, the body weight (in pounds) and the measurements (in inches) for the wrist, waist, hip and forearm. the ID for a male is followed by two decimal values representing body weight (pounds) and waist measurement (inches). spaces separate the input values. samples input line could be:
G428 130.0 6.0 25.0 37.5 9.5
n721 165.5 39.0
r129 140 7.0 28.0 40.0 10.0
M422 182.0 -23
if there is an error detected in the input, identify the error in a message in the output file, and end the run for that input. in the sample input, notice two kinds of input errors that can be detected.
output: is to a file and displays the ID, the weight and body fat percentage (as a percent), with appropriate labels.
note:
1. use ios::app when opening the output file
2. remember to include a header and comments
3. use indentation with, IF, IF...ELSE, and SWITCH statements, for ease in reading your source code
4. use braces, { and }, as needed to group a series of statements into one compound statement under an IF, IF...ELSE, or SWITCH
the formulas to determine the body fat for female and male are as follows:
body fat formula for women:
A1 = (body weight * 0.732) + 8.987
A2 = wrist measurement (at fullest point) / 3.140
A3 = waist measurement (at navel) * 0.157
A4 = hip measurement (at fullest point) * 0.249
A5 = forearm measurement (at fullest point) * 0.434
B = A1 + A2 + A3 + A4 + A5
body fat = body weight - B
body fat percentage = body fat * 100 / body weight
body fat formula for men:
A1 = (body weight * 1.082) + 94.42
A2 = wrist measurement * 4.15
B = A1 - A2
body fat = body weight - B
body fat percentage = body fat * 100 / body weight
Explanation / Answer
// Here you go. //You will need to create the Input.txt and Output.txt files in the main directory. #include #include #include using namespace std; double MaleCalculation(double Weight, double Waist) { double bodyFat; try { double temp1 = (Weight * 1.082) + 94.42; double temp2 = Waist * 4.15; double temp3 = temp1 - temp2; bodyFat = ((Weight - temp3) * 100) / Weight; } catch (exception e) { bodyFat = -1; } return bodyFat; } double FemaleCalculation(double Weight, double Waist, double Wrist, double Hip, double Forearm) { double bodyFat; try { double temp1 = (Weight * 0.732) + 8.987; double temp2 = Wrist / 3.140; double temp3 = Waist * 0.157; double temp4 = Hip * .249; double temp5 = Forearm * 0.434; double temp6 = temp1 + temp2 + temp3 + temp4 + temp5; bodyFat = ((Weight - temp6) * 100) / Weight; } catch (exception e) { bodyFat = -1; } return bodyFat; } int main() { ifstream read; ofstream write; string line; string errorMsg; string id; double weight; double waist; double wrist; double hip; double forearm; double bodyFat; bool isMale = false; read.open("Input.txt"); write.open("Output.txt", ios::app); while(!read.eof()) { //Every pass these are initialized as empty, 0, or invalid errorMsg= ""; weight = 0; waist = 0; wrist = 0; hip = 0; bodyFat = -1; forearm = 0; //Grab the first word read >> line; //if the line read is not blank and there hasn't been an error message previously, use the first character of the word to determine the sex. if(line.length() >= 1 && errorMsg == "") { id = line; switch(line[0]) { case 'L': isMale = true; break; case 'M': isMale = true; break; case 'N': isMale = true; break; case 'l': isMale = true; break; case 'm': isMale = true; break; case 'n': isMale = true; break; case 'E': isMale = false; break; case 'F': isMale = false; break; case 'G': isMale = false; break; case 'e': isMale = false; break; case 'f': isMale = false; break; case 'g': isMale = false; break; default: errorMsg = "Invalid Sex"; } //Once we have the sex successfully get the next word if(errorMsg == "") { //if it's a male, grab the two inputs and assign them to weight and waist if(isMale) { try { if(errorMsg=="") { read >> line; weight = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Weight Parameter"; } try { if(errorMsg=="") { read >> line; waist = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Waist Parameter"; } } //if it's a female, grab the five inputs and assign them if(!isMale) { try { if(errorMsg=="") { read >> line; weight = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Weight Parameter"; } try { if(errorMsg=="") { read >> line; wrist = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Wrist Parameter"; } try { if(errorMsg=="") { read >> line; waist = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Waist Parameter"; } try { if(errorMsg=="") { read >> line; hip = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Hip Parameter"; } try { if(errorMsg=="") { read >> line; forearm = atof(line.c_str()); } } catch (exception e) { errorMsg += " Missing Forearm Parameter"; } } } //Now calculate everything based on the sex. if(isMale && errorMsg == "") bodyFat = MaleCalculation(weight, waist); else { if (errorMsg == "") bodyFat = FemaleCalculation(weight, waist, wrist, hip, forearm); } if(bodyFat == -1) errorMsg = "Calculation Error"; if(errorMsg == "") write