CSCI 261 - Programming Concepts (C++)Fall 2017 - Common Errors and SolutionsQuick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks |
|
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | |
If your code isn't compiling or working as expected and you're
having trouble finding the problem, check through this page to see if you're having one of these common issues. Examples
of bad code are in red containers and the solutions or good examples are in green containers.
I hope this will help with these issues. As always try to start your assignments early so you can go to office hours or tutoring if needed.
Stick with programming through the frustration and you'll find it can be a great feeling to finally finish a big project that works well.
If there is something you feel needs to be added to this, or you see an error, please let me know. Send me an email at dscarbro@mymail.mines.edu. I am one of the Tuesday night tutors (my name is Daniel) so you can also let me know then. Again I hope this can be a helpful resource! -Daniel CSCI 261 Common Errors and Solutions
Early issues:
These issues are common in the beginning of cpp and header files. Make sure you are importing all libraries and header files that you need (libraries use <> and user-written header files use double quotations "") and that you are using namespace std. Only namespace requires a semicolon. Function prototypes should be ended with a semicolon if they are being used.
// Libraries
#include <iostream> #include <string> using namespace std; // User-Written Header Files #include "myHeader.h" // Function prototypes if being used void ExampleFunction (int num, double x, char input);
My variables aren't what I think they should be, or they are giving me weird errors.
This could happen for a few reasons. First make sure your variables are being declared at the beginning of main or the function they are in. Also, make sure the variable is only being declared (i.e has a type in front) once. Examples of what not to do:
int x = 6;
cout << "Initial x: " << x << endl; // x is already declared, int type declaration is not needed here int x = 7; cout << "New x: " << x << endl; Solution:
int x = 6;
cout << "Initial x: " << x << endl; x = 7; cout << "New x: " << x << endl;
Do not use variables without declaring them! If you don't declare each varialbe with a type, it will not work. If the variable
is within a function, make sure it is either passed in as a parameter or it is declared in the function.
Examples:
int main() {
cout << "Here x will be used without a declaration" << endl; x = 7; cout << x << endl; return 0; } Solution:
int main() {
int x; cout << "Here x will be used with a declaration" << endl; x = 7; cout << x << endl; return 0; }
My if-else statements aren't behaving like I think they should.
The most common error I see when this happens is using the assignment "=" operator rather than the equality "==" operator. Using = tells C++ to assign the variable with what you're trying to compare it to, which will yield odd behavior. Make sure you use ==. If you are using other comparisons, double check your logic to make sure you didn't use <= when you meant >=. Example:
if (x = 7) {
... } Solution:
if (x == 7) {
... }
Knowing which type of loop to use is an essential skill. Here are some basic rules of thumb:
1. If you know exactly how many times a loop should run, use a for loop. 2. If the loop should run over and over until a condition is met, use a while loop 3. If the loop should run until a condition is met, but must run at least once, use a do-while loop
Help with functions
Know your function's return type! Should this function calculate something and return a single value? Or should it do some things and change parameters passed by reference or just print things? For the first one you will have a return type like int, double, or string to name a few. For the latter you would use a void type. You can put functions within cout statements IF they have a returned value (something that can be printed). This means do not put a void return type function within cout. Example:
void MyFunction() {
return; } int main () { cout << "My function returns: " << MyFunction() << endl; } Solution:
int MyFunction() {
return 4; } int main () { cout << "My function returns: " << MyFunction() << endl; }
Additonally, don't forget the parentheses when you call a function! Otherwise cout will print something
strange like 0x87b34a06 (which is the memory address that the function is stored in).
Example:
int MyFunction() {
return 4; } int main () { cout << "My function returns: " << MyFunction << endl; } Solution:
int MyFunction() {
return 4; } int main () { cout << "My function returns: " << MyFunction() << endl; }
Input Parameters
For input parameters, it is important to know the relationship between your parameters in your definition and when you call the function. The order of the parameters is important. If your definition has 2 int parameters followed by 2 double parameters, when you call the function you will need to put in 2 ints then 2 doubles. For example:
void MyFunction(int x, int y, double a, double b) {
... } int main () { // Two ints then two doubles are input, as is the order in the definition above MyFunction(20, 30, 6.4, 5.2) } Keep in mind that the order is the key to how a variable is used in the definition and not the name. In the example below
I defined two input parameters of type int. They are named x and y. If I call the function using variables of the same name their order
will affect how they are used in the function. So if x is the second parameter input, then it will be assigned the name y in the function
but has the same actual value.
See below:
void MyFunction(int x, int y) {
cout << "x: " << x << "y: " << y << endl; } int main () { int x = 6; int y = 10; MyFunction(y, x) } This would output the following because y in main is put into the first spot in MyFunction, where it is internally named x.
x: 10 y: 6
I need a function to change multiple values, but only one item can be returned in a function.
This means you want to use a void function that has parameters passed in by reference. Normally parameters that are passed into the function (not by reference) are stored in memory in a location specific to the scope of the function. This means changing it within the function will not change it where it was called (in main for example). This is solved by passing the parameters in by reference. When this is done, the function will then "point" to the variable where it was called. (You'll learn more about this "pointing" much later). Here's an example where I want to change x, y, and z all within one function.
void MyFunction(int& x, int& y, int& z) {
x = x * x; y = y + x; z = x + y + z; return; } int main () { int x = 2; int y = 3; int z = 5; cout << "Initial x:" << x << " y:" << y << " z:" << z << endl; MyFunction(x, y, z); cout << "Final x:" << x << " y:" << y << " z:" << z << endl; } Output:
Initial x:2 y:3 z:5
Final x:4 y:7 z:16
If the data is related, another option could be to return a struct from the function.
struct Point { double x, y, z; }; Point MyFunction(int a, int b, int c) { Point p; p.x = a * a; p.y = b + a; p.z = a + b + c; return p; }
My function always returns the same random value.
Make sure that you are seeding the random number generator - and not inside the function. The following code will generate the same random number: int RandNum() { srand( time(NULL) ); return rand() % 100; } int main() { for( int i = 0; i < 10; ++i ) { cout << RandNum() << endl; } return 0; }
The computer can run the function faster than time can update, so the same time is used to seed the RNG every time. Instead, we will want to seed the RNG as the first step of our main function:
int RandNum() { return rand() % 100; } int main() { srand( time(NULL) ); for( int i = 0; i < 10; ++i ) { cout << RandNum() << endl; } return 0; }
My code isn't working and I've been looking at it forever and can't see what's wrong :(
When your code begins to get more complex and the processes are long, it can be really difficult to tell what is going wrong at this point. The best strategy you can use here is to use cout to print things throughout the process. If you can't tell if an if else statement is working correctly you can print "True" in the if statement, and "False" in the else statement (or some other message to yourself). Print the iteration number in loops to see how it's moving through (or if it's infinite). Print variables out to check their values after various steps. Here is an example of various places to put cout to check what's going on. Think about all the places you can print something to help you track the program's progress.
int MyFunction (int x) {
cout << "My function called" << endl; x += 2; cout << "x changed to: " << x << " within MyFunction" << endl; return x; } int main() { int a = 0; for (int i = 0; i < 5; i++) { cout << "Iteration " << i << "of the for loop" << endl; a = MyFunction(a); cout << "a after function call: " << a << endl; } cout << "For loop completed" << endl; } Some of these may seem redundant, but you might be surprised to find when a function can produce an unexpected result.
This assignment is so big and overwhelming I don't even know how to start.
First, take a deep breath. A lot of projects can seem like a crazy amount of work that has to fit together. If you look through what functionalities you need, you will often find separate chunks you can break the program into. Think of these as mini projects and work on them individually. For example: I know that my program needs to print a menu. So before I worry about what the options do, I'll design the menu and make sure the user is able to make choices. I can just have the choices trigger a simple cout statement for confirmation until the methods are implemented. Then once I have that done, I can design and build each method in the menu one at a time. This also means I can test them one at a time and make sure one works before moving on to the other. This makes debugging much easier. When you write the entire program and compile to find 100 errors it can be very discouraging. Incremental building and testing is going to make these programs much easier to manage.
I'm stuck on this one part and I can't get help on it for a while, I can't do anymore work!
If you get stuck like this, don't think you're completely stuck. Move on from the method or section you're stuck on and work on designing the other sections. Even if these are reliant on what you're stuck on, you can still design and write them. If they require some input from the part you're stuck on, just input a test value that you know is a possible result and see if your function can handle that. By doing this you will be able to keep making progress beyond what you're stuck on until you can get help or find the issue.
My header or cpp file inclusions aren't working, or Visual Studio is giving an unresolved external symbol error.
When working with many files, you have to make sure they are all within the directory that you are working in. This means that they are within the folder that your Visual Studio project is in on your computer's hard drive. Sometimes you might add an existing file but it doesn't get put into the folder you're working in and it won't work. Know where your projects are being saved and make sure all of the files are there. Ask an instructor or tutor if you're unsure about any of this as it will be very important as we begin to use more files in our projects.
Visual Studio is giving me a really weird error that doesn't make sense or one that I've already fixed
Sometimes Visual Studio just goofs up. It happens. The standard computer fix of "did you try turning it off and on again?" applies. As a first attempt, from the Build menu select "Clean Solution" to have Visual Studio delete any temporary files it has created. Then build your solution again. As a last resort, make sure all of your work is saved, and restart Visual Studio. It can be surprising how often this might fix a technical issue. This only works for those very specific instances though, so always check your syntax and logic first before you go for the restart.
I'm still having technical issues especially with Visual Studio not finding files or something else really odd.
Sometimes (very rarely) something really odd happens that makes Visual Studio just not work with your project. If you get very desparate sometimes creating a new project in a new folder can help. Add new files from scratch within this project (yes blank new files). Once you are sure you have all of the files within your new project. Copy the code from the old project. This is another one of those issues with files not being in the right place or Visual Studio just not recognizing these changes. | |
Last Updated: 08/09/17 15:01
|