I'm trying to make a program that reads multiple ISBN-10 codes then calculates the 10th digit for each input, but my issue (at least for now) doesn't seem to be on the calculus itself but on the function that reads the inputs
The function should stop on two occasions: if the input reaches the maximum amount of lines (i=10) or if the current input is 0000000000 (causing a break on the repetition regardless of i)
Following this line of thought I've ended up making isbn_read, but whenever I try to run the program on a compiler the program keeps asking for inputs ignoring BOTH of the conditions I've mentioned above
void isbn_read(int isbn[10][9], int lin )
{
int i, j, cont=0;
for (i = 0; i < 10; i++)
{
for(j = 0; j < 9; j++)
{
scanf("%d", &isbn[i][j]);
if (isbn[i][j] == 0)
cont++;
}
lin = i;
if (cont==9)
{
lin--;
break;
}
}
}
[Some other functions]
int main ()
{
int isbn[10][9], lin, d[10];
isbn_read(isbn, lin);
isbn_dv(isbn, lin, d);
isbn_print(isbn, lin, d);
}
An ideal example for inputs and outputs if the programing was running properly should be something like:
Input 089237010 083063637 000000000
Output 089237010-6 083063637-4
Various fixes and improvements have been suggested in the comments. Below is a working example of code that fits your requirements, written from scratch.
There are many design choices here:
chartypes or (unsigned)inttypes to store the ISBN. This depends on if you want to (mostly) treat the data as a string (opens up string manipulations) or as numbers (for e.g. performing arithmetic). Either way, one is easily converted to the other.scanf("%9s", buffer_of_size_11)) or read one character at a time and convert them to a digit?I hope the code and comments below give you an idea of one possible approach.
Example input/output:
Edit
Here is another, simpler version, which reads and scans the ISBNs as strings:
A character digit such as
5can be converted into the numerical value 5 by "subtracting the character0from it". This is actually subtracting two ASCII values from one-another. E.g.: