Thread: I need comments and answers for this C program

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    9

    I need comments and answers for this C program

    Please guys can you read this code and give your comments? It's for a Library Cataloging System Program that use Encryption and Decryption

    Code:
    #include <stdio.h>
    
    // The Maximum Length of BookName is 100 Characters
    #define MAXL_BOOKNAME	100
    
    // The Maximum Length of BookAuthor is 50 Characters
    #define MAXL_BOOKAUTHOR	50
    
    // The Maximum Length of DATE is 20 Characters
    #define MAXL_DATE
    
    // Defining a struct (Mostly Quoted from Hammer)
    typedef struct BookRecord
    {
    	int Book_Id;
       char Book_Name[MAXL_BOOKNAME];
       char Book_Author[MAXL_BOOKAUTHOR];
       char Book_PublishingDate[MAXL_DATE];
       /* we can use "time_t Book_PublishingDate; */
    
       // return true if the Book is borrowed Is it correct?
    	bool Book_Borrowed;
    } BookRecord_t; // Why using _t ?
    
    void main()
    {
       int val;
    	printf("What Do you Want to do: ");
       printf("\n1. Encrypt The File.");
       printf("\n2. Decrypt The file.");
       printf("\n3. Add a new Book.");
       printf("\n4. Remove an Existing Book.");
       printf("\n5. Update an Existing Book.");
       printf("\n6. Exit Program.\n");
    
       scanf("%d", &value);
    
       switch val {
       	case 1:
          	// What functions to include here?
          break;
          case 2:
          	// What functions to include here?
          case 3:
          	AddRecord() // What parameters to include?
          case 4:
          	RemoveRecord(RecordNum) // I'm not sure about the parameters
          case 5:
          	UpdateRecord(RecordNum) // also i'm not ....
          case 6:
          	exit();
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >// The Maximum Length of DATE is 20 Characters
    >#define MAXL_DATE 20
    You forgot to specify the 20.

    >// return true if the Book is borrowed Is it correct?
    >bool Book_Borrowed;
    Both of these lines are incorrect. The // commenting style is only legal in C99 (which it is safe to assume you aren't using) and C++. Ditto that for bool. In C you can fake it by any number of ways (here is one):
    Code:
    typedef enum { false, true } bool;
    >BookRecord_t; // Why using _t ?
    _t means "type", it's a common idiom for typedef'd identifiers. Not required, but it tells you immediately that the type you're using was created with typedef.

    >void main()
    Bzzt! The only way this would be legal is if you're on a freestanding implementation. Since you're asking for help on these boards it's safe to assume you aren't. Use one of the two legal declarations:
    Code:
    int main(void) /* No arguments */
    int main(int argc, char **argv) /* Command line arguments */
    >scanf("%d", &value);
    Always check the return value of scanf. Users are very good at breaking perfectly good solutions so we have to add three times the code to cover for it.

    >switch (val) {
    Don't forget parentheses.

    >case 1:
    >// What functions to include here?
    Functions to encrypt the file. The creation of such functions is up to you though, so I can't tell you what to do.

    >case 2:
    >// What functions to include here?
    Ditto for decryption.

    >AddRecord() // What parameters to include?
    It depends on what kind of data structure you intend to use. But I can safely assume that it would look something like this:
    Code:
    /* Guessing */
    AddRecord(RecordList);
    >RemoveRecord(RecordNum) // I'm not sure about the parameters
    Code:
    /* Guessing */
    RemoveRecord(RecordList, RecordNum);
    >UpdateRecord(RecordNum) // also i'm not ....
    Code:
    /* Guessing */
    UpdateRecord(RecordList, RecordNum);
    >exit();
    exit requires an argument.

    Finally, the switch statement should have a default case, otherwise you'll need to check the input from scanf for validity elsewhere.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Find another course dude, programming isn't for you
    No one here has the patience to sit and wait for you to guess how to do this program one line at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 01:16 PM
  2. Simple C++ program - help needed
    By Kinnison in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2007, 11:21 AM
  3. String program
    By howdy_doody in forum C Programming
    Replies: 2
    Last Post: 03-13-2005, 07:01 PM
  4. Answers for exercise in K & R
    By kermit in forum C Programming
    Replies: 7
    Last Post: 01-25-2003, 08:38 PM