What's error of my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What's error of my code?

#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { cout<<endl<<"\nWeight:"; double weight; cin>>weight; cout<<endl<<"\nHeight:"; double height; cin>>height; double heightpow2=height*height; double bmi=weight/heightpow2; cout<<endl<<"\nBmi = "<<bmi; double hlr=rand()%17.9+12; if(bmi==hlr){ cout<<"\nWeight loss range"; } double hwr=rand()%24+18; if(bmi==hwr){ cout<<"\nHealthy weight range"; } return 0; }

24th Feb 2021, 10:32 PM
Arash
Arash - avatar
17 Answers
+ 5
like amost all languages: if (low <= bmi && bmi < high) == equals != different < lower than <= lower or equals > greater than >= greater or equals && logical AND || logical OR
24th Feb 2021, 11:35 PM
visph
visph - avatar
+ 3
What are the errors you are getting from it?
24th Feb 2021, 10:47 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
about int and double
24th Feb 2021, 10:50 PM
Arash
Arash - avatar
+ 2
what are you meaning by using % operator, so?
24th Feb 2021, 11:09 PM
visph
visph - avatar
+ 1
Arash the error says that you're trying to get remainder of integer division (% operator) with a decimal (double) as right operand (divisor)... maybe you're trying to get 17.9 percent of rand()? if so, you must do: rand()*17.9/100 ... and add 12, obviously: double hlr = rand()*17.9/100+12; edit: similar for hwr ;)
24th Feb 2021, 10:54 PM
visph
visph - avatar
+ 1
No, I want to find a random number between the two number
24th Feb 2021, 11:08 PM
Arash
Arash - avatar
+ 1
so, first you doesn't need random values, nor hlr and hwr... you just need to test if bmi is greater (or equal) than lowest boundary AND lowest than higest boundary ;)
24th Feb 2021, 11:26 PM
visph
visph - avatar
0
With your solution, the errors disappear but the "if" command is no longer executed
24th Feb 2021, 11:12 PM
Arash
Arash - avatar
0
what do you think doing with % initially? and what are you trying to achieve? as 'if' blocks have quite no chance to be executed, even if you provide the good calculation to get a decimal random value between two bounds ^^
24th Feb 2021, 11:15 PM
visph
visph - avatar
0
What about "switch"?
24th Feb 2021, 11:18 PM
Arash
Arash - avatar
0
what about explaining your purpose?
24th Feb 2021, 11:19 PM
visph
visph - avatar
0
I named the variable under bmi in the code and now I want it to print a message if the value is between the two selected numbers
24th Feb 2021, 11:23 PM
Arash
Arash - avatar
0
How?
24th Feb 2021, 11:32 PM
Arash
Arash - avatar
0
You did type double value of int function. For Example: Int x = 28 double y = 17.9
26th Feb 2021, 9:15 PM
Coni ютубер
Coni ютубер - avatar
- 1
Tanks a lot
24th Feb 2021, 11:37 PM
Arash
Arash - avatar
- 1
#include <iostream> #inlcude <stdlib.h> #include <time.h> using namespace std; Int main() { double weight; cout << "Weight : "; cin >> weight; cout << endl; double height; cout << " Height "; cin >> height; cout << endl; double heightpow2 ; heightpow2 = height*height; cout << endl; double bmi ; bmi = weight/heightpow2; cout << "Bmi " << bmi << endl; double hlr; hlr = rand() % 17.9+12; If(bmi==hlr) { cout << "Weight loss range"; } double hwr; hwr = rand()%24+18; If(bmi==hwr) { cout << "Helthy weight range"; } system(" pause") return 0; } &&&&&: If it c++ : Do u include - <ctime>?? #include <ctime> "for rand" And Why u dont add : srand(0(time)) ?
26th Feb 2021, 6:05 PM
Артур Андросов
Артур Андросов - avatar
- 2
Айдын номе
26th Feb 2021, 5:11 AM
Tanshola Kadyrbekova
Tanshola Kadyrbekova - avatar