CSCI 261 - Programming Concepts - Fall 2020

Common Errors and Solutions

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


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);

[Top]

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;
}

[Top]

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) {
    ...
}

[Top]

for, while, or do-while loop?

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.
[Top]

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;
}

[Top]

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 argument spot in the call to myFunction(), where the parameter is internally named x.

x: 10 y: 6

[Top]

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;
}

[Top]

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;
}

[Top]

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.


[Top]

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.


[Top]


[Top]

CLion is giving me a really weird error that doesn't make sense or one that I've already fixed
Sometimes CLion 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" to have CLion 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 CLion. 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.

[Top]

I'm still having technical issues especially with CLion not finding files or something else really odd.
Sometimes (very rarely) something really odd happens that makes CLion 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 CLion just not recognizing these changes.