Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Seeing my first two comments, I saw it too but didn't think just telling you would be fair ... In basic you do comparisons with a single equals too, same as with SQL. Now, I usually get errors with SQL where I don't expect them...
Poseidon wrote:
took me an hour or so to find out.. you would think after years programming experience you wouldn't make such a mistake ???
Actually, I do. After years of programming you most of all learn to avoid complex mistakes. You will never learn to avoid the simple ones, but compiler warnings take care of them. They explain why I can develop at a pretty nice speed, they just plain warn me about anything.
FYI, my most common mistake is "Undeclared variable: i".
I'm wondering what sort of code the compiler generates for this - might be better to use inline assembly (BTR and BTS instructions)...
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
= or == in c/c++/java and the like I always check first, if some condition doesn't work althou I've deliberately set all the parentheses and curly brackets in correct order - Not as If one immediately sees it.
I've read over your piece of code the first time and simply assumed there were == in the conditional expression - and so /dev/brain gave me two. Such a shame *gg*
the biggest trap as candy says are the all so evident looping variables - and suddenly one is outta scope and the compiler complains - oh, how can you even dare to use that i - and you 've got a hard time finding what's going wrong.
Just for testers: two pieces of code with a hard error to find. First took me two hours, second I didn't find (but it wasn't my problem, the other guy did find it after 2-4 hours):
int calculate_value(int input1, int input2) {
int toreturn = 12 * input1 + 0.75 * input2 //* interest calculation, must also divide by the amount of months */ constant1
+ 14; /* constant adjustment for flaw in input */
return toreturn;
}
The compiler didn't give an error, but the C++ compiler did something other than the C compiler used before. What?
[edit] Ok, the first example featured TWO errors. One was my own in retyping the error, and the second hasn't even been spotted. [/edit]
int main(void); // that looks really like a declaration, so no
//wonder the compiler bails out.
int main(void){ ... } // that's the expected way *gg*
in second example, the c++ compiler will find that there is something to comment out after "* input2". It won't get any division to execute and will continue with "+ 14 ;" *gg* THe moral of the story: never ever place comments inside a line of code, only after the concluding ; *gg* But I admit, upon first look and if desperately searching, one would overlook those simple things as /dev/brain tells "This can't be, so it isn't"
Candy wrote:
Just for testers: two pieces of code with a hard error to find. First took me two hours, second I didn't find (but it wasn't my problem, the other guy did find it after 2-4 hours):
int calculate_value(int input1, int input2) {
int toreturn = 12 * input1 + 0.75 * input2 //* interest calculation, must also divide by the amount of months */ constant1
+ 14; /* constant adjustment for flaw in input */
return toreturn;
}
The compiler didn't give an error, but the C++ compiler did something other than the C compiler used before. What?
int calculate_value(int input1, int input2) {
int toreturn = 12 * input1 + 0.75 * input2 /* interest calculation, must also divide by the amount of months */ constant1
+ 14; /* constant adjustment for flaw in input */
return toreturn;
}
the error is on the second line the extra "/". the compiler would ignore any after the "//". so Constant1 is ingored. another error is the line must funish with an operator to continue on the next line
Do that in a project where you work together with me and I'll have to do you serious bodily harm.
You are checking whether the pointer is zero, not whether the zero is pointer. The compiler is your friend, enable whatever warnings it can give you, and please do not serve the computer, make the computer serve you.
I hate it when people use hard-to-read syntax for claims of "safety" or "efficiency". Unless you're really, really down core and know that strange syntax is the solution for an existing problem, I claim that code must be easy-to-read for humans, not for computers. Not when -Wall would solve the problem just as well.
Every good solution is obvious once you've found it.