0

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;
}
Average kid
  • 6,885
  • 6
  • 21
  • 17

2 Answers2

2

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!

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • oh damn! i can't believe i missed that. Ugh, when I changed int sumNonDigits(int) to (string), it said I can't convert long cardNumber to a string cardNumber – Average kid Feb 25 '13 at 02:03
  • @Averagekid: No, you'll need to call `sumNonDigits()` with a `std::string`. Perhaps `in` will suffice? – johnsyweb Feb 25 '13 at 02:09
  • Um, sorry I don't know much about strings, kinda new to c++. Wat do you mean? – Average kid Feb 25 '13 at 02:19
0

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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252