1

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;
}
HMD
  • 2,202
  • 6
  • 24
  • 37

1 Answers1

5

std::string is an array of characters not integers. and in c++ '1' is not equal to 1.

What you are doing is adding characters ascii codes. what you need to do is change d10 calculation like this :

int d10 = ( (ISBN[0] - '0') * 1 + (ISBN[1] - '0') * 2 + (ISBN[2] - '0') * 3 + (ISBN[3] - '0') * 4 + (ISBN[4] - '0') * 5
 + (ISBN[5] - '0') * 6 + (ISBN[6] - '0') * 7 + (ISBN[7] - '0') * 8 + (ISBN[8] - '0') * 9 ) % 11;

To convert character to actual integer value (I mean '1' -> 1) you need to do something like this :

char a = '1';
int ia = a - '0'; //ia == 1
HMD
  • 2,202
  • 6
  • 24
  • 37
  • Thanks Hamed! It makes sense now. Now to concatenate all I have to do is add the '0' to get the char and append it to the string. I did ISBN+= d10 + '0'. – Luigi Campbell Apr 23 '18 at 22:21
  • @LuigiCampbell Depends on what you need, if you want `1` to be `'1'` yes `+ '0'` is a way but if you want `13601267` to be `"13601267"` you can't simple add `'0'` to it. in this case you can use [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string). – HMD Apr 24 '18 at 01:38
  • Yeah, to convert the integer into a string to concatenate the two elements. I just wanted to use a conditional expression to print out any mod higher than 9 as an X. I did eventually run into this problem on another task but `std:: to_string` made it easier. Thank you! – Luigi Campbell Apr 24 '18 at 02:11