Checking ISBN numbers

Read from an input file and check the checksum digits of the isbn numb

Page 1 of 1

1 Replies - 11960 Views - Last Post: 13 March 2009 - 05:00 PM Rate Topic: -----

#1 atsprink   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-February 09

Checking ISBN numbers

Posted 13 March 2009 - 04:58 PM

I am trying to write a program that checks the checksum digit of the ISBN number for 10 and 13 digits and validates the ISBN number. Was hoping for some hints on what I might be doing wrong. Thanks

/*
 * Author:Taylor Sprinkle
 * Date: March 13, 2009
 * Description: Read in a text file and determine if the ISBN
	checksum is valid, or if the ISBN number is in the 		wrong format.  If there is a problem display an error 		message.
 *
 */
#include "books_a_zillion.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#ifdef BOOKS_A_ZILLION_BUILD

/* This region between the #ifdef and #else must remain empty
   or it will generate link errors when  we test your code
*/

#else

/* Use this for testing. Do not move this definition */
/* See books_a_zillion.h for possible return values  */
int books_a_zillion_display(char *isbn, int digits)
{
  /* Enable both conditions to test your code */

  /*  return BAZ_ERROR; */
  return BAZ_SUCCESS;
}

#endif

//prototype of check isbn fcn

_Bool validate(char *ptr2 );
_Bool checksum(char *ptr);
const int max = 20;

/* Implement the rest of your code here */
int main(int argc, char* argv[])
{
	//declarations
	int linec= 0;
	int x=0;
	char num[20];
	FILE* in;
	//check to see if filename given
	if(argc != 2)
	{
		printf("Must enter filename\n");
		return EXIT_FAILURE;
	}
	//read in the input file
	in = fopen(argv[1], "r");
	//check to see if it is a correct file
	if(in == 0)
	{
		printf("Invalid file name\n");
		return EXIT_FAILURE;
	}
	//read in data until end of file reached
	while((x= fgets(num,max,in)) != EOF)
	{
		linec++;
		if(num[0] == '\n')
			continue;
		// validate call
		int isbnLen = 0;
		if( validate( num ) )
		{
			int errorCode = 0;
			// Count valid ISBN's need another counter
			// Call the Book-aZillion function
			errorCode = books_a_zillion_display( num, strlen(num) );

			if( errorCode == BAZ_ERROR )
			{

			}
		}
		else
		{
			printf( "Error. %s Line#: %d ISBN is invalid.\n", num, linec);
		}


	}
	return 0;
}

// fscanf( in, " %[0-9] ", num )

//validate implemented
_Bool validate(char *ptr2 )
{
	_Bool retVal = false;
	char *temp = ptr2;

	// Get rid of white space and dashes
	// Don't forget to check for 'X
	for( int i = 0; i<strlen(ptr2); i++)
	{
		if( isdigit( ptr2[i] ) )
		{
			*temp = ptr2[i];
			temp++;
		}
	}

	// Null terminate the string again.
	*temp = 0x00;

	retVal = checksum( ptr2 );

	return retVal;
}
//checksum implemented
_Bool checksum(char *ptr)
{
	_Bool retVal = false;

	if( strlen(ptr) == 13 )
	{
		
		// Perform the 13 bit Checksum
		*ptr[12] == ([10-([ptr[0] + 3*ptr[1] + ptr[2] + 3*ptr[3] + ptr[4] + 3*ptr[5] + ptr[6] + 
			   3*ptr[7] + ptr[8] + 3*ptr[9] + ptr[10] + 3*ptr[11]]%10)%10]);
		// Check against last digit
		if(*ptr[12] == "%[0-9]")
			retVal = true;
		// Validate the first three digits
		if((*ptr[0]== 9 && (*ptr[1]== 7) && (*ptr[2] == 8||9)
			continue;
		else
			retVal = false;
	}
	else if( strlen( ptr ) == 10 )
	{
		
		//Perfrom checksum
		*ptr[9] == (ptr[0] + 2*ptr[1] + 3*ptr[2] + 4*ptr[3] + 5*ptr[4] +
			  6*ptr[5] + 7*ptr[6] + 8*ptr[7] + 9*ptr[8])%11;
		
			
		// Validate the sum
		if(*ptr[9] == "%[0-9]")
			retVal = true;
		if(*ptr[9] == 'X')
			retVal = true;
		else
			retVal = false;
			
	}

	return retVal;
}

/*int Perform13Checksum()
{
	
}

char Perform10Checksum()
{
	unsigned char checkVal = 0;

	return checkVal;
}

}

*/



compiler errors:
cc1: warnings being treated as errors
isbn.c: In function :
isbn.c:62: warning: assignment makes integer from pointer without a cast
isbn.c:74: warning: implicit declaration of function
isbn.c:74: warning: incompatible implicit declaration of built-in function
isbn.c:68: warning: unused variable
isbn.c: In function :
isbn.c:96: error: undeclared (first use in this function)
isbn.c:96: error: (Each undeclared identifier is reported only once
isbn.c:96: error: for each function it appears in.)
isbn.c:101: error: loop initial declaration used outside C99 mode
isbn.c:101: warning: incompatible implicit declaration of built-in function
isbn.c: In function :
isbn.c:120: error: undeclared (first use in this function)
isbn.c:122: warning: incompatible implicit declaration of built-in function
isbn.c:126: error: invalid type argument of
isbn.c:126: error: expected expression before token
isbn.c:129: error: invalid type argument of
isbn.c:130: error: undeclared (first use in this function)
isbn.c:132: error: invalid type argument of
isbn.c:132: error: invalid type argument of
isbn.c:132: error: invalid type argument of
isbn.c:133: error: expected before
isbn.c:136: error: expected before token
isbn.c:136: error: expected expression before token
isbn.c:141: error: invalid type argument of
isbn.c:146: error: invalid type argument of
isbn.c:148: error: invalid type argument of

This post has been edited by atsprink: 13 March 2009 - 05:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Checking ISBN numbers

#2 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Checking ISBN numbers

Posted 13 March 2009 - 05:00 PM

Post any compiler errors, that might help.
EDIT: Welcome to DIC.

This post has been edited by Hyper: 13 March 2009 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1