0
votes

This program was written in our book and we were told to compile it in both C and C++ although it wont work for both. the error below was when i tried to compile it in .cpp format

first error: LNK2019: unresolved external symbol "long __cdecl oddnumber(short)" (?oddnumber@@YAJF@Z) referenced in function "int __cdecl evennumber(int)" (?evennumber@@YAHH@Z) second error: LNK1120: 1 unresolved externals

// This program shows function and variable declarations and their scopes.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include<iostream.h>
int keven = 0, kodd = 0;
long evennumber(short);
long oddnumber(short);
int even(int);
int evennumber(int a) {
    // genuine declaration
    if (a == 2) {
        printf("keven = %d, kodd = %d\n", keven, kodd);
        return keven;
    }
    else {
        a = (int) a / 2;
        if (even(a)) {
            // Is a even?
            keven++;
            return evennumber(a);
        }
        else {
            kodd++;
            return oddnumber(a);
        }
    }
    // return a;
}
int oddnumber(int b) {
    // genuine declaration
    if (b == 1) {
        printf("keven = %d, kodd = %d\n", keven, kodd);
        return kodd;
    }
    else {
        b = 3 * b + 1;
        if (!even(b)){
            //Isbodd?
            kodd++;
            return oddnumber(b);
        }
        else {
            keven++;
            return evennumber(b);
        }
    }
    // return b;
}
int even(int x) {
    // % is modulo operator.
    return ((x % 2 == 0) ? 1 : 0);
}
void main() {
    register short r = 0;   // a register type variable is faster,
    int i = r;  // it is often used for loop variable
    float f;
    for (r = 0; r < 3; r++) {
        printf("Please enter an integer number that is >= 2\n");
        scanf("%d", &i);

        if (even(i))
            f = evennumber(i);
        else
            f = oddnumber(i);
    }
}
1

1 Answers

1
votes

The linker indicates that you declared a symbol long evennumber(short);, but can not find the definition. First review the signatures of your functions: the declaration and definition must match! For instance I think you wanted to declare int oddnumber(int) instead?

Be careful in case you are defining several functions with the same name, but different signature. This is allowed in C++ but not in C.