So, in this post, I will tell some common mistakes, which we always do.... Even I do sometimes now.... But I used to repeat same mistakes again and again when I was newbie in programming. SO, I just thought that it would be helpful for you, if I alert you about those mistakes. Let's start it from here using a list.... :P
1. Undeclared Variables
The most common programming mistake, we do, is just we use variables before declaring them. So compiler becomes angry on us and fights for its right i-e declaration of variables. And we just don't give it its rights, what we do is we throw our laptops on bed, shout on it and rush from our room, and finally while closing doors with full force, we just say
I HATE PROGRAMMING MAN I JUST HATE IT Here is an example of what I am saying.
int main() { cin>>x; cout<<x; }
"Huh! Heyyyyyyyyyyyyy" Oh my God!!!!!!!!! Why Do I get an ERROR?............. :( "
Your dearest friend, whose name is Microsoft Visual Studio, does not know about x. So make him a favor on him. And declare variable x. He will not complain after that. int main()
{
int x;
cin>>x;
cout<<x;
}
"Whoa!!! WhooooooooooooWHOOOOOO....... I made it.... I finally did it..... :) "
2. Uninitialized Variable
But some times, we declare our variables but we forget to initialize them. So compiler gets angry on us. In the reaction, we get angry on compiler. Thus, in the result, we just exit our compiler and start watching movies. (not we, I watch movie when i exit compiler... :) ). Here is an example...int count; while(count<100) { cout<<count; }
"Oh my God... Why doesn't my program enter the while loop? :( "
Now after declaring variable count, it has been initialized to a garbage value. So, compiler doesn't know about value. So, on what basis, he should enter in loop. So don't worry, just make count equal to zero or any other value. And you are done....TIP: Always make your variable equal to zero at time of declaration.
3. Setting a variable to an initialized value
So when we forget initializing our variables, then a new problem occurs. That is we just enter formulas and mathematical expressions on other variables. However, some variables have garbage value, then how can compiler evaluate those expressions. If YES, then on what basis? So, in the result, compiler gives an ERROR. Here is an example:int a, b; int sum=a+b; cout<<"Enter two numbers to add: "; cin>>b; cout<<"The sum is: "<<sum; When Run: Enter two numbers to add: 1 3 The sum is: -1393
"Every thing is perfect, then why sum is a negative number?
I know compiler has problems. Let's try to reinstall it."
No there is no problem in compiler, but there is problem in your program. Often some beginning programmers believe that variables work like equations - if you assign a variable to equal the result of an operation on several other variables that whenever those variables change (a and b in this case), the value of an variable (sum in this case) will change automatically. No, in C, assignment operator ( = ) does not work this way: it's a one shot deal. Once you assign a value to a variable, it will be same until you reassign the values. In the example, because a and b are not initialized, sum will be any garbage number. Then it does not matter what user inputs in a and b. Here is solution of this problem:int a, b; int sum; cout<<"Enter two numbers to add: "; cin>>b; cin>>a; sum=a+b; cout<<"The sum is: "<<sum;
"Yeah!!!!! hooooooo!!!! I made it... Its time to submit it.... :) "
4. Using a single equal sign to check equality
Here is a very common mistake we do. Even I sometimes do it mistakenly. Sometimes, we just compare two variables with single equal sign in any if condition or in any loop. Compiler will not give you error on this problem, but your program will not work as you want it to do. So please be careful. Sometimes, this problem becomes very dangerous for your program as well as your computer. Here I show you an example,char x='Y'; while(x='Y') { //... cout<<"Continue? (Y/N)"; cin>>x; }
"Why doesn't my loop ever end?"
When you use a single equal sign to check equality, then instead of checking equality, it performs assignment. It means it assigns right side value to left side (in this case, x will become 'Y' on every iteration). In the end, either your loop never ends or never start (in this case, x becomes 'Y' every time, that is true, so your loop will not exit. It keeps on running and running making your brain hot :) ). Always use double equal sign to check equality carefully. Further more, to avoid accidental assignment, put your variables on right side and values on left side. When you put single equal to check equality, then compiler will give you an error, that he cannot put values in constants. So, you will get it at compile time. Here I show you example of it:
char x='Y'; while('Y'==x) { //... cout<<"Continue? (Y/N)"; cin>>x; }
"Yes! Yes Yes Yes!!!! I made it."
char x='Y'; while('Y'=x) //Compiler ERROR { //... cout<<"Continue? (Y/N)"; cin>>x; }"Huh! Compiler Error.... Oh yeah! I forgot to put two equal signs. :) "
5. Undeclared Functions
Sometimes, like variables, we call functions before declaring them. So, always put prototypes of functions above main, and definitions below main. And call them in any order, you will not get any error. Here I show you an example,int main() { menu(); } void menu() { //... }
"Why do I get an error about menu being unknown?"
The compile, off course, don't know about menu. Because you are calling menu() before declaration or definition. So, just put a prototype before main() and you are done.
void menu(); int main() { menu(); } void menu() { ... }
"That's what I wanted to do for last 4 hours. :p"
6. Overstepping Array Boundaries
Sometimes, we put indexes of array, which goes beyond array boundaries. Like if array is of 10 elements, we use number 10 in an index. Although we are allowed only from 0 to 9 not 0 to 10. And remember, in this case compiler will not give you error. It just will run your program and do what program says. Here is an example:int array[10]; //... for(int x=1; x<=10; x++) cout<<array[x];"Huh!!!! Why doesn't my program showing me first number?"So you are going from 1 to 10. Although array only exists from 0 to 9. So be careful when using arrays. So, always start from 0 and end at < arraySize. That is tip, which can be applied on loops.
int array[10]; //... for(int x=0; x<10; x++) //Start from 0. End when less than Array Size cout<<array[x];
"Finally, I got it........... :)"
So dear guys, I make sure your program does not contain these mistakes. And be careful at the time of programming. I will keep on posting other basic and common mistakes. Please keep on reading it....
Regards,
Wajahat KarimAuthor, Dreamers Are Developers