#Review my code

79 messages · Page 1 of 1 (latest)

icy cradle
#

Hey, I made BMI calculator and so far it works fine.
I want someone to review it,tell me where this can be improved further and can I make it more readable?

#include<iostream>

int main() {
float kg,meter,feet,inch,BMI,height;
int Q;
std::cout<<"\t\t Welcome to BMI calculator! \t\t\n\n";
std::cout<<"Enter the Weight(Kg):";
std::cin>>kg;

std::cout<<"Enter the height(feet):";
std::cin>>feet;
std::cout<<"Enter the height(inch):";
std::cin>>inch;

meter=(feet*0.3)+(inch*0.025);
BMI=kg/(meter*meter);
std::cout<<"Your BMI is: "<<BMI<<std::endl;
if(BMI<18.5){
    Q=1;
}else if(BMI>18.5 && BMI<24.9){
    Q=2;
}else if(BMI>=25 && BMI<29.9){
    Q=3;
}else if(BMI>=30 && BMI<34.9){
    Q=4;
}else if(BMI>=35 && BMI<39.9){
    Q=5;
}else{
    Q=6;
}
switch(Q){
    case 1:
    std::cout<<"You are Underweight.";
    break;
    case 2:
    std::cout<<"You have Normal weight.";
    break;
    case 3:
    std::cout<<"You are Over weight.";
    break;
    case 4:
    std::cout<<"You are in Obese class 1.";
    break;
    case 5:
    std::cout<<"You are in Obese class 2.";
    break;
    case 6:
    std::cout<<"You are in Obese class 3.";
    break;
    default:
    std::cout<<"Invalid";
}
return 0;

}

vernal havenBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
native wasp
#

You don't need the BMI > ?? checks, because the previous if already checked the same thing

#

And also because you added those, BMI == 24.95 will now slip between the two conditions

#

You're missing the \n after the final result

#

You could also put all those strings into a single array and use Q as the index, to avoid a lengthy switch

native wasp
#

