++ What s wrong with my program Common Programming Mistakes
This article is an attempt to explain the basic programming mistakes that beginners often make. It is an ongoing project that attempts to cover all the basic errors I get when people email me. If you're just starting out programming, looking through it might be a good way to solidify your programming knowledge.
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;
} Uninitialized variables:
int count; while(count<100) {
cout<<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.
Setting a variable to an uninitialized value: int a, b; int sum=a+b; cout<<"Enter two numbers to add: "; cin>>b; cout<<"The sum is: "<<sum; when></sum; when>>b; cin>>a; sum=a+b; cout<<"The sum is: "<<sum; using x="Y) { "></sum; using>>x; } "Why doesn't my loop ever end?"
If you use a single equal sign to check equality, your program will instead assign the value on the right side of the expression to the variable on the left hand side, and the result of this statement is TRUE. Therefore, the loop will never end. Use == to check for equality; furthermore, to avoid accidental assignment, put variables on the left hand side of the expression and you'll get a compiler error if you accidentally use a single equal sign as you can't assign a value to something that isn't a variable. char x='Y'; while('Y'==x) {
//... cout<<"Continue? (Y/N)"; cin>>x;
} Undeclared Functions: int main() {
menu();
} void menu() {
//...
} "Why do I get an error about menu being unknown?"
The compiler doesn't know what menu() stands for until you've told it, and if you wait until after using it to tell it that there's a function named menu, it will get confused. Always remember to put either a prototype for the function or the entire definition of the function above the first time you use the function. void menu(); int main() {
menu();
} void menu() {
...
} Extra Semicolons: int x; for(x=0; x<100; x++);
cout<<x;
"why></x; "why>100?
You put in an extra semicolon. Remember, semicolons don't go after if statements, loops, or function definitions. If you put one in any of those places, your program will function improperly. int x; for(x=0; x<100; x++)
cout<<x;
overstepping x="1;" x<="10;"></x; overstepping>[ten] (brackets added to show grouping) is translatable to !(value==10 || value==20), and when you distribute the !, it becomes !(value==10) && !(value==20).
The proper way to rewrite the program: int value; do {
//... value=10;
}while(!(value==10) && !(value==20))