# Tutorials On Eight Programming Mistakes Which Could Be Avoided

June 2, 2017

Well programming is certainly not an easy task to do. There are lots of inter-mediate processes involved in the whole scenario. But while in the process, there can be lots of mistakes which can be made. Here are some programming mistakes which can be avoided if focused properly.

### 1. Undeclared Variables

While you are declaring a variable, it is very important to insert the right value into it. If there are any undeclared variable then you can get an error.

```int main()
{
cin>>x;
cout<<x;
}
```

Here is where you will get an error. The reason being the fact that the complier does not know what x means. One certainly needs to declare a variable.

```int main ()
{
int x;
cin>>x;
cout<<x;
}
```

### 2. Uninitialized variables

```int count;
while (count<100)
{
cout<<count;
count++;
}
```

With this command, the program will not enter while it is in the loop. The reason being in C++, variables are certainly not initialized to zero. For examples, in the above snippet code, the count certainly value in the range of interior. There might be a case where it can be 586 but in that situation the loop’s condition can never be true. Say suppose, the output of the program can be to print numbers from 1000 to 99. But in scenario, the variable was assigned a location in memory with data which can happen to evaluate to -1000.

Make sure that the variables are initialized.

### 3. How to set a variable with an uninitialized value

```int a, b;
int sum=a+b;
cout<<"Enter two numbers to add: "; cin>>a;
cin>>b;
cout<<"The sum is: "<<sum;
When Run:
Enter two numbers to add: 1 3
The sum is: -1393
```

You may be wondering what is wrong with the program. In certain cases, beginning programmers wonder that variables work very like equations. If a value is assigned to a variable which can be equal to the result of the operation on various other variables, the value of the variable will change. In case of C++, this is one shot deal. If a value is assigned to a variable then it stays that value until it is reassigned again.

In the example program just because a and b are not initialized, the sum will add up to an unknown random number so matter what value, the users inputs.

For fixing this error, just move an addition step right after the input line.

```int a, b;
int sum;
cout<<"Enter two numbers to add: "; cin>>b;
cin>>a;
sum=a+b;
cout<<"The sum is: "<<sum;
```

### 4. Make use single equal sign for checking quality

```char x='Y';
while(x='Y')
{
//...
cout<<"Continue? (Y/N)"; cin>>x;
}
```

After typing this program, you will always wonder “Why does not my loop ever end?”

One single equal sign can be used for checking quality then in that case, the program will assign value on the right side of the expression right to the variable on the left hand side and the result of this statement is certainly the value which is assigned. If that is the case then the value is ‘Y’ which can be treated as true. Then the loop will never really end.
One can use== for checking the equality; in fact, to avoid accidental assignment, trying putting the variables on the right hand side of the expression and one will get a complier error if one is accidentally using a single equal sign which cannot assign a value to something which is not a variable.

```char x='Y';
while('Y'==x)
{
//...
cout<<"Continue? (Y/N)"; cin>>x;
}
```

### 5. Functions which not declared

```int main()
{
}
{
//...
}
```

If the above program is given then one will get an error about the menu being unknown. What will happen in this case is that the compiler would not know what menu() will stand for until one as told about it and if you are waiting till you are using it to tell that there is a function which is named menu then the whole function will surely get confused. Remember that for putting a prototype for the function or the entire definition of the function right for the first time, try to use the function.

```void menu();
int main()
{
}
{
...
}
```

### 6. Extra Semicolons

```int x;
for(x=0; x<100; x++);
cout<<x;
```

The use of extra semicolons can surely give different results which one has surely not accepted.

Like the above program will display an output of 100.

The reason is the fact that one has put extra semicolon. Always make sure that semicolons do not go after if statements loop or function defines. If semicolon is put in any if these places, then the program will not function properly.

```int x;
for(x=0; x<100; x++)
cout<<x;```

### 7. Overstepping array boundaries

```int array[10];
//...
for(int x=1; x<=10; x++)
cout<<array[x];```

After typing the above program, the output will surely not show correct values.

Try and remember that arrays start indexing at 0; again they end indexing at the length -1. Take the example; if there is an array of 10 elements, then the first element is usually at the position zero while the last element is at the position 9.

```int array[10];
//...
for(int x=0; x<10; x++)
cout<<array[x];
8. Misusing the && and || operators
int value;
do
{
//...
value=10;
}while(!(value==10) || !(value==20))
```

There is certainly error in the above program also. Even though the value is 10 the program loops. You may be wondering Why?

Think about the only time when the loop condition certainly could be false and both the value==10 and value==20 would have to be true so in that case negation of each would be false so that the || operation would certainly return false.

It is certainly always true that both the value cannot be equal to both 10 or equal to 20 as there cannot both values at the same time. However if the program only requires to loop and the value has neither 10 nor 20, then it is important to use &&:!(value==10) && ! (value==20), which will read much more nicely: “ if the value is certainly not equal to 10 or the value certainly not equal to 20”, this will mean if the value is certainly some other value than ten or twenty. This will be the case where the programmer makes mistake and he reads that it is when there is “this” or “that”, and when one forgets the “other than” then it will apply to the entire statement “ ten or twenty” and certainly not to two terms- “ ten”, “ twenty” – individually.

Now if you do a quick Boolean algebra then the result will help you to a great extent :!( A || B) is equal to! A &&! B. The sentence “value is other than [ten or twenty]”. The right way to write the program is:

```int value;
do
{
//...
value=10;
}while(!(value==10) && !(value==20))
```

Hope this tutorial was helpful as it will help you overcome various programming mistakes in the best way possible. The process will also help you to overcome some of the common mistakes.