So "a" is body mass index you might call the variable "bmi" instead.
double bmi;
w and h are ok for variable names, but they could be better.
double weightKG;
double heightM;
Or something similar.
Blogs > konadora |
GogoKodo
Canada1785 Posts
So "a" is body mass index you might call the variable "bmi" instead. double bmi; w and h are ok for variable names, but they could be better. double weightKG; double heightM; Or something similar. | ||
Cambium
United States16368 Posts
On July 10 2009 11:26 Cambium wrote: 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. Show nested quote + #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
Singapore66063 Posts
| ||
GogoKodo
Canada1785 Posts
| ||
GogoKodo
Canada1785 Posts
Maybe you are somehow running an old executable. Try a clean and build. | ||
konadora
Singapore66063 Posts
Sorry about that! | ||
SonuvBob
Aiur21548 Posts
| ||
fusionsdf
Canada15390 Posts
On July 10 2009 12:20 SonuvBob wrote: lol that's always fun. I FIXED IT, WHY ISN'T IT WORKING?!! god. so much time wasted because of that and you can't even feel all that happy when you fix it. "THIS held me up for 2 hours? THIS?!!?" | ||
konadora
Singapore66063 Posts
Gotta continue revising | ||
coltrane
Chile988 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; // } why are you closing the if here 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; } // closeit here return 0; } look the correction and the comentary in bold. there is something weird in the first scope too... I now think that something... it seems that you believe that return will end the program. Thats not gonna happen, return 0; will jump out of the scope, not from the program. And that first return should be diferent to 0, 0 is NO ERROR. Using stdlib.h (#include <stdlib.h>) you could use exit() function, like this "exit(1);" that will end the program and return 1 from main to std out The thing is "a" cannot be empty, when you reserve the memory it has some value. So it will mostly have any value different to 1..24, then you always will see at std out "You have exceeded..." Try this as an example: #include <iostream> using namespace std; int main() { int a; double b; char c; cout << a << b << c; return 0; } | ||
fusionsdf
Canada15390 Posts
To simplify it, int a = 0; is just as valid as int a; //a's value is ???? a = 0; As long as the assignment of 0 occurs before the variable is used, its fine. As for exit, while you are correct it will work, its been out of favor and frowned upon (in both academic and most professional environments) for a long time. Because main is a function, when it returns, the function will end and it will return to the C++ environment, which will then end as a consequence. Having multiple return statements in main() will work just like it would in any function, although I disagree with it from a stylistic point of view. here's a pretty cool discussion of multiple returns in C/C++: http://bytes.com/groups/c/617841-multiple-returns-functions | ||
konadora
Singapore66063 Posts
thanks fusionsdf so I can just say double a = (w/(h*h)); ? | ||
fusionsdf
Canada15390 Posts
1) a isnt used any point before that line (since by saying double a you are declaring a) 2) w and h have already been declared and given values before that line. As a matter of practice, its considered best to put all your variable declarations in one spot right at the top. so while both + Show Spoiler [#1] + #include <iostream> using namespace std; int main() { double w, h; 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 { double a = (w/(h*h)); //declaring here. This is still valid and will execute properly. 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; } + Show Spoiler [#2] + #include <iostream> using namespace std; int main() { double w, h; //all variables are declared in the same spot at the top of the function 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; } are valid, #2 is considered better from a style point of view (both will execute exactly the same). Since you have to deal with potentially other people going through your code, or you taking a break, forgetting about this program and trying to understand what you were doing 3 month or even 3 years from now, you really want to make sure you are aware of what is considered good style and follow it whenever it makes sense to you. Extra stuff I want to add 2 more things that aren't obvious: 1) In Cambium's code he has: 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; For single line statements you do not need braces. That code is equivalent to: 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; } Note that if there is more than one line, you cant omit the braces and still have it work. This will NOT do what is expected: if ( a > 0 && a < 20 ) //line 1 cout << "You have a low BMI." << endl; //line 2 cout << "good bye!." << endl; //line 3 and will execute line 2 only if line 1 evaluates true and will execute line 3 in every case. When its more than one line, use braces. For multiple single line if else statements, omitting the braces is more often used, but check with your teacher to make sure thats the style she prefers. 2) the second point is that while int a = 0; and int a; a = 0; is true for pretty much every primitive type (double, char, int, float), this does not work for c-strings char c = "hello"; is valid. char c; c = "hello"; won't work (instead you would have to use a built in function)... this might not be an issue for you right now (its certainly not important for this program), but if you ever have a problem with c-strings like that, at least now you've seen it pointed out. | ||
konadora
Singapore66063 Posts
| ||
yh8c4
108 Posts
put your code on http://codepad.org. saves the formatting, has syntax coloring, and can even compile and run the code. i put your code to http://codepad.org/Yn2ulRxd and added some minor corrections (marked by comments). good c++ resources: http://www.parashift.com/c -faq-lite/ http://www.cplusplus.com/ channel ##c++ on irc.freenode.net | ||
coltrane
Chile988 Posts
On July 10 2009 23:53 konadora wrote: coltrane's post is headache >__< thanks fusionsdf so I can just say double a = (w/(h*h)); ? first... why??? and second... no, you cant unless you define a after w and h. And if you do so and then you input 2 wrong values a will have the first value from its definition. | ||
konadora
Singapore66063 Posts
I'm fairly new to C++ so... | ||
coltrane
Chile988 Posts
copypaste this and compile it: #include <iostream> using namespace std; int main() { int a; double b; char c; cout << a << b << c; return 0; } When you run that shit you will realize that: Variables always have a value, even when you dont asign any variable to it. So, a good manner thing is to put some value in a variable like the a in your program WHY? because you are ALWAYS running a multiple selection test on it, but you are NOT ALWAYS assigning a value to it. A good form to do that is changing double a; for the sentence double a(0); ^ thats a constructor, is a function over a variable. It basically says something like "assign 0 to a variable" at the very moment that you declare it as variable. I will rewrite your code. + Show Spoiler + #include <iostream> using namespace std; int main() { double w, h; double a(0); cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; //this line is a comment. The compiler will skip it. ok, you asked for //2 values, if they dont fit the condition then we end just here, // and a is 0 right now. } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; //a has a new valid value. And then we ask: 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; // i suppose you understand why we dont need brackets {} in this if } // if the inputed values are useless to us, then we are here. return 0; } | ||
Jovan
Canada65 Posts
On July 11 2009 01:09 yh8c4 wrote: some recommendations: put your code on http://codepad.org. saves the formatting, has syntax coloring, and can even compile and run the code. i put your code to http://codepad.org/Yn2ulRxd and added some minor corrections (marked by comments). good c++ resources: http://www.parashift.com/c -faq-lite/ http://www.cplusplus.com/ channel ##c++ on irc.freenode.net If you are going to link C++ FAQ Lite, at least link C++ FQA Lite | ||
fusionsdf
Canada15390 Posts
On July 12 2009 05:37 coltrane wrote: okok; copypaste this and compile it: Show nested quote + #include <iostream> using namespace std; int main() { int a; double b; char c; cout << a << b << c; return 0; } When you run that shit you will realize that: Variables always have a value, even when you dont asign any variable to it. So, a good manner thing is to put some value in a variable like the a in your program WHY? because you are ALWAYS running a multiple selection test on it, but you are NOT ALWAYS assigning a value to it. A good form to do that is changing double a; for the sentence double a(0); ^ thats a constructor, is a function over a variable. It basically says something like "assign 0 to a variable" at the very moment that you declare it as variable. I will rewrite your code. + Show Spoiler + #include <iostream> using namespace std; int main() { double w, h; double a(0); cout << "Please enter your weight (in KG) and height (in meters)." << endl; cin >> w >> h; if (w <= 0 || h <= 0){ cout << "Invalid input." << endl; //this line is a comment. The compiler will skip it. ok, you asked for //2 values, if they dont fit the condition then we end just here, // and a is 0 right now. } else { a = (w/(h*h)); cout << "Your Body Mass Index is " << a << "." << endl; //a has a new valid value. And then we ask: 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; // i suppose you understand why we dont need brackets {} in this if } // if the inputed values are useless to us, then we are here. return 0; } honestly I think that's overkill. As long as the programmer is smart enough to assign a value to the variable at some point before use (and hopefully a sanity check), there is no need to have every variable declaration include an assignment. int a; double b; char c; a = 0; b = 0.0; c = cin.get(); cout << a << b << c; return 0; There is no point declaring c with an initial value when you are just going to be getting it from the keyboard immediately after anyways. | ||
| ||
StarCraft 2 StarCraft: Brood War Britney 19478 Dota 2Flash 896 ggaemo 506 Soulkey 204 Hyuk 165 Dewaltoss 141 firebathero 88 PianO 79 sSak 72 Noble 43 [ Show more ] Counter-Strike Other Games Grubby3430 ScreaM2084 hiko1321 FrodaN974 Beastyqt857 XBOCT660 B2W.Neo459 Lowko350 Fuzer 278 Mew2King230 RotterdaM222 Hui .171 QueenE112 ArmadaUGS86 NeuroSwarm86 Trikslyr78 Organizations
StarCraft 2 • MindelVK 301 StarCraft: Brood War• StrangeGG 61 • -Miszu- 11 • Adnapsc2 10 • Kozan • Migwel • AfreecaTV YouTube • sooper7s • intothetv • IndyKCrew • LaughNgamezSOOP • Laughngamez YouTube Dota 2 League of Legends Other Games |
BSL: GosuLeague
Master's Coliseum
Korean StarCraft League
StarCraft2.fi
SOOP
Creator vs Solar
Master's Coliseum
Cheesadelphia
Cheesadelphia
BSL: ProLeague
Cross vs Zhanhun
Dewalt vs TBD
CSO Cup
[ Show More ] Master's Coliseum
BSL: ProLeague
OlimoLeague
Sparkling Tuna Cup
The PondCast
|
|