• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 18:01
CEST 00:01
KST 07:01
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Maestros of the Game: Week 1/Play-in Preview9[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9
Community News
Weekly Cups (August 25-31): Clem's Last Straw?32Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris46Weekly Cups (Aug 11-17): MaxPax triples again!15Weekly Cups (Aug 4-10): MaxPax wins a triple6
StarCraft 2
General
Speculation of future Wardii series Weekly Cups (August 25-31): Clem's Last Straw? Heaven's Balance Suggestions (roast me) Geoff 'iNcontroL' Robinson has passed away #1: Maru - Greatest Players of All Time
Tourneys
LiuLi Cup - September 2025 Tournaments Sea Duckling Open (Global, Bronze-Diamond) Sparkling Tuna Cup - Weekly Open Tournament Maestros of The Game—$20k event w/ live finals in Paris Monday Nights Weeklies
Strategy
Custom Maps
External Content
Mutation # 489 Bannable Offense Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies
Brood War
General
ASL20 General Discussion Starcraft at lower levels TvP BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion Victoria gamers
Tourneys
[Megathread] Daily Proleagues Is there English video for group selection for ASL [ASL20] Ro24 Group F [IPSL] CSLAN Review and CSLPRO Reimagined!
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Warcraft III: The Frozen Throne Nintendo Switch Thread Mechabellum
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread YouTube Thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
Customize Sidebar...

Website Feedback

Closed Threads



Active: 742 users

C++ revision help! - Page 5

Blogs > konadora
Post a Reply
Prev 1 2 3 4 5 6 7 Next All
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 09 2009 22:51 GMT
#81
Oh lol

Kk thanks
POGGERS
qrs
Profile Blog Joined December 2007
United States3637 Posts
Last Edited: 2009-07-09 22:54:14
July 09 2009 22:53 GMT
#82
'As per the American Heart Association, the beat of the Bee Gees song "Stayin' Alive" provides an ideal rhythm in terms of beats per minute to use for hands-only CPR. One can also hum Queen's "Another One Bites The Dust".' —Wikipedia
fusionsdf
Profile Blog Joined June 2006
Canada15390 Posts
Last Edited: 2009-07-10 00:37:39
July 10 2009 00:37 GMT
#83
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.
SKT_Best: "I actually chose Protoss because it was so hard for me to defeat Protoss as a Terran. When I first started Brood War, my main race was Terran."
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 00:59 GMT
#84
Multiple return statements are acceptable in a professional environment.
When you want something, all the universe conspires in helping you to achieve it.
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 10 2009 01:52 GMT
#85
Okay I debugged the thing

#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?
POGGERS
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 02:00 GMT
#86
that's surprising... did you copy and paste or did you modify your source?
When you want something, all the universe conspires in helping you to achieve it.
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
Last Edited: 2009-07-10 02:01:06
July 10 2009 02:00 GMT
#87
Modify

Oh, and happy birthday
POGGERS
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 02:01 GMT
#88
paste your source
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 02:01 GMT
#89
thanks for the hb
When you want something, all the universe conspires in helping you to achieve it.
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 10 2009 02:14 GMT
#90
#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;
}
POGGERS
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
Last Edited: 2009-07-10 02:19:37
July 10 2009 02:18 GMT
#91
Are you still using commas in the 'if' statements? If you are then that's a problem. You have to use && for AND, || for OR

I guess you maybe just pasted the wrong source?
twitter: @terrancem
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 10 2009 02:19 GMT
#92
Nope. The modified one is the one few posts above
POGGERS
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 10 2009 02:20 GMT
#93
Ok. I got confused because your last post has the commas in it.
twitter: @terrancem
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 02:20 GMT
#94
What's your OS and what IDE/compiler are you using?

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, 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;
}
When you want something, all the universe conspires in helping you to achieve it.
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 10 2009 02:22 GMT
#95
Ooooh

