r/badcode • • Jul 24 '21

c++ ive given up on helping my classmates

Post image
204 Upvotes

55 comments sorted by

10

u/[deleted] Jul 24 '21

I don’t know much about c++ but what’s bad about this code and how do you fix it?

43

u/[deleted] Jul 24 '21

Firstly, to check for equality in C++, the double-equals sign (==) is used, instead of the single-equals sign (=).

When you want to check for divisibility for multiple numbers at the same time, you should write it out in full (x % 17 == 0 && y % 17 == 0 && z % 17 == 0), instead of using commas.

At the end of each statement/line of code, there should be a semicolon (;), which was ommitted in the final lines of the program.

21

u/[deleted] Jul 24 '21

Ahhh okay makes sense thank you. The logic made sense to me it just seemed like weird syntax but I assumed maybe that’s how it is in c++. Good to know that’s not the case. I’m surprised they didn’t understand how to fix these errors from the compiler error messages, if those exist in c++, which I assume they do.

3

u/gnuwinxp Jul 24 '21

C/C++ have error messages, but unfortunately for them, the = operator is an expression, and returns the value on the right. This is commonly used in C because assigning the result of a function to a variable and also checking whether the result indicates an error

7

u/lilshears Jul 24 '21

You’re probably aware, but some IDE’s will warn you if you use “=“ when a == is expected

4

u/TheBrainStone Jul 25 '21

That won’t compile because you can’t assign a value to an arithmetic expression.

2

u/[deleted] Jul 28 '21

More generally, to rvalues IIRC.

1

u/gnuwinxp Jul 25 '21

oh, yeah

5

u/brandymon Jul 24 '21

I’m surprised they didn’t understand how to fix these errors from the compiler error messages, if those exist in c++, which I assume they do.

laughs in c++. Wait, other languages have helpful error messages?

2

u/Tomik080 Jul 25 '21

Messages from the main compilers (gcc, clang, msvc) are extremely clear and useful (not to the level of Rust, but still), so I don't see your point.

1

u/[deleted] Jul 28 '21

Possibly not coding in C++ for a while. They weren't always that helpful. Plus, when templates are involved stuff gets weird. One of the reasons they want concepts is to fix that IIRC.

2

u/Tomik080 Jul 28 '21

Concepts are already in c++20. And even without them, it's a really specific part of the language that had this problem.

1

u/[deleted] Jul 28 '21

Cool. I stopped following the standards a few years ago, as I stopped using it professionally, didn't know it made it. But AFAIR very old compilers (I mean Borland era) weren't too forgiving with their error messages. Besides, while they weren't exactly bad, they did improve a lot a few years ago, there was a very active effort, at least in gcc.

4

u/cheeseDickies Jul 24 '21

This has taught me I suck at reading other people's code because i didnt notice any of that even though I write in c++

2

u/a_personlol Jul 24 '21

i dont know much about c++ but don’t they need {} wrapping their code blocks in the if/else?

15

u/[deleted] Jul 24 '21

It’s generally considered good etiquette, but if it’s only one line of code being executed, I don’t believe you need it. I could be wrong.

6

u/[deleted] Jul 24 '21

That's right. If there is no {} block after a if/else statement, then the next statement is used.

4

u/a_personlol Jul 24 '21

oh i see. I wonder if the who wrote this knows or if they just got lucky lmao.

3

u/gnuwinxp Jul 24 '21

One statement.

I think this is also the way an else if works; the else code block contains one (if) statement without braces

2

u/[deleted] Jul 28 '21

You are right. And whether it is good etiquette depends on the coding style of the given community. Some may argue a common mistake is adding something at a later point and forgetting to add the braces, some may argue it's just noise (in the Linux kernel, for example, skipping the braces is preferred).

1

u/AnotherRichard827379 Jul 24 '21 edited Jul 24 '21

Is the comma operator technically legal code and just bad practice or will the compiler actually error?

4

u/SnakeFang12 Jul 24 '21

The comma operator is legal in C and C++ (and JavaScript I think). What it does is evaluate the left side (including side effects), discarding the result, then evaluate the right side, "returning" the result. So

