BMI calculator lab can't find issue - c

For this lab I am not allowed to edit the main function, everything must be done in the function below main. I can't seem to find my problem here. I think it has something to do with the call to the calculateBMI function.
#include <stdio.h>
FILE *fp;
//For loop, which allows up to 4 entries.
int main(void) {
int i;
fp = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateBMI();
}
fclose(fp);
return 0;
}
//Function that calculates the BMI of the Input.
double calculateBMI(int weightInPounds, int heightInInches) {
double BMI;
BMI = weightInPounds * 703 / heightInInches * heightInInches;
//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %d, you are underweight.", BMI);
fprintf(fp, "Your BMI is %d, you are underweight.", BMI);
}
//if BMI is between 18.5 and less then 25 print this.
else if (BMI > 18.5 & BMI < 25) {
printf("Your BMI is %d, you are Normal.", BMI);
fprintf(fp, "Your BMI is %d, you are Normal.", BMI);
}
//if BMI is greater then 25 and less then 30 print this.
else if (BMI > 25 & BMI < 30) {
printf("Your BMI is %d, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %d, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else (BMI > 30) {
printf("Your BMI is %d, you are Obese.", BMI);
fprintf(fp, "Your BMI is %d, you are Obese.", BMI);
}
//Asks user for input weight in pounds.
printf("What is your weight in pounds?");
fprintf(fp, "What is your weight in pounds?");
scanf("%d\n", weightInPounds);
fscanf(fp, "%d\n", weightInPounds);
// Asks user for input height in inches.
printf("What is your height in inches?");
fprintf("What is your height in inches?");
scanf("%d\n", heightInInches);
fscanf(fp, "%d\n", heightInInches);
getchar(0);
return (0);
}

In the else if statement you used the & operator , but in this case you need to use && operator . The & operator is a bitwise operator .
For example if you have two 4 bit variable 1001 and 1010 . You use the & operator the result will be 1000 .
In this case you have to use && operator
It should look like this:
else if (BMI > 18.5 && BMI < 25)

There are many simple mistakes in your code.
You should define your calculateBMI function before main or You should declare it before main.
while calling calculateBMI function pass the parameter for the function / read the values inside the calculateBMI function.
if you declare BMI as double then use %lf as format specifier in printf statement.
cant give condition for else statement, so make it else if
use bracket for equation
BMI = weightInPounds * 703 / heightInInches * heightInInches;
you should pass the address of the variable for scanf statement (i.e &variable)
here is the modified code.
#include <stdio.h>
FILE *fp;
double calculateBMI();
//For loop, which allows up to 4 entries.
int main(void) {
int i;
fp = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateBMI();
}
fclose(fp);
return 0;
}
//Function that calculates the BMI of the Input.
double calculateBMI(int weightInPounds, int heightInInches) {
double BMI=0;
//Asks user for input weight in pounds.
printf("What is your weight in pounds?");
fprintf(fp, "What is your weight in pounds?");
scanf("%d\n", &weightInPounds);
fscanf(fp, "%d\n", weightInPounds);
// Asks user for input height in inches.
printf("What is your height in inches?");
fprintf(fp,"What is your height in inches?");
scanf("%d\n", &heightInInches);
fscanf(fp, "%d\n", heightInInches);
BMI = (weightInPounds * 703) / (heightInInches * heightInInches);
//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %f, you are underweight.", BMI);
fprintf(fp, "Your BMI is %f, you are underweight.", BMI);
}
//if BMI is between 18.5 and less then 25 print this.
else if (BMI > 18.5 & BMI < 25) {
printf("Your BMI is %f, you are Normal.", BMI);
fprintf(fp, "Your BMI is %f, you are Normal.", BMI);
}
//if BMI is greater then 25 and less then 30 print this.
else if (BMI > 25 & BMI < 30) {
printf("Your BMI is %f, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %f, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else if(BMI > 30) {
printf("Your BMI is %f, you are Obese.", BMI);
fprintf(fp, "Your BMI is %f, you are Obese.", BMI);
}
getchar();
return (0);
}
extra info. i think the in BMI formula you should give height in meters/ convert it into meters.

Related

Math is not outputting correctly

