ISBN-10 "calculator"

Hello; I am new to programming, and in need of some assistance with a problem in trying to use a string... My error seems to be happening where the calculation for the next digit takes place. Any help would be greatly appreciated.


This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <string>
using namespace std;

int main() {
	// define variables
	string s;
	s.length()-9;

	cout << "Enter the first 9 digits of an ISBN: " << endl;
	cin >> s;

	s.at(9) == ((s.at(0) * 1 + s.at(1) * 2 + s.at(2) * 3 + s.at(3) * 4 + s.at(4) * 5 + s.at(5) * 6 + s.at(6) * 7 + s.at(7) * 8 + s.at(8) * 9)) % 11;

	if (s.at(9) == 10) {
		cout << s.at(0) << s.at(1) << s.at(2) << s.at(3) << s.at(4) << s.at(5) << s.at(6) << s.at(7) << s.at(8) << s.at(9) << "X" << endl;

	}

	else {
		cout << s;
	}

	 
	
	system("pause");
	return 0;

	
}



#################################################################

And just for clarity, this is what I am attempting to do, but by using a string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;

int main() {
	// define variables
	int isbn;
	int d1;
	int d2;
	int d3;
	int d4;
	int d5;
	int d6;
	int d7;
	int d8;
	int d9;
	int d10;
	
	
	//Receive values for variables
	cout << "Enter the first 9 digits of an ISBN as integer:" << endl;
	cout << "ISBN = ";
	cin >> isbn;
	//find d9, d8, d7 ... d1
	d9 = isbn % 10;
	isbn = isbn / 10;
	d8 = isbn % 10;
	isbn = isbn / 10;
	d7 = isbn % 10;
	isbn = isbn / 10;
	d6 = isbn % 10;
	isbn = isbn / 10;
	d5 = isbn % 10;
	isbn = isbn / 10; 
	d4 = isbn % 10;
	isbn = isbn / 10; 
	d3 = isbn % 10;
	isbn = isbn / 10;
	d2 = isbn % 10;
	isbn = isbn / 10;
	d1 = isbn % 10;
	


	d10 = ((d1 * 1) + (d2 * 2) + (d3 * 3) + (d4 * 4) + (d5 * 5) + (d6 * 6) + (d7 * 7) + (d8 * 8) + (d9 * 9)) % 11;

	cout << "The ISBN is: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << d9;
	
	if (d10 == 10) {
		cout << "X ";
	}
	else {
		cout << d10 << endl;
	}

	
	system("pause");

	return 0;
}
s.length()-9; This line calculates a value and does nothing with it. It effectively does nothing. What's it meant to do?

== is a logcal test operation. It does not change any values. So this line:
s.at(9) == ((s.at(0) * 1 + ... is another line that does nothing.
the s.length line was meant to tell the program that the value of "s" should be 10 digits. so that when i cout the IBN, it would have all d1 - d10 when the user imputs 9 of those digits

the s.at(9) was meant to calculate the 10th digit.


so check to see if you just meant = instead of ==. This and the reverse of this are two typos that often compile but either do nothing or do the wrong thing entirely, depending on context.
if (x = y) //this is true unless y is zero. it assigns x to the value of y as a side effect, usually not intended.
if(x==y) //this is true if x and y have the same value. x is not modified.

and the reverse
x == y+2; //evaluates a temporary result of true or false and throws that result away. nothing is actually done here.

your compiler should be set to warn about these. Because it compiles and because its a common typo / mistake, compilers look for it.
Last edited on
Topic archived. No new replies allowed.