Average kid
Average kid

Reputation: 7403

random 1 unresolved external error

So i am getting a weird error message in my c++ program. Currently using visual studios (2012). I have a program which adds every other digit of a number, so 1234567 would be like 7+5+3+1=16, then I take all the non added numbers and multiply em by two and add em up. Then I add up the result of the first one (16) and add it to the result of the second one. Here's my code:

#include <iostream>
#include <cmath> 
#include <string>
#include <sstream>

using namespace std;


int sumAltDigits(int);
int sumNonDigits(int);

int main() {
    long cardNumber; //cardNumber must stay as 'long'. Teacher says so.
    string in;
    stringstream ss;
    int total;

    cout << "Please enter a chain of integers: ";
    getline(cin, in);
    ss.clear(); ss.str(in);
    while (!(ss >> cardNumber) || (cardNumber < 1)); {
        cout << sumAltDigits(cardNumber) << endl;
        //get answer
        total = sumAltDigits(cardNumber) + sumNonDigits(cardNumber); //this line causes me an error, sumNonDigits(cardNumber)
    }
    system("pause");
}

// adds every other digit, starting from the right
int sumAltDigits(int cardNumber)
{
    if (cardNumber < 10) 
        return cardNumber;
    return (cardNumber % 10) + sumAltDigits(cardNumber / 100);
}

// adds digits that were not included in previous step, multiply them by 2, then add all digits in those numbers
int sumNonDigits(string cardNumber) // I think the error is also being caused by string cardNumber, but if i try to change that, it screws up this function.
{
    int checkSum = 0;
    int i;
    for (i = cardNumber.length() - 2; i >= 0; i -= 2) {
        int val = ((cardNumber[i] - '0') * 2);
        while (val > 0) {
            checkSum += (val % 10);
            val /= 10;
            cout << checkSum << endl;
        }
    }
    return checkSum;
}

Upvotes: 0

Views: 81

Answers (2)

johnsyweb
johnsyweb

Reputation: 141868

You've forward declared (and called):

int sumNonDigits(int);

But you've defined:

int sumNonDigits(string cardNumber)

You'll need to change one to match the other.


If you change them both to be:

int sumNonDigits(string cardNumber)

This is likely to mean less work but you will need to change the call here:

total = sumAltDigits(cardNumber) + sumNonDigits(cardNumber);

...to pass in a [std::]string, rather than cardNumber, which is long. Perhaps the input string in would be a good substitution, or perhaps you need to convert cardNumber back to a string. Only you can choose!

Upvotes: 2

Tony Delroy
Tony Delroy

Reputation: 106206

You have a int sumNonDigits(string cardNumber) but declare int sumNonDigits(int);. You are calling sumNonDigits(int) in the line...

total = sumAltDigits(cardNumber) + sumNonDigits(cardNumber);

...but it's an unresolved external because there's no definition.

I recommend avoiding function declarations altogether for now, and putting your function bodies above their first point of use.

Upvotes: 0

Related Questions