I am still learning to code and wrote this code to get the total after the weight and distance are calculated. Can someone tell my why the math is not working?
Example: When I enter 5 for weight and 1500 miles instead of getting 8.20 back I get 3.50.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
double weight,mileCost;
int miles, segment, remainder, stop = 1;
while(stop == 1){
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%f", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 || weight == 0 || weight > 10){
printf("Invalid entry! Try Again.");
}
segment= ceil((double) miles / 500);
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
else if(weight > 2 && weight <= 6){
mileCost = 3.70 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight <= 10){
mileCost = 5.25 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
system("pause");
printf("Would you like to ship another package?\n Enter 1 to continue or 0 to stop. \n");
scanf("%d", &stop);
if(stop == 0){
printf("Thank you for your business! \n");
}
}
return 0;
}
Changing double weight, mileCost; to float weight, mileCost; will make it work since you are reading the variable from stdin using scanf("%f", &weight) (for floats) and not scanf("%lf", &weight) (for double).
I am still puzzled by why you do this:
if(remainder > 0)
remainder = 1;
Wouldn't ceil() already handle that in:
segment = ceil((double) miles / 500);
Also, based on the formula you provided, inputting weight=5 and miles=1500 should output 11.1.
You must use "lf" for a reading of a double variable:
scanf("%lf", &weight);

How do I only make a printf statement appear if the statement is true?

I'm making a shipping calculator, if the package weighs over 50 pounds were unable to ship the package. I want to make a statement appear only if the package weighs more than 50 pounds, but it appears no matter what.
I tried it as an else statement, if statement, and if else statement.
main(){
double distance, weight, weightCharges, shippingCharge, distanceCharge, unableToShip;
printf ("Enter the weight of the package:\n");
scanf ("%lf", &weight);
printf ("Enter the distance your package needs to go: \n");
scanf ("%lf", &distance);
if (weight <= 10)
weightCharges = weight * 3.00;
else
if (weight <= 50)
weightCharges = weight * 5.00;
else (weight > 50);
weightCharges= 0;
if (distance > 1000)
distanceCharge = weightCharges + 10;
shippingCharge = weightCharges + distanceCharge;
unableToShip = weight > 50;
printf ("Your total cost is: %.2lf \n", shippingCharge);
printf ("We're unable to ship your package \n", unableToShip);
}
I expect the second printf to only appear if we cannot ship their package, but it appears no matter what.
if works the same way here as it does anywhere else:
if (unableToShip)
printf ("We're unable to ship your package\n");
Did you try to put some if-else statements?
int main()
{
double distance, weight, weightCharges, shippingCharge, distanceCharge; // no need for unableToShip;
printf ("Enter the weight of the package:\n");
scanf ("%lf", &weight);
printf ("Enter the distance your package needs to go: \n");
scanf ("%lf", &distance);
if (weight <= 10)
weightCharges = weight * 3.00;
else
if (weight <= 50)
weightCharges = weight * 5.00;
else (weight > 50);
weightCharges= 0;
if (distance > 1000)
distanceCharge = weightCharges + 10;
shippingCharge = weightCharges + distanceCharge;
//unableToShip = ;
printf ("Your total cost is: %.2lf \n", shippingCharge);
if(weight > 50)
printf ("We're unable to ship your package \n");
}
First off, unableToShip should be of type int or bool (if you are using a compiler that is up-to-date and supports <stdbool.h>). double doesn't really work well for this sort of thing.
Secondly, even if unableToShip were a correct type for a boolean condtion, passing as an argument to printf does not make printf work conditionally. That makes it part of the formatted output, if you had a format string to accept it. What you need to have is if (unableToShip) followed by the printf statement.
Third, if you are printing a string that does not contain any formatting and ends in a new line, you should use puts instead of printf.
int main(){
double distance, weight, weightCharges, shippingCharge, distanceCharge, unableToShip;
printf ("Enter the weight of the package:\n");
scanf ("%lf", &weight);
printf ("Enter the distance your package needs to go: \n");
scanf ("%lf", &distance);
if (weight <= 20)
weightCharges = weight * 5.00;
else
if (weight <= 100)
weightCharges = weight * 10.00;
else (weight > 100);
weightCharges= 0;
if (distance > 1000)
distanceCharge = weightCharges + 20;
shippingCharge = weightCharges + distanceCharge;
unableToShip = weight > 100;
printf ("Your total cost is: %.2lf \n", shippingCharge);
printf ("We're unable to ship your package \n", unableToShip);
}

Simple IF statement scanf