Instead of else if(BMI>18.5 && BMI<24.9) you can just do else if(BMI<24.9) (or < 25, whatever it's supposed to be)

native wasp
#

Something like const char *results[] = {"Your are ...", "...", "..." /*and so on*/};, then print results[Q-1]

icy cradle
muted lodge
native wasp
#

Your book should have a chapter on arrays 🤷‍♂️

#

But if you're required to use a switch, then ok

vernal havenBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
native wasp
#

Read this 👆

#

Yeah this works. A bit too clever perhaps

vernal havenBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
icy cradle
#

Don't know how to use this command

#

But anyways thank you

muted lodge
#

!f

vernal havenBOT
#

I have learnt about array but didn't knew you can use it like that

Btw did some changes

#include <iostream>

int main() {
  float kg, meter, feet, inch, BMI, height;
  std::cout << "\t\t Welcome to BMI calculator! \t\t\n\n";
  std::cout << "Enter the Weight(Kg):";
  std::cin >> kg;

  std::cout << "Enter the height(feet):";
  std::cin >> feet;
  std::cout << "Enter the height(inch):";
  std::cin >> inch;

  meter = (feet * 0.3) + (inch * 0.025);
  BMI = kg / (meter * meter);
  std::cout << "Your BMI is: " << BMI << std::endl;

  switch (1 + (BMI >= 18.5) + (BMI >= 25) + (BMI >= 30) + (BMI >= 35) +
          (BMI > 40)) {
    case 1:
      std::cout << "You are Underweight." << std::endl;
      break;
    case 2:
      std::cout << "You have Normal weight." << std::endl;
      break;
    case 3:
      std::cout << "You are Over weight." << std::endl;
      break;
    case 4:
      std::cout << "You are in Obese class 1." << std::endl;
      break;
    case 5:
      std::cout << "You are in Obese class 2." << std::endl;
      break;
    case 6:
      std::cout << "You are in Obese class 3." << std::endl;
      break;
    default:
      std::cout << "Invalid" << std::endl;
  }
  return 0;
}
Aditya
icy cradle
#

Oh

#

I see

muted lodge
icy cradle
#

Lemme try

vernal havenBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
icy cradle
icy cradle
#

Did same

muted lodge
#

you did /code

icy cradle
#
#include<iostream>

int main() {
    float kg,meter,feet,inch,BMI,height;
    std::cout<<"\t\t Welcome to BMI calculator! \t\t\n\n";
    std::cout<<"Enter the Weight(Kg):";
    std::cin>>kg;
    
    std::cout<<"Enter the height(feet):";
    std::cin>>feet;
    std::cout<<"Enter the height(inch):";
    std::cin>>inch;
    
    meter=(feet*0.3)+(inch*0.025);
    BMI=kg/(meter*meter);
    std::cout<<"Your BMI is: "<<BMI<<std::endl;
    
    switch(1+ (BMI>=18.5) + (BMI>=25)+ (BMI>=30)+(BMI>=35)+(BMI>40)){
        case 1:
        std::cout<<"You are Underweight."<<std::endl;break;
        case 2:
        std::cout<<"You have Normal weight."<<std::endl;break;
        case 3:
        std::cout<<"You are Over weight."<<std::endl;break;
        case 4:
        std::cout<<"You are in Obese class 1."<<std::endl;break;
        case 5:
        std::cout<<"You are in Obese class 2."<<std::endl;break;
        case 6:
        std::cout<<"You are in Obese class 3."<<std::endl;break;
        default:
        std::cout<<"Invalid"<<std::endl;
    }
    return 0;
}
#

Ah

#

Thank

#

Now I got it

#

Is there command to compile the code?

native wasp
#

;compile
```cpp
code here
```

sonic waspBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

native wasp
#

But since your program requires user input, it's not gonna work in the bot

icy cradle
#

;compile

#include<iostream>

int main() {
    float kg,meter,feet,inch,BMI,height;
    std::cout<<"\t\t Welcome to BMI calculator! \t\t\n\n";
    std::cout<<"Enter the Weight(Kg):";
    std::cin>>kg;
    
    std::cout<<"Enter the height(feet):";
    std::cin>>feet;
    std::cout<<"Enter the height(inch):";
    std::cin>>inch;
    
    meter=(feet*0.3)+(inch*0.025);
    BMI=kg/(meter*meter);
    std::cout<<"Your BMI is: "<<BMI<<std::endl;
    
    switch(1+ (BMI>=18.5) + (BMI>=25)+ (BMI>=30)+(BMI>=35)+(BMI>40)){
        case 1:
        std::cout<<"You are Underweight."<<std::endl;break;
        case 2:
        std::cout<<"You have Normal weight."<<std::endl;break;
        case 3:
        std::cout<<"You are Over weight."<<std::endl;break;
        case 4:
        std::cout<<"You are in Obese class 1."<<std::endl;break;
        case 5:
        std::cout<<"You are in Obese class 2."<<std::endl;break;
        case 6:
        std::cout<<"You are in Obese class 3."<<std::endl;break;
        default:
        std::cout<<"Invalid"<<std::endl;
    }
    return 0;
}
sonic waspBOT
#
Program Output
Welcome to BMI calculator! 

Enter the Weight(Kg):Enter the height(feet):Enter the height(inch):Your BMI is: -nan
You are Underweight.
icy cradle
#

Hmm it works

native wasp
#

Your BMI is: -nan yeah right

muted lodge
#

because there's no input

#

to input: ;compile | input1 input2

native wasp
#

Oh, you can do this? Nice

icy cradle
#

;compile | input1 input2 input3

#include<iostream>

int main() {
    float kg,meter,feet,inch,BMI,height;
    std::cout<<"\t\t Welcome to BMI calculator! \t\t\n\n";
    std::cout<<"Enter the Weight(Kg):";
    std::cin>>kg;
    
    std::cout<<"Enter the height(feet):";
    std::cin>>feet;
    std::cout<<"Enter the height(inch):";
    std::cin>>inch;
    
    meter=(feet*0.3)+(inch*0.025);
    BMI=kg/(meter*meter);
    std::cout<<"Your BMI is: "<<BMI<<std::endl;
    
    switch(1+ (BMI>=18.5) + (BMI>=25)+ (BMI>=30)+(BMI>=35)+(BMI>40)){
        case 1:
        std::cout<<"You are Underweight."<<std::endl;break;
        case 2:
        std::cout<<"You have Normal weight."<<std::endl;break;
        case 3:
        std::cout<<"You are Over weight."<<std::endl;break;
        case 4:
        std::cout<<"You are in Obese class 1."<<std::endl;break;
        case 5:
        std::cout<<"You are in Obese class 2."<<std::endl;break;
        case 6:
        std::cout<<"You are in Obese class 3."<<std::endl;break;
        default:
        std::cout<<"Invalid"<<std::endl;
    }
    return 0;
}
sonic waspBOT
#
Program Output
Welcome to BMI calculator! 

Enter the Weight(Kg):Enter the height(feet):Enter the height(inch):Your BMI is: -nan
You are Underweight.
muted lodge
icy cradle
muted lodge
#

what do you want to input

icy cradle
#

Hmmm
70
5
9

Random 3 value

#

Try putting this

sonic waspBOT
#
Program Output
Welcome to BMI calculator! 

Enter the Weight(Kg):Enter the height(feet):Enter the height(inch):Your BMI is: 23.5245
You have Normal weight.
muted lodge
#

that's how

icy cradle
sonic waspBOT
#
Program Output
Welcome to BMI calculator! 

Enter the Weight(Kg):Enter the height(feet):Enter the height(inch):Your BMI is: 168.032
You are in Obese class 3.
warm otter
#

Thank you for making us this toy

#

I approve

icy cradle
#

500kg

#

😶

warm otter
#

... You got a problem with that or something?

muted lodge
#

chill

icy cradle
#

No? Why I should have problem with other people body...

It was just shocking,I didn't meant that reaction to be offensive,sorry if you felt hurt

warm otter
#

Truth is I'm not actually 500 kilograms

#

I'm 1000

#

But yeah good app

#

Great way to demonstrate switch statement syntax

chrome pewter
#

That adding of boolean values is clever as hell. However, remember the Brian Kernighan quote about clever code.

warm otter
#

It's fine

#

I understood what was happening immediately

#

The case is going to be the number of booleans that matched

#
  • 1
#

... actually yeahhh that +1 fucks it up

#

It'd be okay if you didn't have the +1

#

I'd get rid of it and then decrement all of the case ints by 1