Re: Critique my assignment please




foo.c:24: warning: no previous prototype for `DigitFromChar'
foo.c:44: warning: no previous prototype for `NumberFromUpperLetter'
foo.c:66: warning: no previous prototype for `IsValidEuroSerial'

I realise your compiler is set to warn about this, but I'm happy with
their omission in this program.

foo.c: In function `IsValidEuroSerial':
foo.c:67: warning: unused variable `dummy'

Again I'm happy with this.

foo.c: In function `main':
foo.c:103: warning: implicit declaration of function
`Is_ValidEuroSerial'

Wups a daisy. I'll rewrite the code and post below.

foo.c:95: warning: `output_string' might be used uninitialized in this
function

The surrounding functions prevent that. I'm happy with the setup.

foo.o: In function `main':
foo.c:100: the `gets' function is dangerous and should not be used.

fgets instead, I presume?

foo.c:103: undefined reference to `Is_ValidEuroSerial'
collect2: ld returned 1 exit status
make: *** [foo] Error 1

Here we go:

#include <assert.h> /* For assert */
#include <ctype.h> /* For stuff like isupper */
#include <stdio.h> /* For puts and gets */
#include <string.h> /* For strchr */


#define SERIAL_LEN 12 /* Serial number = one letter followed by
eleven digits */


/* Function: DigitFromChar


Converts '0' to 0, '5' to 5, '3' to 3, etc..


Exploits C89 feature that '0' through '9' must be consecutive.


Release Mode: UNSAFE because behaviour is undefined if input is
invalid
Debug Mode: SAFE because assertion fails if input is invalid
*/


unsigned DigitFromChar(char const x)
{
assert( x >= '0' && x <= '9' );


return x - '0';

}


/* Function: NumberFromUpperLetter

Converts 'A' to 1, 'B' to 2, 'C' to 3... and so on.


Uses "ABCDEF..." and strchr.


Release Mode: UNSAFE because behaviour is undefined if input is
invalid
Debug Mode: SAFE because assertion fails if input is invalid
*/


unsigned NumberFromUpperLetter(char const x)
{
static char const letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


assert(isupper(x));


return strchr(letters, x) - letters + 1;

}


/* Function: Is_ValidEuroSerial

Returns 1 if valid, 0 if invalid, or -1 if syntax error.


Loops through the characters, summing with each iteration.


Release Mode: UNSAFE because behaviour is undefined if pointer is
invalid
Debug Mode: SAFE because assertion fails if pointer is invalid
*/


int Is_ValidEuroSerial(char const *p)
{
int const dummy = ( assert(p), 0 );


char const *const pend = p + SERIAL_LEN;


unsigned sum;


if(!isupper(*p)) return -1;


sum = NumberFromUpperLetter(*p++);


do
{
if(!isdigit(*p)) return -1;
sum += DigitFromChar(*p++);
} while (pend != p);


if (*pend) return -1;


if (8 == sum%9) return 1;
else return 0;



}


int main(void)
{
char input[SERIAL_LEN + 1];

char const *output_string;


puts("Enter Euro banknote serial number: ");


fgets(input,sizeof input / sizeof *input, stdin);


switch (Is_ValidEuroSerial(input))
{
case -1: output_string = "\n\nInvalid Input. Input must consist "
"of an uppercase letter followed by "
"eleven digits only.\n";
break;


case 0: output_string = "\n\nINVALID\n";


break;


case 1: output_string = "\n\nValid\n";


break;
}


puts(output_string);


return 0;

}

.



Relevant Pages

  • Re: Critique my assignment please
    ... Debug Mode: SAFE because assertion fails if input is invalid ... unsigned NumberFromUpperLetter(char const x) ... "eleven digits only.\n"; ...
    (comp.lang.c)
  • Critique my assignment please
    ... We were issued with an assignment a yesterday. ... Debug Mode: SAFE because assertion fails if input is invalid ... unsigned NumberFromUpperLetter(char const x) ...
    (comp.lang.c)
  • Re: Macys Parade - Unicycling Video Clip
    ... I think the whole gallery is messed up.. ... Warning: Invalid argument supplied for foreach() in ...
    (rec.sport.unicycling)
  • Re: HELP! Trying to add Email Notification to Download File Code
    ... This warning will go away once you get the headers sorted out. ... Invalid argument supplied for foreachin ... # verifies if the $filelist array has an element ... # builds an array with all valid files in the ...
    (comp.lang.php)
  • Re: Critique my assignment please
    ... out for themselves how to check the validity of a Euro bank note ... Debug Mode: SAFE because assertion fails if input is invalid ...
    (comp.lang.c)