For an assignment I was asked to create a small program which asks for the users input which determines the converter they wish to operate. My question is why doesn't the program ask for the users input, AFTER they have entered which convertor they wish to use (1 or 2). Instead of calling scanf, it just runs the entire statement in one go.
#include <stdio.h>
int main()
{
float cm;
float inches;
int operation;
printf("Hello and welcome to the Inches to Centimetres converter. \n");
printf("Choose from the options below by entering the corresponding number:\n");
printf("Inches to CM converter (1)\n");
printf("CM to Inches converter (2)\n");
scanf("&d", &operation);
if (operation == 1)
{
printf("Please enter the amount of Inches you wish to convert : \n");
scanf("%f", &inches);
cm = inches * 2.54;
if (inches <= 0)
{
printf("Invalid number");
}
else
printf("%f inches is equal to %f centimetres.", inches, cm);
}
else if (operation == 2);
{
printf("Please enter the amount of Centimetres you wish to convert : ");
scanf("%f", &cm);
inches = cm / 2.54;
if (cm <= 0)
{
printf("Invalid number");
}
else
printf("%f centimetres is equal to %f inches.", cm, inches);
}
}
Output
Two problems here. First:
scanf("&d", &operation);
There's a typo, "&d" should be "%d", and it's why you get prompted twice right away. You want:
scanf("%d", &operation);
Second is this:
}
else if (operation == 2);
{
The ; immediately ends the else block. So the block in braces will always run. Get rid of the ;
}
else if (operation == 2)
{
Better yet:
} else if (operation == 2) {
Formatting your braces this way will practically eliminate this type of error.

Outputting the maximum and minimum value from an array in C

I have written a program that asks the user the input the high and low temperature over the course of four days. Following this, the program calculates the mean temperature using the inputs from all four days. Everything is working fine however, I need to have the program determine and output the greatest high temperature and the day it occurred on as well as the smallest low temperature and the day it occurred on. Here's my code so far
#include <stdio.h>
#define NUMS 4
int main (void)
{
int high[NUMS];
int low[NUMS];
const int MAX = 40;
const int MIN = -40;
int totalhigh;
int totallow;
int sum;
float avg;
printf ("---===IPC Temperature Analyzer ===---\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high[0]);
printf ("Enter the low value for day 1: ");
scanf ("%d", &low[0]);
while (high[0] > MAX || low[0] < MIN || high[0] < low[0]) {
printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high[0]);
printf ("Enter the low value for day 1: ");
scanf ("%d", &low[0]);
}
printf ("Enter the high value for day 2: ");
scanf ("%d", &high[1]);
printf ("Enter the low value for day 2: ");
scanf ("%d", &low[1]);
while (high[1] > MAX || low[1] < MIN || high[1] < low[1]) {
printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
printf ("Enter the high value for day 2: ");
scanf ("%d", &high[1]);
printf ("Enter the low value for day 2: ");
scanf ("%d", &low[1]);
}
printf ("Enter the high value for day 3: ");
scanf ("%d", &high[2]);
printf ("Enter the low value for day 3: ");
scanf ("%d", &low[2]);
}
printf ("Enter the high value for day 4: ");
scanf ("%d", &high[3]);
printf ("Enter the low value for day 4: ");
scanf ("%d", &low[3]);
while (high[3] > MAX || low[3] < MIN || high[3] < low[3]) {
printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
printf ("Enter the high value for day 4: ");
scanf ("%d", &high[3]);
printf ("Enter the low value for day 4: ");
scanf ("%d", &low[3]);
}
totalhigh = high[0] + high[1] + high[2] + high[3];
totallow = low[0] + low[1] + low[2] + low[3];
sum = totalhigh + totallow;
avg = sum/8.0;
printf ("The average (mean) temperature was: %.2f\n", avg);
if (high[0] > high[1] || high[0] > high[2] || high[0] > high[3]) {
printf ("The highest temperature was %d, on day 1\n", high[0]);
}
else if (high[1] > high[0] || high[1] > high[2] || high[1] > high[3]) {
printf ("The highest temperature was %d, on day 2\n", high[1]);
}
else if (high[2] > high[0] || high[2] > high[1] || high[2] > high[3]){
printf ("The highest temperature was %d, on day 3\n", high[2]);
}
else {
printf ("The highest temperature was %d, on day 4\n", high[3]);
}
return 0;
}
Your current code can use a loop and a helper function, which would shorten your code by reducing all those scanf() calls. You could also abstract a lot more, by using more functions, but it will show the general idea.
It is also good to check the result of scanf(), just in case the user enters a non-integer.
Your current code could look like this:
#include <stdio.h>
#include <stdlib.h>
#define NUMS 4
/* takes a pointer to a number */
void get_input(int *temp) {
if (scanf("%d", temp) != 1) {
printf("Invalid temp entered\n");
exit(EXIT_FAILURE);
}
}
int main(void) {
int high[NUMS];
int low[NUMS];
const int MAX = 40;
const int MIN = -40;
int day = 1, totalhigh = 0, totallow = 0, sum;
float avg;
for (size_t i = 0; i < NUMS; i++) {
printf ("Enter the high value for day %d: ", day);
/* takes the address of the pointer given by get_input() */
get_input(&high[i]);
printf ("Enter the low value for day %d: ", day);
get_input(&low[i]);
while (high[i] > MAX || low[i] < MIN || high[i] < low[i]) {
printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
printf ("Enter the high value for day %d: ", day);
get_input(&high[i]);
printf ("Enter the low value for day %d: ", day);
get_input(&low[i]);
}
day++;
}
for (size_t i = 0; i < NUMS; i++) {
totalhigh += high[i];
totallow += low[i];
}
sum = totalhigh + totallow;
avg = sum/8.0;
printf ("The average (mean) temperature was: %.2f\n", avg);
return 0;
}
In terms of finding the largest and smallest temperatures, here is a method you can use:
Set max and min to the first element of your array, array[0].
loop from i=1 to i=n.
If and element if bigger than max, set max to array[i]. If an element is smaller than min, set min to array[i].
The day for the highest and lowest temperatures will be i+1.
Since doing something like this will help you understand loops better, I decided to just describe the steps. The above code was just an improvement on your current code, and showing you a easier way to do it will show you a different perspective on how to do problems like these.
I updated my code to have the if statement mentioned in my above code to function correctly. Here it is:
if (high[0] > high[1] && high[0] > high[2] && high[0] > high[3]) { // Check to see if day 1 has the highest temperature against days 2,3 and 4.
printf ("The highest temperature was %d, on day 1\n", high[0]); // Output day 1 as the highest temperature and indicate the temperature value.
}
else if (high[1] > high[0] && high[1] > high[2] && high[1] > high[3]) { // Same function as the above function for day 1 except this is used for day 2.
printf ("The highest temperature was %d, on day 2\n", high[1]); // Refer to day 1 printf
}
else if (high[2] > high[0] && high[2] > high[1] && high[2] > high[3]){
printf ("The highest temperature was %d, on day 3\n", high[2]);
}
else {
printf ("The highest temperature was %d, on day 4\n", high[3]);
}
// Switch out high values with low values in order to determine lowest temperature and its corresponding day.

