Did you know..? C and C++ Tips 4

In this episode of Did you know…? we look at some common coding mistakes.

  • Undeclared Variables
  • Uninitialized Variables
  • Setting a variable to an uninitialized value
  • Single equal sign to check equality

Undeclared Variables

Every programmer knows that you need to declare a variable before it is used, but many beginners forget to do this. Take the following example:



int main()
{
	cin >> a;
	cout << a;
}

At a first glance a beginner may think that this program will not give an error, but it does, because the variable a is not declared.
This is a correct example:


int main()
{
	char a;
	cin >> a;

	cout << a;

}


Uninitialized Variables

C++ variables are not initialized to zero if you declare a variable.
Take the following source code example:



int counter;
while ( counter < 25 )
{
	// some code
}

Because counter is not initialized the while loop may or may not work. It all depends on what value is at memory location of counter. The value could be (and most likely is) larger then 25. So you should always initialize it before you use it.


Setting a variable to an uninitialized value

Many beginning programmers think that variables work as equations, but this is not the case.

Take the example below: most of the beginning programmers think that whenever you assign a variable to an equal, the result of an operation on one of the variables (a or b) will change the outcome of the variable.


int a, b;
int sum=a+b;

cout << "Enter two numbers: ";
cin >> a
cin << b;

cout << "Sum = " << sum;

The result of this program can be anything because variable a and b are not initialized, thus the sum of a + b is garbage (whatever value is at the memory location of a and b.) Further more in C++ the first value that you apply to sum will stick to sum until you change it. So calling it in a cout for instance will have no affect on sum even if you changed a and b in this case.

A better example would be:


int a, b;
int sum;

cout << "Enter two numbers: ";
cin >> b;
cin >> a;

sum=a+b;
cout << "Sum = " << sum;


Single equal sign to check equality

Many beginners make the common mistake to use a single equal sign to check equality. For instance in a “while loop”:


char x='Y';
while(x='Y')
{
  cout << "Continue program? (Y or N)";
  cin >> x;
}

Because you only use an equal sign in this “while loop” the “while loop” never ends. The program assigns Y to x even if you enter a N. To fix it you should use a double equal sign (==) if you want to check for equality. Take a look at the working program below:


char x='Y';
while(x=='Y')
{
  cout << "Continue program? (Y or N)";   cin >> x;
}

Tip: if you put the variables on the left hand side of the expression then most compilers will give an error if you place only one equal sign.

Gives an error:


while('Y'=x)

Will work:


while('Y'==x)

This entry was posted in Programming Tips. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.