Undeclared Variables: "Huh? Why Do I Get An Error?"
Undeclared Variables: "Huh? Why Do I Get An Error?"
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; } "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.
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. To fix this error, move the addition step 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;
5. 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.
6. Extra Semicolons
int x; for(x=0; x<100; x++); cout<<x; "Why does it output 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;
have to be true so that the negation of each would be false in order to make the || operation return false. In fact, the statement given above is a tautology; it is always true that value is not equal to 10 or not equal to 20 as it can't be both values at once. Yet, if the intention is for the program only to loop if value has neither the value of ten nor the value of 20, it is necessary to use && : !(value==10) && !(value==20), which reads much more nicely: "if value is not equal to 10 and value is not equal to 20", which means if value is some number other than ten or twenty (and therein is the mistake the programmer makes - he reads that it is when it is "this" or "that", when he forgets that the "other than" applies to the entire statement "ten or twenty" and not to the two terms - "ten", "twenty" individually). A quick bit of boolean algebra will help you immensely: !(A || B) is the equivalent of !A && !B (Try it and see; you can read more about this rule on Wikipedia: DeMorgan's Law). The sentence "value is other than [ten or twenty]" (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))