8 Common Programming Mistakes

Learning to program can be tough--just ask anyone who's done it! Fortunately, a lot of problems happen over and over again--I've put together 8 of the most common problems that you'll run into as a new programmer.


1. Undeclared Variables


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

"Huh? Why do I get an error?"


Your compiler doesn't know what x means. You need to declare it as a variable.

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

2. Uninitialized variables


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

"Why doesn't my program enter the while loop?"


In C++ variables are not initialized to zero. In the above snippet of code, count could be any value in the range of int. It might, for example, be 586, and in that situation the while loop's condition would never be true. Perhaps the output of the program would be to print the numbers from -1000 to 99. In that case, once again, the variable was assigned a memory location with garbage data that happened to evaluate to -1000.


Remember to initialize your variables.

3. Setting a variable to 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

"What's wrong with my program?"


Often 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 example), the value of the variable will change. In C++ assignment does not work this way: it's a one shot deal. Once you assign a value to a variable, it's that value until you reassign the values. In the example program, because a and b are not initialized, sum will equal an unknown random number, no matter what the user inputs.