I'm having to write a program for class which verifies if a number is a ISBN-13 number. The formula to check if a number is a ISBN-13 is illustrated here
10 –((1*9+ 3*7+ 1*8+ 3*1+ 1*5+3*8+ 1*0+ 3*8+ 1*9+ 3*2+ 1*7+ 3*9)% 10) == 7 Each of the first 12 digits were multiplied by cycling 1 and 3, then added together, divide the summation by 10, and subtract the remainder from 10.I can't seem to figure out what I'm doing wrong in my code and some help would really be appreciated.
#include <iostream>
#include <string>
usingnamespace std;
string str;
bool is_isbn_13_valid (string str)
{
for (int i = 0; i < str.length(); i++)
{
str[i] = str[i] - '0';
}
for (int x = 0; x < str.length(); x++)
{
if (x%2 == 0)
{
str[x] = str[x]*1;
}
else
{
str[x] = str[x]*3;
}
}
if ((10-((str[0]+str[1]+str[2]+str[3]+str[4]+str[5]+str[6]+str[7]+str[8]+str[9]+str[10]+str[11])%10)) == str[12])
{
returntrue;
}
else
{
returnfalse;
}
}
int main()
{
cout << "Enter a number to check if it is a ISBN-13 number: ";
cin >> str;
is_isbn_13_valid(str);
if (is_isbn_13_valid)
{
cout << "It is a ISBN-13 number";
}
else
{
cout << "It is not a ISBN-13 number";
}
}
No matter what I do the value always returns as true for some reason and I'm not sure why. I think it is because I'm not actually converting the numbers in the string to an integer data type but I don't know how to fix it.