LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2005, 12:29 PM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
I give up, here is my code, please help


I'm posting the code of my program and the messages the compiler gives. I spent almost 2 days to find the problems, but couldn't, I don't understand arrays and pointer well.

Code:
#include <stdio.h>
#include <string.h>

#define BUFSIZE 256
#define TRUE 1
#define FALSE 0

char getISBN(char ISBN[]);

char checkDigitFromISBN(const char ISBN[]);

void getPartISBNFromISBN(char PartISBN[], const char ISBN[], unsigned nChars);

unsigned digitFromCharacter(char ch);

char characterFromDigit(unsigned digit);

char calculateCheckDigit(const char PartISBN[]);

int checkDigitAreEqual(char claculatedCheckDigit, char theCheckDigit);

int main(void)
{
  char string[BUFSIZ], part[BUFSIZ], isbn[BUFSIZ], checkDigit[BUFSIZ], calculate[BUFSIZ];
  
  strncpy(isbn, getISBN(string), 11);
  
  strncpy(checkDigit, checkDigitFromISBN(isbn), 2);
  
  getPartISBNFromISBN(part, isbn, 10);
  
  strncpy(calculate, calculateCheckDigit(part), 2);
  
  if(checkDigitAreEqual(calculate, checkDigit) == 0)
    printf("The number entered is not a valid ISBN\n");
  else if(checkDigitAreEqual(calculate, checkDigit) == 1)
    printf("ISBN is OK\n"); 
}

char getISBN(char ISBN[])
{   
  printf("Enter the ISBN number: ");
  fgets(ISBN, BUFSIZE, stdin);
  return ISBN;
}
  
char checkDigitFromISBN(const char ISBN[])
{
  return ISBN[9];
}
   
void getPartISBNFromISBN(char PartISBN[], const char ISBN[], unsigned nChars)
{
  strncpy(PartISBN, ISBN, nChars);
}

unsigned digitFromCharacter(char ch)
{
  return (unsigned)ch - '0';
}

char characterFromDigit(unsigned digit)
{
  return (char)digit + '0';
}

char calculateCheckDigit(const char PartISBN[])
{
  unsigned i = 0u;
  unsigned integer;
  unsigned weight = 10u;
  unsigned partSum;
  unsigned total;
  unsigned remainder;
  unsigned check;
  char character;
     
  while(PartISBN[i] != NULL)
  {
     integer = digitFromCharacter(PartISBN[i]);
     partSum = integer * weight;
     i++;
     weight--;
     total = total + partSum;
  }
  
  remainder = total % 11;
  
  check = 11 - remainder;
  
  return character = characterFromDigit(check);
}
 
int checkDigitAreEqual(char calculatedCheckDigit, char theCheckDigit)
{
  if(calculatedCheckDigit == theCheckDigit)
    return TRUE;
  else
    return FALSE;
}
COMPILER MESSAGES:
[root@dhcppc2 prog]# cc -o prog prog6.c
prog6.c: In function `main':
prog6.c:26: warning: passing arg 2 of `strncpy' makes pointer from integer without a cast
prog6.c:28: warning: passing arg 2 of `strncpy' makes pointer from integer without a cast
prog6.c:32: warning: passing arg 2 of `strncpy' makes pointer from integer without a cast
prog6.c:34: warning: passing arg 1 of `checkDigitAreEqual' makes integer from pointer without a cast
prog6.c:34: warning: passing arg 2 of `checkDigitAreEqual' makes integer from pointer without a cast
prog6.c:36: warning: passing arg 1 of `checkDigitAreEqual' makes integer from pointer without a cast
prog6.c:36: warning: passing arg 2 of `checkDigitAreEqual' makes integer from pointer without a cast
prog6.c: In function `getISBN':
prog6.c:44: warning: return makes integer from pointer without a cast
prog6.c: In function `calculateCheckDigit':
prog6.c:78: warning: comparison between pointer and integer

