Kk thanks

| Blogs > konadora |
|
konadora
Singapore66357 Posts
Kk thanks ![]() | ||
|
qrs
United States3637 Posts
| ||
|
fusionsdf
Canada15390 Posts
On July 10 2009 01:47 Cambium wrote: You need to figure out the inequalities... you have 0 < a < 20 and 20 < a < 25, i.e. you don't cover the case where a == 20. #include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; // assuming you can't weight 0 kg and have 0 cm height ( h = 0 actually give you division by zero exception if( w <= 0 || h <= 0 ) { cout << "Invalid input." << endl; return 0; } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << endl; } if ( a > 0 && a < 20 ) cout << "You have a low BMI." << endl; else if ( a > 20 && a < 25 ) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } ewww. I was always taught multiple return statements like that are frowned upon. to be honest, I'm not exactly sure what's considered acceptable in a professional environment. The code (if someone else is wondering) will still work perfectly, its just a matter of style. | ||
|
Cambium
United States16368 Posts
| ||
|
konadora
Singapore66357 Posts
#include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; return 0; } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; } if ( a > 0 && a < 20 ) cout << "You have a low BMI." << endl; else if ( a > 20 && a < 25 ) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } And still, if I input two negative values, "You have a low BMI." still shows. What's the problem here? | ||
|
Cambium
United States16368 Posts
| ||
|
konadora
Singapore66357 Posts
Oh, and happy birthday ![]() | ||
|
Cambium
United States16368 Posts
| ||
|
Cambium
United States16368 Posts
| ||
|
konadora
Singapore66357 Posts
#include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; a = (w/(h*h)); if (w > 0, h > 0) cout << "Your Body Mass Index is " << a << endl; else cout << "Invalid input." << endl; if (0 < a < 20, w > 0, h > 0) cout << "You have a low BMI." << endl; else if (20 < a < 25, w > 0, h > 0) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } | ||
|
GogoKodo
Canada1785 Posts
I guess you maybe just pasted the wrong source? | ||
|
konadora
Singapore66357 Posts
| ||
|
GogoKodo
Canada1785 Posts
| ||
|
Cambium
United States16368 Posts
I'm amazed at its ability to interpret equalities and inequalities... The problem with your code is that you don't terminate when you detected invalid inputs. The program continues to run and since two negatives give you a positive, you get "You have a low BMI". After you terminate, you no longer need to check w > 0 and h > 0, as you have already done so. #include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; a = (w/(h*h)); if (w > 0, h > 0) cout << "Your Body Mass Index is " << a << endl; else { cout << "Invalid input." << endl; return 0; } if (0 < a < 20 cout << "You have a low BMI." << endl; else if (20 < a < 25 cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } | ||
|
konadora
Singapore66357 Posts
I was wondering but, it's okay to have many 'return 0; ' statements? | ||
|
GogoKodo
Canada1785 Posts
| ||
|
Cambium
United States16368 Posts
There is no way you get anything beyond "invalid input" if you ran the code below... if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; return 0; } catches any negative inputs. + Show Spoiler + #include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; return 0; } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; } if ( a > 0 && a < 20 ) cout << "You have a low BMI." << endl; else if ( a > 20 && a < 25 ) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } | ||
|
GogoKodo
Canada1785 Posts
| ||
|
Cambium
United States16368 Posts
#include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; if ( a > 0 && a < 20 ) cout << "You have a low BMI." << endl; else if ( a > 20 && a < 25 ) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; } return 0; } | ||
|
konadora
Singapore66357 Posts
Compiler = Visual C++ 2008 Express Edition I'm using this one #include <iostream> using namespace std; int main() { double w, h; double a; cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; return 0; } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; } if ( a > 0 && a < 20 ) cout << "You have a low BMI." << endl; else if ( a > 20 && a < 25 ) cout << "You are within the healthy BMI range. Congratulations!" << endl; else cout << "You have exceeded the healthy BMI range." << endl; return 0; } The one I posted after that was the original before I modified it | ||
| ||
BSL 21
ProLeague - RO32 Group A
Gosudark vs Kyrie
Gypsy vs OyAji
UltrA vs Radley
Dandy vs Ptak
ZZZero.O213
LAN Event
Stellar Fest: Day 2
ByuN vs LamboLIVE!
Zoun vs Scarlett
Clem vs TriGGeR
[ Submit Event ] |
StarCraft 2 StarCraft: Brood War Dota 2 League of Legends Counter-Strike Heroes of the Storm Other Games Grubby3971 Beastyqt740 Pyrionflax247 Fuzer Mlord154 mouzStarbuck129 ToD114 ArmadaUGS105 goatrope65 Organizations Other Games Counter-Strike Other Games StarCraft 2 StarCraft: Brood War
StarCraft 2 StarCraft: Brood War Dota 2 League of Legends Other Games |
|
Replay Cast
Sparkling Tuna Cup
WardiTV Korean Royale
LAN Event
IPSL
JDConan vs WIZARD
WolFix vs Cross
BSL 21
spx vs rasowy
HBO vs KameZerg
Cross vs Razz
dxtr13 vs ZZZero
Replay Cast
Wardi Open
WardiTV Korean Royale
Replay Cast
[ Show More ] Kung Fu Cup
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
The PondCast
RSL Revival
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
WardiTV Korean Royale
RSL Revival
Classic vs Creator
Cure vs TriGGeR
Kung Fu Cup
CranKy Ducklings
RSL Revival
herO vs Gerald
ByuN vs SHIN
Kung Fu Cup
BSL 21
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
|
|
|