Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
print("BMI Calculator\n\n1. Weight in pounds, height in inches\n2. weight in kilograms, height in meters\n");

    choice=int(input("choice: "))
    while((choice !=1) and (choice !=2)):
            choice = int(input("invalid choice! please enter a valid choice: "))

    BMI = 0.0
    W = 0.0
    H = 0.0

    if (choice == 1):

        W=float(input("\nWeight in pounds?: "))
        H=float(input("\nHeight in inches?: "))
        BMI = (W*703)/(H*H)

        print("\n\nResult............\n\n Weight:\t"+str(W)+" pounds")
        print(" Height:\t"+str(H)+" inches")
        print(" BMI:\t"+str(round(BMI,1)))

   elif (choice == 2):
        W=float(input("\nWeight in kilograms?: "))
        H=float(input("\nHeight in meters?: "))
        BMI = (W)/(H*H)

        print("\n\nResult............\n\n Weight:\t"+str(W)+" kilograms")
        print(" Height:\t"+str(H)+" meters")
        print(" BMI:\t"+str(round(BMI,1)))

   if (BMI >= 30)
   {
       print("Status:\tObese")
   }
    elif ((BMI < 30)and(BMI >= 25)):
    {
        print("Status:\tOverweight")
    }
    elif ((BMI<25)and(BMI>=18.5)):
    {
        print("Status:\tNormal")
    }
    else:
    {
        print("Status:\tUnderweight")
    }


What I have tried:

this is my C code but it keeps on executing a infinite loop




C++
     double BMI = 0.0;
    double weight = 0.0;
   double height = 0.0;
    int choice;

    printf("BMI Calculator\n\n1. Weight in pounds, Height in inches\n2. Weight in kilograms, Height in meters \n");

    printf("\n\nchoice:\t");
    scanf("%lf",&choice);
    printf("\n\n");


    while((choice != 1)||(choice != 2))
    {
    printf("invalid choice! please enter a valid choice: ");

    }

   if (choice==1)
  {
    printf("Enter weight in kilograms: ");
  scanf("%lf\n", &weight);
  printf("Enter height in meters: ");
  scanf("%lf\n", &height);
   BMI = (weight)/(height*height);
   printf("\n\nResult..............\n\n");
  printf("\n Weight:\t%.1lf kg\n Height:\t%.1lf m\n BMI:\t\t%.1lf \nStatus:\t",weight,height,BMI);
}
else if (choice==2)
{
  printf("Enter weight in pounds: ");
  scanf("%lf", &weight);
  printf("Enter height in inches: ");
  scanf("%lf", &height);
   BMI = (weight*703)/(height*height);

  printf("\n Weight:\t%.1lf kg\n Height:\t%.1lf m\n BMI:\t\t%.1lf \nStatus:\t",weight,height,BMI);
}
  printf("Enter weight in kilograms: ");
  scanf("%lf", &weight);
  printf("Enter height in meters: ");
  scanf("%lf", &height);
   BMI = (weight)/(height*height);
  
  printf("\n\nResult..............\n\n");
  printf("\n Weight:\t%.1lf kg\n Height:\t%.1lf m\n BMI:\t\t%.1lf \nStatus:\t",weight,height,BMI);

 if (BMI >= 30)
 printf("Obese\n");
 else if ((BMI < 30)&&(BMI >= 25))
 printf("Overweight\n");
 else if ((BMI < 25)&&(BMI>= 18.5))
 printf("Normal\n");
 else
 printf("Underweight\n");
Posted
Updated 5-Aug-21 20:35pm
v2

Quickly scanning your code, I see
C++
while((choice != 1) || (choice != 2))...
This will always evaluate to true and therefore loop forever. I think you want && instead of ||.
 
Share this answer
 
Quote:
this is my C code but it keeps on executing a infinite loop

Yes, you exactly designed your code to do so.
In this loop, you forgot to read user input again.
C++
while((choice != 1)||(choice != 2))
{
printf("invalid choice! please enter a valid choice: ");
// you need to read the new choice here
}

Your choice of parameter "%lf" is weird to read an integer
C++
scanf("%lf",&choice);

You should read the documentation again.

In any case, the debugger is the tool that will let you see what is going on side your code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Try
C++
#include <iostream>
#include <limits>
#include <map>
using namespace std;

class Bmi
{
  double m, h, f;
public:
  Bmi(double m, double h, double f): m{m},h{h}, f{f} {}
  double operator() () {return f * m / h /h; }
  const string desc()
  {
    double b = (*this)();

    if (b >= 30)
      return "obese";
    else if (b >= 25)
      return "overweight";
    else if (b >= 18.5)
      return "normal";
    else
      return "underweight";
  }
};


int main()
{
  int selection;

  map<int, double> mfactor{ { 1, 1.0}, {2, 703.0} };

  for (;;)
  {
    cout << "select 1 for metric units (kg, m), 2 for imperial units (in, lb)\n";

    cin >> selection;

    auto it = mfactor.find(selection);

    if ( it != mfactor.end() )
    {
      double m, h;
      cout << "please enter the mass and height\n";
      cin >> m >> h;
      Bmi bmi( m, h, it->second );
      cout << "BMI is " << bmi() << ", you are " << bmi.desc() << "\n";
      break;
    }
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    cout << "invalid selection, please try again\n";
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900