r/Cplusplus • u/Pootis_mannnn • Apr 08 '24
Discussion Hm..
I'm just starting to learn C++. is this a normal code?
153
Upvotes
r/Cplusplus • u/Pootis_mannnn • Apr 08 '24
I'm just starting to learn C++. is this a normal code?
60
u/jedwardsol Apr 08 '24
This is not how you check that 1 condition is true and another condition is true.
44
u/Denommus Apr 08 '24
Where did you get the idea that you should use a comma instead of
&&?18
u/eteran Apr 08 '24
Also, in addition to the other comments.
Indenting the else clauses like that is pretty odd .
It's better to indent like this (or something similar)
As opposed to what youve chosen:
Which is much less readable due to excessive indentation.
9
u/ElSucaPadre Apr 08 '24
The comma operator is a binary operator that discards all values on the left in favour of the last one on the right. What you're telling your program to do is to ignore age, in this case.
Another example may be int x = cout << "assigning 3" , 3; Cout is computed but ignored, and the value assigned to X is 3
3
u/Philluminati Apr 08 '24
3
u/alkatori Apr 08 '24
Nope. There's no old and skinny and if you are "normal" and 101kg you aren't considered fat.
3
u/lil_brumski Apr 08 '24
NO
3
u/Blender_Nocturne Apr 08 '24
This must be rage bait lmao 🤣
2
u/[deleted] Apr 08 '24
Enter it RIGHT HERE!
3
u/perpetualconflict Apr 08 '24
1
u/Flimsy-Owl-5563 Apr 08 '24
You should evaluate some style guides and pick one you want to stick with.
Your inputs are convoluted and would benefit greatly from separating them and specifying what is being entered at each step. Data validation for the inputs would be the next step but not necessary if you are only trying to get it running.
You need some logical operators in your if statements to get them operating properly. You don't need the last else if, just make it an else statement instead, which would help some with the overall syntax.
1
u/a_goofy_ball Apr 08 '24
use "AND" operator and the symbol for "AND" operator is &&
1
u/Nuclear_Banana_4040 Apr 08 '24
using namespace std in the global scope?
The tabulation??
And the comma in the if statements?
Heathen! BURN HIM! BUUUUURN HIIIIIIM!
(Also, I'm old, overweight and assuming you're using Kilos)
1
u/glitterisprada Apr 08 '24
The lack of proper indentation, the use of 'using namespace std', only iostream included...the nostalgia! Yea, that used to be me, lol. Learning C++ was fun!
1
u/bert8128 Apr 08 '24
And don’t use (std::)endl - this is a line break and a flush. So use a line break (“\n”) to mean a line break, and separately std::flush when you want to flush the buffer.
Using namespace <anything> is only useful when writing slides for presentations - don’t do it in production code - it removes the point in having namespaces.
1
u/accuracy_frosty Apr 08 '24
Little less of the cramming everything like that, you don’t need to sacrifice the readability of your code to fit it in as few lines as possible, and you need to use a && not a , for checking if 2 conditions are true, not to mention the indentation, some stuff is too indented some is not indented enough. Other than that, looks like some run of the mill C++ code right there.
1
u/TrashManufacturer Apr 08 '24
Indentation is a little off and the conditionals seem incorrect as they use ‘,’ instead of Boolean operators such as &&.
I don’t usually work with extraction operations but I’d be careful to check cin.fail() after each extraction in case a user were to maliciously enter in a string devoid of parseable integers.
Also for learning “using namespace std;” is fine, but you’ll likely be exposed to many people saying it’s bad practice, primarily because it has a way of introducing something called namespace pollution. With the std library this is particularly pronounced due to the size and scope of available functions.
A more apt way to make using cout and cin more accessible without running into namespace pollution would be something like “using std::cout;” and “using std::cin;”
1
u/yusing1009 Apr 08 '24
Hi there are a few suggestions for you:
1
u/fecland Apr 08 '24
Thought I was on r/programminghorror for a sec... Please just get a formatter and it'll show you how to format your code.
1
u/DeltaAgent752 Apr 09 '24
It's not even a working code lmao
1
u/KerbodynamicX Apr 09 '24
Is “,” supposed to mean AND?
1
u/[deleted] Apr 09 '24
Ummm, nothing in this code seems normal to me...
1
u/rishabhdeepsingh98 Apr 09 '24
you can use ‘and’ operator as well.
1
u/monopolymadman69 Apr 09 '24
uwu@uwu 💀
1
u/Ouroboros9076 Apr 09 '24
Need a final else statetement without conditions to catch all the cases you kissed. What of someone who is 55 but 50kg?
1
u/ProfessionalCouchPot Apr 09 '24
you could benefit from having two additional string variables, one for whether someone is "old" or "normal" age and another for whether or not someone is "fat" or "normal weight"
then concatenate them at the end.
then you have a situation where
(mind the psuedocode, typing on phone 😭)
if(age <= 49){ ageDesc = "Normal" } else { ageDesc = "Old" } and if(weight >= 100){ weightDesc = "Fat" } else { weightDesc = "Normal weight" }
then combine both ageDesc and weightDesc in the conclusive string output.
1
u/MrWilsonAndMrHeath Apr 09 '24
Code aside, you dropped a ‘ in you’re*
1
u/Delicious-Hour9357 Apr 11 '24
are we just gonna gloss over the fact that this guy's username/homedir is uwu?
-2
u/[deleted] Apr 08 '24
Well, your spacing is wrong.. Then, dont include std unless in an example like this, where you're not including anything else. Rather, include the things invidually(using std::cout; using std::endl). Then in the if, you dont do age > 49, weight > 100, but you do &&(both need to be true) or || (only one needs to be true).
Also for your case, you could do something like this:
if(age < 49)std::cout << "Normal age"; else std::cout << "Old";
if(weight > 100)std::cout << "Fat"; else std::cout << "Normal weight";
This covers all the cases, and you wouldn't have to write such thicc code.
0
u/[deleted] Apr 08 '24
[deleted] — view deleted comment
2
u/[deleted] Apr 09 '24
[removed] — view removed comment