I was wondering but, it's okay to have many 'return 0; ' statements?
POGGERS
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 10 2009 02:23 GMT
#96
Now Cambium is modifying the source with the commas too. Hrmm, perhaps I'm still confused as to what your latest source is.
twitter: @terrancem
Cambium
Profile Blog Joined June 2004
United States16368 Posts
July 10 2009 02:23 GMT
#97
I'm confused. Which source are you running?

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;
}
When you want something, all the universe conspires in helping you to achieve it.
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 10 2009 02:26 GMT
#98
It's sort of ok. You can do it, but for beginning programming it's usually much easier to keep a single return for a function (main in this case).
twitter: @terrancem
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2009-07-10 02:27:05
July 10 2009 02:26 GMT
#99
Yes, it's okay to have multiple return statements... but this is better structurally, because your first if/else statement doesn't make too much sense.



#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;
}
When you want something, all the universe conspires in helping you to achieve it.
konadora *
Profile Blog Joined February 2009
Singapore66201 Posts
July 10 2009 02:26 GMT
#100
OS = Vista 64-bit
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
POGGERS
Prev 1 2 3 4 5 6 7 Next All
Please log in or register to reply.
Live Events Refresh
Next event in 1h 59m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
JuggernautJason79
StarCraft: Brood War
Britney 19530
Dewaltoss 108
Aegong 41
sSak 40
NaDa 22
yabsab 7
Counter-Strike
Stewie2K737
Foxcn303
Super Smash Bros
Liquid`Ken21
Heroes of the Storm
Liquid`Hasu492
Other Games
summit1g3062
Grubby2345
FrodaN1368
C9.Mang0161
Trikslyr130
Livibee82
Mew2King69
Nathanias33
Chillindude23
ViBE22
rGuardiaN20
Kaelaris9
Organizations
Other Games
BasetradeTV12
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• davetesta74
• StrangeGG 31
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• intothetv
• Migwel
• Kozan
• IndyKCrew
StarCraft: Brood War
• Pr0nogo 4
• ZZZeroYoutube
• STPLYoutube
• BSLYoutube
Dota 2
• masondota22322
League of Legends
• Doublelift5328
• TFBlade360
• HappyZerGling72
Counter-Strike
• imaqtpie1111
• Shiphtur196
Other Games
• Scarra603
Upcoming Events
Replay Cast
1h 59m
The PondCast
11h 59m
RSL Revival
11h 59m
Maru vs SHIN
MaNa vs MaxPax
Maestros of the Game
18h 59m
Classic vs TriGGeR
Reynor vs SHIN
OSC
1d 4h
MaNa vs SHIN
SKillous vs ShoWTimE
Bunny vs TBD
Cham vs TBD
RSL Revival
1d 11h
Reynor vs Astrea
Classic vs sOs
Maestros of the Game
1d 18h
Serral vs Ryung
ByuN vs Zoun
BSL Team Wars
1d 20h
Team Bonyth vs Team Dewalt
CranKy Ducklings
2 days
RSL Revival
2 days
GuMiho vs Cham
ByuN vs TriGGeR
[ Show More ]
Cosmonarchy
2 days
TriGGeR vs YoungYakov
YoungYakov vs HonMonO
HonMonO vs TriGGeR
Maestros of the Game
2 days
Solar vs Bunny
Clem vs Rogue
[BSL 2025] Weekly
2 days
RSL Revival
3 days
Cure vs Bunny
Creator vs Zoun
Maestros of the Game
3 days
Maru vs Lambo
herO vs ShoWTimE
BSL Team Wars
3 days
Team Hawk vs Team Sziky
Sparkling Tuna Cup
4 days
Monday Night Weeklies
4 days
Liquipedia Results

Completed

CSL Season 18: Qualifier 2
SEL Season 2 Championship
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
CSL 2025 AUTUMN (S18)
Maestros of the Game
Sisters' Call Cup
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

LASL Season 20
2025 Chongqing Offline CUP
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
EC S1
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
MESA Nomadic Masters Fall
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.