Last edited by hubabuba; 01-29-2005 at 12:31 PM.
 
Old 01-29-2005, 01:12 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
OK. I was not able to understand what each function is supposed to do. (or more honestly, I didn't feel like figuring it all out from your code).

So I fixed the code up so it compiles without errors. But I think my "fixing" will have also destroyed the way it's supposed to work. Especially the supossed result of the function "calculateCheckDigit()" was quite unclear to me. Sorry to say it this way, but your code has quite a few mistakes, and my "fixed" version still has most of them. It didn't feel like figuring out how ISBN-numbers are formatted and how your program tries to check them.

But now at least it compiles without errors. Hope I could help a litle this way.
Most of the errors had to do with your code assuming a char is the same as a char* (i.e. a string), which is not the case (in C) and resulted in compiler errors.

Code:
#include <stdio.h>
#include <string.h>

#define BUFSIZE 256
#define TRUE 1
#define FALSE 0

char *char2str(char c);
char *getISBN(char ISBN[]);
char *checkDigitFromISBN(const char ISBN[]);
void getPartISBNFromISBN(char PartISBN[], const char ISBN[], unsigned nChars);
unsigned digitFromCharacter(char ch);
char characterFromDigit(unsigned digit);
char *calculateCheckDigit(const char PartISBN[]);
int checkDigitAreEqual(char claculatedCheckDigit, char theCheckDigit);

int main(void)
{
    char string[BUFSIZ], part[BUFSIZ], isbn[BUFSIZ], checkDigit[BUFSIZ],
        calculate[BUFSIZ];

    strncpy(isbn, getISBN(string), 11);
    strncpy(checkDigit, checkDigitFromISBN(isbn), 2);
    getPartISBNFromISBN(part, isbn, 10);
    strncpy(calculate, calculateCheckDigit(part), 2);

    if (checkDigitAreEqual(calculate[0], checkDigit[0]) == 0)
        printf("The number entered is not a valid ISBN\n");
    else if (checkDigitAreEqual(calculate[0], checkDigit[0]) == 1)
        printf("ISBN is OK\n");

    return 0;
}

char *char2str(char c)
{
    /* Dirty hack, but works in this case. */
    static char str[2];
    str[0] = c;
    str[1] = '\0';
    return str;
}

char *getISBN(char ISBN[])
{
    printf("Enter the ISBN number: ");
    fgets(ISBN, BUFSIZE, stdin);
    return ISBN;
}

char *checkDigitFromISBN(const char ISBN[])
{
    return char2str(ISBN[9]);
}

void getPartISBNFromISBN(char PartISBN[], const char ISBN[], unsigned nChars)
{
    strncpy(PartISBN, ISBN, nChars);
}

unsigned digitFromCharacter(char ch)
{
    return (unsigned) ch - '0';
}

char characterFromDigit(unsigned digit)
{
    return (char) digit + '0';
}

char *calculateCheckDigit(const char PartISBN[])
{
    unsigned i = 0u;
    unsigned integer;
    unsigned weight = 10u;
    unsigned partSum;
    unsigned total;
    unsigned remainder;
    unsigned check;
    /* char character; */  /* removed */

    while (PartISBN[i] != '\0') {
        integer = digitFromCharacter(PartISBN[i]);
        partSum = integer * weight;
        i++;
        weight--;
        total = total + partSum;
    }

    remainder = total % 11;

    check = 11 - remainder;

    return char2str(characterFromDigit(check));
}

int checkDigitAreEqual(char calculatedCheckDigit, char theCheckDigit)
{
    if (calculatedCheckDigit == theCheckDigit)
        return TRUE;
    else
        return FALSE;
}

Last edited by Hko; 01-29-2005 at 01:14 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
C++ code,please C++ programer to give me some advise naihe2010 Programming 13 10-17-2005 10:30 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
What is the best place to give away C++ code? ta0kira Programming 13 05-19-2005 12:58 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration