testing validity of ISBN

Hello, I am having trouble with a homework assignment where I need to check if the ISBN code is valid. This is a beginner C++ class, and I am new to all of this. Right now, no matter what ISBN I put in, it tells me they are all valid. If someone could help point me in the right direction to figure out if my formula is correct when in my for() statement.

I have no problem with the outside loops, it's just the conversion I believe I am doing wrong.

being this is an intro c++ class, we are still only using basic functions, so the professor suggested reading in the ISBN and storing it under a char variable.

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
  
	int Counter;
	int WeightedSum;
	int WeightedNumber;
	char ISBN{};
	char Choice;
	char Again{};
	int Sum = 0;
	const char X = 10;
	int IterationCounter = 0;
	


	do // begininng of do/while loop
	{
		cout << "Would you like to check if your ISBN is valid or not? ";
		cout << "Press Y/y for yes or N/n for no and to end program: ";
		cin >> Choice; //choice input
		cout << endl << endl; // blank line 
		while (Choice != 'Y' && Choice != 'y' && Choice != 'N' && Choice != 'n') // beginning of while loop. checks user input to see if valid y/n choice
		{
			cout << "Invalid choice! Enter either Y/y or N/n"; // displays when anything other than y/n is entered 
			cout << "Press Y/y for yes or N/n for no and to end program: "; //gives user another chance to enter y/n
			cin >> Choice;
			cout << endl << endl;
		}
		if (Choice == 'Y' || Choice == 'y')
		{
			cout << "Enter the ISBN you wish to convert: " << endl;
			cin >> ISBN;
			cout << endl << endl;

			for (Counter = 0; Counter < 10; Counter++)
			{
				WeightedSum = ISBN * (10 - Counter);
				Sum = WeightedSum + Sum;

			}
			cout << Sum;
			if (Sum % 11 == 0)
			{
				cout << Sum;
				cout << " Is a valid ISBN " << endl;
			}
			else 
			{
				cout << Sum;
				cout << " Is invalid " << endl;
			}
			
			cout << endl << endl;
			IterationCounter = IterationCounter++; // increments counter after each conversion is done
			cout << "Conversions Done: " << IterationCounter << endl; // displays the conversion counter 
			cout << endl << endl;
			cout << "#########################################################################################################################" << endl;
			cout << endl << endl;
		}
Do you realize that ISBN is a single character?

You're trying to use a single char variable to store a lot of numbers This makes no sense.

char ISBN{};

This is ONE char. You can store a single char in it. Just one.

So if your ISBN is more than one digit, this is totally unsuitable.


That said, you're only using one char anyway.

1
2
3
4
5
6
	for (Counter = 0; Counter < 10; Counter++)
			{
				WeightedSum = ISBN * (10 - Counter);
				Sum = WeightedSum + Sum;

			}


ISBN never changes. If you're ISBN value is 1234123412 , aren't you meant to do a calculation using the 1, and then a calculation on the 2, and so on? You're not doing that.

Honestly, delete it all. Start again. Break it into tiny pieces. Get each piece correct before you move on. This is just a big bag of mistakes.

Don't use a char. That makes no sense. How can you store a ten digit ISBN in a single char?

the professor suggested reading in the ISBN and storing it under a char variable.
I suspect he misspoke, or you completely misunderstood.

I suggest the first thing you do is get the ISBN from the user, and print it on screen. So you know you're actually taking it as input.
Last edited on
Another prof telling you not to use a std::string... foozle.

An ISBN number is either 10 or 13 digits, so you need at minimum fourteen characters to store it.

 
  char ISBN[14] = { 0 };

To input the ISBN number, make sure you are careful:

1
2
3
  std::cin >> std::ws;
  std::cin.get( ISBN, 14 );
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

Once you have that, you can check to see how long the string is and act accordingly:

1
2
3
4
5
6
  switch (std::strlen( ISBN ))
  {
    case 10: check_10_digit_ISBN( ISBN ); break;
    case 13: check_13_digit_ISBN( ISBN ); break;
    default: complain();
  }

Hope this helps.
Last edited on
thank you for your help. It's not that he doesn't want us to use it. its that we are not at that point in the course yet.
It's not that he doesn't want us to use it. its that we are not at that point in the course yet.


It is what it is. std::string should come before char arrays. strings are for beginners. They work how beginners expect and do what beginners need. They should come first, but many professors simply teach what they learned themselves thirty years previously; basically, C with a few extra features.
yea guess thats what it is. do you have any other pointers I could use? i
In case it isn't clear: Duthomhas' code does not use std::string. It's a valid way to get user input as a char array, and store it into the ISBN char array.

To get a particular digit of this char array string as a number, you can do

int digit = static_cast<int>(ISBN[i] - '0');

1
2
3
4
5
6
7
8
9
10
11
12
// Example program
#include <iostream>
#include <cstring>
int main()
{
    char ISBN[14] = "1234567890123";
    for (size_t i = 0; i < std::strlen(ISBN); i++)
    {
        int digit = static_cast<int>(ISBN[i] - '0');
        std::cout << digit << ' ';
    }
}
Last edited on
Topic archived. No new replies allowed.