Help me out here if you can PLEASE 
Author Message
 Help me out here if you can PLEASE

I can't get the add remove function to work it is supposed to add to or
subtract from the quantity of items in the record.

Thanks,
Irvin.

/* This progam creates and maintains a simple
 database as a stock control system
 I used Borland C++ 4.5 */

#include <stdlib.h>  /* Standard Header files for C */
#include <stdio.h>
#include <conio.h>
#include <string.h>

#define CODE_LEN        5  /*Length of code required */
#define DESC_LEN        40 /*Length of description */

#define STD_BUFFER      256  /*Standard buffer */

/* #define clrscr()        (void)0 Some Compilers require these lines*/
/* #define gotoxy(x, y)    (void)0 to declare (define) clrscr & gotoxy
*/

/* Record structure defined here */
struct record{
  char code[CODE_LEN + 1];
  char description[DESC_LEN + 1];
  /* Extra character for a null terminator*/
  double quantity, price;

Quote:
} store;

#define dfile "DATAFILE.DAT" /* defines the file */

/* Function Prototypes */
void show_options(void);
/* Shows all the options avaliable */
void clear_record(void);
/* Clears the record for more input so no characters will be carried
through */
void add_record(FILE *fp);
/* Add a record to the file */
void edit_record(FILE *fp);
/* Edit an existing record */
void addsub_record(FILE *fp);
/*Add to or Subtract from the quantity of stock in file */
/*void delete_record(FILE *fp); */
/* Delete a record */
void query_record(FILE *fp);
/*Check up on a record in file */
void display_records(FILE *fp);
/* Displays all the records in the file */

int main(void)
{
    FILE *fp;
    int inkey = 0;

  if ((fp = fopen (dfile, "r+b")) == NULL)
  if ((fp = fopen (dfile, "w+b")) == NULL)
  {
    fprintf (stderr, "\aCould not create %s\n", dfile);
    exit (EXIT_FAILURE);
  }

  /* Repeat forever */
  while (inkey != 'x')
  {
    char buffer[STD_BUFFER];

    show_options();
    fgets(buffer, STD_BUFFER, stdin);

    inkey = buffer[0];

    switch (inkey)
    {
    case '1':
      add_record(fp);
      break;

    case '2':
      edit_record(fp);
      break;

    case '3':
      addsub_record(fp);
      break;

    /*  case '4':
      delete_record(fp);
      break; */

    case '5':
      query_record(fp);
      break;

    case '6':
      display_records(fp);
      break;

    case 'x':
      printf("Thanks, bye\n");
      break;
    }
  }

  if (fp != NULL) fclose(fp);

  return 0;

Quote:
}

void show_options(void)
/* Shows all the options avaliable */

{
  clrscr();
  gotoxy(20,10);
  printf("Select which of the following you want");
  gotoxy(19,11);
  printf("  (Enter the number, or 'x' to quit):");
  gotoxy(19,12);
  printf("--------------------------------------");
  gotoxy(19,13);
  printf("1. Add more stock to the database.");
  gotoxy(19,14);
  printf("2. Edit stock already in the database.");
  gotoxy(19,15);
  printf("3. Add/Subtract from stock in database.");
  gotoxy(19,16);
  printf("4. Delete stock from database.");
  gotoxy(19,17);
  printf("5. Query a stock item.");
  gotoxy(19,18);
  printf("6. Produce a report of the entire database.");
  gotoxy(15,19);
  printf("x. Exit\n");

Quote:
}

void clear_record(void)
/* Clears the record for more input so no characters will be carried
through */

{
  int n;

  for (n = 0; n < CODE_LEN; n++)
  store.code[n] = '\0';

  for (n = 0; n < DESC_LEN; n++)
  store.description[n] = '\0';

  store.quantity = 0.0;
  store.price = 0.0;

Quote:
}

void add_record(FILE *fp)
/* Add a record to the file */

{
  char buffer[STD_BUFFER];

  clrscr();
  printf("\nEnter ID code 5 characters :");
  fgets(buffer, STD_BUFFER, stdin);
  strncpy(store.code, buffer, CODE_LEN);

  printf("\nEnter description of item up to 40 characters : ");
  fgets(buffer, STD_BUFFER, stdin);
  strncpy(store.description, buffer, DESC_LEN);

  printf("\nEnter quantity of item(s) : ");
  fgets(buffer, STD_BUFFER, stdin);
  store.quantity = atof(buffer);

  printf("\nEnter cost of item : R ");
  fgets(buffer, STD_BUFFER, stdin);
  store.price = atof(buffer);

  fwrite(&store, sizeof(store), 1, fp);
  fflush(fp); /* commit any changes */

  printf("Stock added");
  clear_record();

Quote:
}

void edit_record(FILE *fp)
/* Edit an existing record */

{
  char buffer[STD_BUFFER];
  long recno = 0;

  clrscr();
  printf("\nEnter ID code (5 chars)of item to edit: ");
  fgets(buffer, STD_BUFFER, stdin);
  recno = atol(buffer) - 1;       /* adjust for indexing */

  /* fseek positions the file pointer */
  fseek(fp,sizeof(store)*recno,SEEK_SET);

  /* read 1 record */
  fread(&store, sizeof(store),1,fp);

  /* display record */
  printf("\nCode :%s",store.code);
  printf("\nDescription  :%s",store.description);
  printf("\nQuantity :%f",store.quantity);
  printf("\nPrice :R %f\n",store.quantity);

  /* Get user to input changes.*/
  printf("Code :");
  fgets(buffer, STD_BUFFER, stdin);
  strncpy(store.code, buffer, CODE_LEN);

  printf("Description :");
  fgets(buffer, STD_BUFFER, stdin);
  strncpy(store.description, buffer, DESC_LEN);

  printf("Quantity :");
  fgets(buffer, STD_BUFFER, stdin);
  store.quantity = atof(buffer);

  printf("Price :R ");
  fgets(buffer, STD_BUFFER, stdin);
  store.price = atof(buffer);

  /* The fwrite function saves your changes to the file */
  /* It writes 1 record of store to the file pointed by fp */
  fseek(fp, sizeof(store)*recno,0);
  fwrite(&store, sizeof(store), 1, fp);

  fflush(fp); /* commit any changes */
  clear_record();

Quote:
}

void addsub_record(FILE *fp)
/* Edit an existing record */
/* Can't get this to work */

{
  char buffer[STD_BUFFER];
  int ans = 0, inkey = 0;
  char addsub;
  long recno = 0;

  clrscr();
  printf("\nEnter ID code (5 chars) of item to ad/subtract from: ");
  fgets(buffer, STD_BUFFER, stdin);
  recno = atol(buffer) - 1;       /* adjust for indexing */

  /* fseek positions the file pointer */
  fseek(fp,sizeof(store)*recno,SEEK_SET);

  /* read 1 record */
  fread(&store, sizeof(store),1,fp);

  /* display record */
  printf("\nCode :%s",store.code);
  printf("\nDescription  :%s",store.description);
  printf("\nQuantity :%f",store.quantity);
  printf("\nPrice :R %f\n",store.quantity);

  printf("\nDo you want to (a)dd or (s)ubtract from quantity? ");
  inkey = ans;
  switch (inkey)
    {
    case 'a':
      {
       printf("\nEnter the amount you need to add to the database: \n");

       scanf(addsub);
       fgets(buffer, STD_BUFFER, stdin);
       store.quantity = atof(buffer)+addsub;
      }
      break;

    case 's':
      {
       printf("\nEnter the amount you need to subtract from the
database: \n");
       gets(addsub);
       fgets(buffer, STD_BUFFER, stdin);
       store.quantity = atof(buffer)+addsub;
      }
      break;
    }
  /* display new record */
  printf("\nCode :%s",store.code);
  printf("\nDescription  :%s",store.description);
  printf("\nQuantity :%f",store.quantity);
  printf("\nPrice :R %f\n",store.quantity);

  /* The fwrite function saves your changes to the file */
  /* It writes 1 record of store to the file pointed by fp */
  fseek(fp, sizeof(store)*recno,0);
  fwrite(&store, sizeof(store), 1, fp);

  fflush(fp); /* commit any changes */
  clear_record();

Quote:
}

/* void delete_record(FILE *fp)
 Delete a record */
/*  {                      I did not find any reference anywhere to */
/*   deleting from a file
  printf("[delete_record] Not yet implemented!\n");

Quote:
} */

void query_record(FILE *fp)
/*Check up on a record in file */

{
  char fstr[STD_BUFFER]; /* variable to hold search string */
  int recno = 0; /* variable to hold record # */
  char Enter[1]; /* reads enter after displaying record */

  clrscr();
  printf("\nEnter Search String: ");
  fgets(fstr, STD_BUFFER, stdin);

  fseek(fp,0,SEEK_SET);   /* start at beginning of file */

  while (!feof(fp)) /* loop while not end of file */
  {
  /* The fread function reads from the file */
  /* 1 record of store*/
  if (fread(&store, sizeof(store), 1, fp))
  {
    recno++;

    /* displays record if found */
    if (strncmp(store.code,fstr, CODE_LEN)==0)
    {
    printf("\nRecord # %d",recno);
    printf("\nItem Code :%s",store.code);
    printf("\nDescription:%s",store.description);
    printf("\nQuantity:%10.2f",store.quantity);
    printf("\nPrice  : R %10.2f\n",store.price);
    gets(Enter);
    }
  }
  else break;

  clear_record();
  }

Quote:
}

void display_records(FILE *fp)
/* Displays all the records in the file */

{
  int recno = 0;
  double rtotal[100], total;
  char Enter[1]; /*reads enter after displaying each record */

  clrscr();
  total = 0;
  fseek(fp,0,SEEK_SET);   /* start at beginning of file */
  printf("Press Enter after each record has been displayed. \n");

  while (!feof(fp)) /* loop while not end of file */
  {
  /* The fread function reads from the file */
  /* 1 record of store*/

  if (fread(&store, sizeof(store), 1, fp))
  {
    recno++;

    /* Display each record */
    printf("\nRecord # %d",recno);
    printf("\nCode :%s",store.code);
    printf("\nDescription  :%s",store.description);
    printf("\nQuantity  :%10.2f",store.quantity);
    printf("\nPrice  :R %8.2f\n",store.price);
    printf("\nTotal for this item  :R
%16.2f\n",rtotal[recno]=store.quantity*store.price);
    gets(Enter);
   total=rtotal[recno]+total;
  }
  else break;

  clear_record();
  }
  printf("\nThe Overal Total is :R %16.2f\n",total);
      gets(Enter);

Quote:
}

--



Fri, 09 Nov 2001 03:00:00 GMT  
 Help me out here if you can PLEASE
Groovy hepcat Irvin Jacobson was jivin' on Mon, 24 May 1999 21:25:12
GMT in comp.lang.c.
Help me out here if you can PLEASE's a cool scene! Dig it!

Quote:
>I can't get the add remove function to work it is supposed to add to or
>subtract from the quantity of items in the record.

  [Much code snipped.]
  I'm not even going to bother trying to help you until you

1) restate the problem more clearly and in more detail, telling us
exactly what the errant function is supposed to do and what it is
doing instead; and
2) cut down the code to the smallest complete, compileable program
that demonstrates the problem, cutting out all stuff not related to
the problem, but providing enough of a program to compile and run.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============
--



Fri, 23 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

2. Help with compiling a "canned" program

3. Please help!!!!Please help!!!!Please help!!!!

4. Checksums of a.outs

5. Colour print outs of source code ?

6. Determining what canned preprocessor symbols are available

7. Beginner [Q] Using Canned File Open/Save dialog

8. Canned Dialogs

9. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

10. PLEASE PLEASE HELP HELP...question on interleaving C functions

11. help: Guru needed please please please

12. HELP please on QUEUES!! please, please, please, please,please...

 

 
Powered by phpBB® Forum Software