# C: BMI calculator stuck in do-while loop

Page 1 of 1

## 5 Replies - 5861 Views - Last Post: 11 February 2010 - 09:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=155298&amp;s=1f501fb4315c522a83d99505278b9cd7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 samhutto

Reputation: 0
• Posts: 8
• Joined: 27-August 09

# C: BMI calculator stuck in do-while loop

Posted 10 February 2010 - 11:09 PM

Hey I'm having a problem with my BMI calculation program. The program is made to find a person's BMI given their weight and height(within a certain range), then find their target weight based on their desired BMI. Lastly it uses a formula to find their ideal body weight based on their gender and previously mentioned height.
I don't get any errors when compiling, but when i run the program, it just gets stuck in the do-while loop(from lines 64-69) that deals with the person's gender. no matter what I enter for the person's gender, it just asks for it again. Do you see what it might be?
Thanks!

```*/
#include <stdio.h>
#include <math.h>
int main()
{
double pounds=0, kg=0, height=0, bmi=0, meter=0, dBMI=0, tKG=0, weight=0, IBW=0;
char gender=0, m=0, f=0;

// Read in weight of person in pounds

do
{
printf("Enter the weight of the person in pounds (between 90 and 350).\n");
scanf("%lf", &pounds);
}
while( pounds <= 90.0 || pounds >= 350.0);

// Read in the height of person in inches

do
{
printf("Enter the height of the person in inches (between 59 and 78).\n");
scanf("%lf", &height);
}
while (height <= 59.0 || height >= 78.0);

kg=pounds/2.2046;
meter=height*.0254;

bmi = kg/(meter*meter);

printf("The BMI is: %.2lf.", bmi);
// Read in the desired BMI

do
{
printf("\n\nEnter the desired BMI (between 18.5 and 30).\n");
scanf("%lf", &dBMI);
}
while (dBMI <=18.5 || dBMI >=30);

tKG = dBMI*(meter*meter);
weight = tKG*2.2046;

printf("The target weight is: %.2lf.\n", weight);

do
{
printf("\nIs the person a male or female? Enter m or f:\n");
scanf(" %c", &gender);
}
while (gender != m || gender != f);

//Compute IBW according to gender
if (gender == m)
{
IBW = 50.0 + 2.3 * (meter - 60.0);
printf("The ideal weight is: %lf.", IBW);
}
else if (gender == f)
{
IBW = 45.5 + 2.3 * (meter - 60.0);
printf("The ideal weight is: %lf.", IBW);
}

return 0;
}

```

This post has been edited by JackOfAllTrades: 11 February 2010 - 06:19 AM

Is This A Good Question/Topic? 0

## Replies To: C: BMI calculator stuck in do-while loop

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: C: BMI calculator stuck in do-while loop

Posted 10 February 2010 - 11:12 PM

Thanks!

### #3 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

## Re: C: BMI calculator stuck in do-while loop

Posted 10 February 2010 - 11:21 PM

```do
{
printf("\nIs the person a male or female? Enter m or f:\n");
scanf(" %c", &gender);
}
while (gender != m || gender != f);
```
First, you're checking variables m and f which are equal to 0, not 'm' and 'f' (the characters). Second, you should use &&, not ||.

This post has been edited by PlasticineGuy: 10 February 2010 - 11:26 PM

Reputation:

## Re: C: BMI calculator stuck in do-while loop

Posted 11 February 2010 - 12:08 AM

```do
{
printf("\nIs the person a male or female? Enter m or f:\n");
scanf("%c", &gender);
}
while (gender != 'm' && gender != 'f');

//Compute IBW according to gender
if (gender == 'm')
{
IBW = 50.0 + 2.3 * (meter - 60.0);
printf("The ideal weight is: %lf.", IBW);
}
else if (gender == 'f')
{
IBW = 45.5 + 2.3 * (meter - 60.0);
printf("The ideal weight is: %lf.", IBW);
}

return 0;
}
```

This post has been edited by JackOfAllTrades: 11 February 2010 - 06:20 AM
Reason for edit:: Added code tags.

### #5 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

## Re: C: BMI calculator stuck in do-while loop

Posted 11 February 2010 - 01:26 AM

Don't write code for people.

### #6 samhutto

Reputation: 0
• Posts: 8
• Joined: 27-August 09

## Re: C: BMI calculator stuck in do-while loop

Posted 11 February 2010 - 09:06 AM

PlasticineGuy, on 10 February 2010 - 10:21 PM, said:

```do
{
printf("\nIs the person a male or female? Enter m or f:\n");
scanf(" %c", &gender);
}
while (gender != m || gender != f);
```
First, you're checking variables m and f which are equal to 0, not 'm' and 'f' (the characters). Second, you should use &&, not ||.

Thanks very much! That helps a ton