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