Need help writing a BMI calculator in C

I am attempting to write a program that will take a user's input of weight and height and then return a BMI value and tell the user if they are under/over or normal weight. The code compiles with no errors, however no matter what numbers I input for weight and height, the result is always "You have a BMI of 0 and your weight status is overweight". Is there something wrong with my code or is my math just incorrect?
#include <stdio.h>
int main()
{
double wt_lb, ht_in, bmi, ht_ft;
printf("Please enter your weight in whole pounds: ");
scanf("%lf", &wt_lb);
printf("Please enter your height in whole inches: ");
scanf("%lf", &ht_in);
ht_ft = ht_in/12;
bmi = (703*wt_lb)/(ht_ft*ht_ft);
if (bmi < 18.5) {
printf("You have a BMI of %.lf, and your weight status is underweight\n" &bmi);
} else if (bmi >= 18.5 && bmi < 25) {
printf("You have a BMI of %.lf, and your weight status is normal\n", &bmi);
} else {
printf("You have a BMI of %.lf, and your weight status is overweight\n", &bmi);
}
}
Remove & from aal of your printf's argument.
printf("You have a BMI of %f, and your weight status is underweight\n" &bmi);
^
|
Remove this &
It should be
printf("You have a BMI of %f, and your weight status is underweight\n", bmi);
Also never use %lf specifier for double in printf (in scanf you have to use) instead use %f.
In the printf statement don't use &bmi, use simple bmi.
It should work

Resources