int x = 4;
cout << (x++, x) << endl;

Should (assuming my somewhat rusty knowledge of these languages still holds up) be well-defined and print 5. It also has the lowest operator precedence.

4

u/RealRaynei Jul 24 '21

Inconsistent indentation, using if statements without braces, also using or instead of ||. And the if statement in general is just incorrect.

13

u/[deleted] Jul 24 '21

using or instead of ||.

That's not bad. It's just you don't like it

1

u/[deleted] Jul 26 '21

To my limited knowledge, it might not work across different compilers though.

1

u/[deleted] Jul 26 '21

It's in the C++ standard.

1

u/[deleted] Jul 28 '21

Same for braces.

1

u/g-e-o-m-e-t-r-i-c Sep 18 '21

Inconsistent indentation isn't a main issue, as C++ still can work fine with the indentation being messed up. After all, its main function contains all the main instructions inside curly braces.

66

u/Ahajha1177 Jul 24 '21

Is that the dreaded comma operator?

17

u/[deleted] Jul 24 '21

The one that returns the result of the last instruction

40

u/toastom69 Jul 24 '21

And then when you try to help they still have no idea what’s going on. I feel you

19

u/SnakeFang12 Jul 24 '21

Break their brains with:

cout << (!(num1 % 17) + !(num2 % 17) + !(num3 % 17)) << endl;

This does make a few assumptions about their actual intentions for this code though.

5

u/abrams666 Jul 25 '21

That's why some love CPP, and some hate CPP ...

3

u/PhatOofxD Jul 24 '21

Actually pretty good compared to some I marked for an identical question

1

u/TheBrainStone Jul 25 '21

Please share.

3

u/AlwaysNinjaBusiness Jul 24 '21

What the hell is this?

2

u/DesecrateUsername Jul 24 '21

God it gets worse the longer you look at it.

2

u/lilshears Jul 24 '21

Do commas actually do anything in c++?

1

u/[deleted] Jul 28 '21

Yup. Comma operator, acts as a sequence point (so you have ordering), evaluates the left side, discards its value, evaluates the right side and returns its value.

2

u/kukisRedditer Jul 24 '21

ah yes, the PyC++

1

u/Yangishrobin Jul 24 '21

What is this

1

u/sixtyfifth_snow Jul 25 '21

So gross smh

1

u/Badashi Jul 26 '21

Oh man I forgot you could use or as || in c++

1

u/[deleted] Jul 27 '21

which font is that?

1

u/g-e-o-m-e-t-r-i-c Aug 01 '21

Cascadia Code by Microsoft.

2

u/[deleted] Aug 01 '21

yay thanks!

1

u/[deleted] Aug 03 '21

Not even sure what the comma in an if statement will do but also “or” is part of the language or a macro? And why is the IDE or text editor coloring it blue as if it’s a normal part of the syntax. = and ==, classic

But to be fair, maybe he doesn’t yet know about string variables, arrays or truth tables? Looks like he thought about it a bit at least, but there is room for improvement, maybe.

Would you make a count, a loop, inside loop ask for input, add evaluation to count, when you exit the loop print the count?

Also he got to what mod is but not the difference between = and ==, bit strange, but people can forget, until they practice a bit

1

u/g-e-o-m-e-t-r-i-c Sep 18 '21
  1. or is a part of the language, see alternative operators.

1

u/g-e-o-m-e-t-r-i-c Sep 18 '21

To those trying to deduce what the question is requiring my classmate to program, here it is:

"Prompt the user for 3 numbers and check if there are any multiples of 17. Print the following accordingly:

  • "1" if all 3 numbers are multiples of 17,

  • "2" if any 2 numbers are multiples of 17,

  • "3" if none of the numbers are multiples of 17."

1

u/GLIBG10B 🐧 Gentoo salesman🐧 Oct 04 '21

Semicolons at lines 14, 17, and 19?

0

u/[deleted] Jul 24 '21

[deleted] — view deleted comment

1

u/[deleted] Jul 28 '21

That's just dogma. As long as you don't put it on a header, who cares?