I am in my 70s and enjoying learning C to keep my mind exercised and working from a 1985 book "The C Compendium by David Lawrence and Mark England ISBN 0-946408-86-6". This code is from the book and with a few adjustments I have it working. My question is whether this code is in line with current practice for console applications?
/****************************************************************************
* *
* File : main.c *
* *
* Purpose : Console mode (command line) program. *
* *
* History : Date Reason *
* 00/00/00 Created *
* *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
char *err_mgs [] = {
"\nMessage 1" ,
"\nMessage 2" ,
"\nMessage 3" ,
"\nMessage 4" ,
"\nMessage 5"
};
#define PROG_WARN 1
#define PROG_FATAL 2
#define DATA_WARN 3
#define DATA_FATAL 4
#define ERROR_MAX sizeof(err_mgs)/sizeof(char *)
//FUNCTION: ERROR - PROCEDURE WITH NO RETURN VALUE
void error (int num, int type, int extra)
{
extern char *err_mgs[];
switch (type) {
case PROG_WARN:
printf ("\n\n***** WARNING : possible error in program *****");
break;
case PROG_FATAL:
printf ("\n\n***** FATAL ERROR in program *****");
break;
case DATA_FATAL:
case DATA_WARN:
break;
default:
error (-2,PROG_FATAL, type);
}
if (num >= 0 && num < ERROR_MAX)
printf ("%s", err_mgs[num]);
else
switch (num) {
case -1:
printf("\nUnknown error number: %d",extra);
break;
case -2:
printf("\nUnknown error type: %d",extra);
break;
default:
error (-1,PROG_FATAL, num);
}
if (type == PROG_FATAL || type == DATA_FATAL) {
printf("\n\nProgram Execution Terminated");
abort();
}
}
/****************************************************************************
* *
* Function: main *
* *
* Purpose : Main entry point. *
* *
* History : Date Reason *
* 00/00/00 Created *
* *
****************************************************************************/
int main(int argc, char *argv[])
{
/* TODO: Enter code here */
int n, num, type;
for (n=0;n<10;n++)
{ printf ("\n\nError numbers 0-%d,error type 1-4", ERROR_MAX-1);
printf ("\nInput Error number to print, error type ? ");
scanf ("%d, %d", &num, &type);
printf("\n\n num = %d type = %d", num, type);
error (num, type,0);
}
return 0;
}