I am missing something from the chapters; I've read them front and back but I think I need some sort of general guidance.
Not allowed to use loops
and I've read through the JAVA and through the Python examples.
I'm supposed to modify my first (top) code to use a string input using getline
then calculate the last digit of an ISBN-10.
With an input of 013601267
, I'm not sure why my output is 5
after the checksum for the 10th digit in my modified code. The value should be 1
.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout
<< "Enter the first nine digits as integerss of an ISBN.."
<<endl;
int d1, d2, d3, d4, d5, d6, d7, d8, d9;
int d10;
cin
>> d1
>> d2
>> d3
>> d4
>> d5
>> d6
>> d7
>> d8
>> d9;
d10 = ( d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9 ) % 11;
if ( d10 > 9)
{
cout
<< "d10: "
<< d10
<< endl
<<"The corresponding ISBN-10 is... "
<< d1
<< d2
<< d3
<< d4
<< d5
<< d6
<< d7
<< d8
<< d9
<< 'X'
<< endl;
}
else
{
cout
<< "d10: "
<< d10
<< endl
<<"The corresponding ISBN-10 is... "
<< d1
<< d2
<< d3
<< d4
<< d5
<< d6
<< d7
<< d8
<< d9
<< d10
<<endl;
}
return 0;
}
Below is modified code and If I'm successful I'm going to concatenate ISBN to d10 but I've left them separate as I was trying to see what the values of the math'd index elements were.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
cout
<< "Enter the first nine digits of an ISBN-10..."
<< endl;
string ISBN;
getline(cin, ISBN, '\n');
int d10 = ( ISBN[0] * 1 + ISBN[1] * 2 + ISBN[2] * 3 + ISBN[3] * 4 + ISBN[4] * 5 + ISBN[5] * 6 + ISBN[6] * 7 + ISBN[7] * 8 + ISBN[8] * 9 ) % 11;
cout
<< d10
<< endl
<< ISBN
<< endl;
return 0;
}