Assign4 and lab4
This commit is contained in:
parent
4a1c1376a0
commit
22cf3e8401
@ -1,31 +1,70 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
// Declare Variables
|
// Declare Variables
|
||||||
|
// Float values for calculations
|
||||||
float bmi, weight, height, mostellar, duBois, boyd, lifeExpect;
|
float bmi, weight, height, mostellar, duBois, boyd, lifeExpect;
|
||||||
char gender[15];
|
|
||||||
|
// Int values to store the result of the scanf fucntion
|
||||||
|
int weightResult, heightResult;
|
||||||
|
|
||||||
|
// Char to store the gender with max length of 7 and a line string for formatting
|
||||||
|
char gender[7];
|
||||||
char line[] = "********************************";
|
char line[] = "********************************";
|
||||||
|
|
||||||
// Display Program Purpose
|
// Display Program Purpose
|
||||||
printf("%s%s%s\n", line, line, line);
|
printf("%s%s%s*\n", line, line, line);
|
||||||
printf("This program will calculate your BMI, Life Expectancy, and BSA(mulitple methods)\nwith just your weight(kg), height(cm), and gender.\n");
|
printf("*\tThis program will calculate your BMI, Life Expectancy, and BSA(mulitple methods)\t*\n");
|
||||||
printf("%s%s%s\n", line, line, line);
|
printf("*\twith just your weight(kg), height(cm), and gender.\t\t\t\t\t*\n");
|
||||||
|
printf("%s%s%s*\n", line, line, line);
|
||||||
|
|
||||||
// Read in weight, height, and gender from user
|
// Read in weight, height, and gender from user
|
||||||
printf("Please enter your weight(kg): ");
|
printf("Please enter your weight(kg): ");
|
||||||
scanf("%f", &weight);
|
weightResult = scanf("%f", &weight);
|
||||||
|
while (weight <= 0 || weightResult != 1) {
|
||||||
|
while (getchar() != '\n');
|
||||||
|
if (weight <= 0) {
|
||||||
|
printf("Bad Value: Weight must greater than 0! fmt: XX.X\n");
|
||||||
|
} else {
|
||||||
|
printf("Bad Value: Please enter a decimal input! fmt: XX.X\n");
|
||||||
|
}
|
||||||
|
printf("Please re-enter your weight(kg): ");
|
||||||
|
weightResult = scanf("%f", &weight);
|
||||||
|
}
|
||||||
|
|
||||||
printf("Please enter your height(cm): ");
|
printf("Please enter your height(cm): ");
|
||||||
scanf("%f", &height);
|
heightResult = scanf("%f", &height);
|
||||||
|
while (height <= 0 || heightResult != 1) {
|
||||||
|
while (getchar() != '\n');
|
||||||
|
if (height <= 0) {
|
||||||
|
printf("Bad Value: Height must greater than 0! fmt: XX.X\n");
|
||||||
|
} else {
|
||||||
|
printf("Bad Value: Please enter a decimal input! fmt: XX.X\n");
|
||||||
|
}
|
||||||
|
printf("Please re-enter your height(cm): ");
|
||||||
|
heightResult = scanf("%f", &height);
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure correct values for gender are entered
|
// Make sure correct values for gender are entered
|
||||||
while (1) {
|
while (1) {
|
||||||
|
// Read in gender from user
|
||||||
printf("Please enter your gender(male or female): ");
|
printf("Please enter your gender(male or female): ");
|
||||||
scanf("%s", gender);
|
scanf("%s", gender);
|
||||||
if (strcmp("male", gender) == 0 || strcmp("female", gender) == 0) {
|
|
||||||
|
// Loop through all chars in gender and convert to lower case
|
||||||
|
for (int i = 0; i < strlen(gender); i++) {
|
||||||
|
gender[i] = tolower(gender[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if gender is valid
|
||||||
|
if (!(strcmp("male", gender)) || !(strcmp("female", gender))) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
printf("Bad value: Please enter either male or female all lowercase\n");
|
printf("Bad value: Please enter either male or female\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +82,7 @@ int main(){
|
|||||||
boyd = (0.0003207 * pow(height, 0.3)) * pow((1000 * weight), (0.6721 - (0.0188 * log10(weight))));
|
boyd = (0.0003207 * pow(height, 0.3)) * pow((1000 * weight), (0.6721 - (0.0188 * log10(weight))));
|
||||||
|
|
||||||
// Check if overweight and gender to select correct life expectancy message
|
// Check if overweight and gender to select correct life expectancy message
|
||||||
|
// Only need to check one since the input screens to just two options
|
||||||
if (strcmp("male", gender)) {
|
if (strcmp("male", gender)) {
|
||||||
if (bmi > 25) {
|
if (bmi > 25) {
|
||||||
lifeExpect = 81.4;
|
lifeExpect = 81.4;
|
||||||
|
|||||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user