Time to time I see some common mistakes or even make some my self. I figure I would post some of these, with hopes that people will learn from my and others mistakes.
1) Why you no work?
int sum = 0;
for(int i = 1; i < MAX; ++i);
{
sum += i;
}
2)Why you work so well?
char * arguments[argCount] //argCount defined somewhere else
for(int i = 0; i < argCount; ++i){
char *arg = new char[256];
initialize(arg,256); //assume
arguments[i] = arg;
delete arg;
arg = NULL;
}
3) I use C++ like I use math!
int input = 0;
cin >> input;
char letterGrade;
if(50 <= input <= 60) letterGrade = 'F'; //Fail
else letterGrade = 'P'; //Pass
4)Why you garbage one time and work other?
int x;
get(x);
doStuff(x);
//....
void get(int& x){
if(! (cin >> x) ){ resetStream(cin); } //assume resetStream resets cin to valid
}
5)Why you garbage one time and work other?
int input;
char letterGrade;
cin >> input;
if( 0 < input && input < 70) letterGrade = 'F';
else if(70 < input && input < 100) letterGrade = 'P';
6)Why you not print all?
void print(int *arr){
for(int i = 0; i < sizeof(arr)/sizeof(*arr); ++i){ cout << arr[i] << endl; }
}
7) Why you wrong value?
//x,y are passed coordinates and buttonID is the id of which button is clicked
void mousePressed(int x, int y, char buttonID){
if(buttonID == LEFT_BUTTON){
int x = x/WINDOW + OFFSET;
int y = y/WINDOW + OFFSET;
player.move(x,y);
}
}
8) Why I so skinny?
int myWeight = 50;
int yourWeight = 100;
cout << "I am " << myWeight/yourWeight << " times skinner than you? ";
These are just some common mistakes I have witness people make, some I made before. I left the solution as an exercise to the reader( ask/post if you want answer ). Some of these are straight forward, others aren't as much. Some have more than one bugs.
If you have some common mistakes people usually make, post it here to contribute. Thanks.
Regards, D.Chhetri
EDIT: Just to clarify, the reason why I'm not posting the answers right now is to help you learn. Reading it might make you aware of it, but thinking will help you learn. After you have figured it out, post here or msg me, then you will get evaluated. Also after some time, I will post all answers.