In 'comp.lang.c' said:
I know that there¡¯s must be more bugs in it, hope you guys
point them out for me, and any suggestion, critics to my code or
the my programming style will be appreciated !
Hard to say, because your code doesn't compile, and there are a lot of
functions missing.
Compiling MAIN.C:
Warning MAIN.C 109: Call to function 'errorMessage' with no prototype
Warning MAIN.C 147: Call to function 'isFileExist' with no prototype
Error MAIN.C 147: Undefined symbol 'true'
Warning MAIN.C 152: Call to function 'errorMessage' with no prototype
Warning MAIN.C 176: Call to function 'errorMessage' with no prototype
Warning MAIN.C 190: Call to function 'errorMessage' with no prototype
Warning MAIN.C 239: Call to function 'validateName' with no prototype
Warning MAIN.C 242: Call to function 'validateStudentID' with no prototype
Warning MAIN.C 249: Call to function 'currentTime' with no prototype
Error MAIN.C 249: Type mismatch in parameter '__src' in call to 'strcpy'
Warning MAIN.C 258: Call to function 'isFileExist' with no prototype
Error MAIN.C 258: Undefined symbol 'true'
Warning MAIN.C 264: Call to function 'errorMessage' with no prototype
Warning MAIN.C 273: Call to function 'errorMessage' with no prototype
Warning MAIN.C 311: Call to function 'errorMessage' with no prototype
Warning MAIN.C 320: Call to function 'currentTime' with no prototype
Warning MAIN.C 320: Nonportable pointer conversion
Warning MAIN.C 341: Call to function 'errorMessage' with no prototype
Warning MAIN.C 380: Call to function 'errorMessage' with no prototype
Warning MAIN.C 403: Initialization is only partially bracketed
Warning MAIN.C 448: Call to function 'menu' with no prototype
Warning MAIN.C 486: Call to function 'credits' with no prototype
Warning MAIN.C 491: Call to function 'menu' with no prototype
Warning MAIN.C 496: Call to function 'systemHelp' with no prototype
BTW, your indentation is not consistent, and your line spacing is boring
(could be an editor issue). Here is a partial review of your code (-ed-).
It's far to be complete.
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if 0 /* -ed- Not standard C, and probably useless.
Removed */
#include <conio.h>
#endif
#include <ctype.h>
#include <string.h>
#define GENRAND(n) (rand()%n)
#define UNOCCUPIED -1 /* to test whether the machine occupied */
#define ENTER 1
#define EXIT 0
#define DATABASE "database.dat" /* the database file name */
const char *mainMenu[] =
{
"+=======================================================+",
"| 1. Log on to system. |",
"| 2. Log out from system. |",
"| 3. Record inquire |",
"| 4. Data statistics |",
"| 5. Credit |",
"| 6. This Menu |",
"| 7. Help |",
"| 8. Exit |",
"+=======================================================+\n",
NULL
};
const char *credit[] =
{
"+======================================================+",
"| CREDIT |",
"+======================================================+",
"| THE FOLLOWING STUDENTS FROM |",
"| FACULTY OF COMPUTER SCIENCE AND TECHNOLOGY CLASS 024 |",
"| WHO HAVE MADE THIS SYSTEM POSSIBLE. |\n",
"+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+",
NULL
};
const double payPerHour = 1.00; /* payment rate on an hourly basis */
#if 0 /* -ed- Not C. */
const unsigned totalMachine = 100; /* total machines */
int statistics[totalMachine];
#else
/* total machines */
#define totalMachine 100
int statistics[totalMachine];
#endif
typedef struct tagRECORD
{ /* the management record structure */
char name[8]; /* student name */
char studentID[9]; /* student ID */
char logonTime[25]; /* log on time */
char logoutTime[25]; /* log out time */
int machineID; /* machine ID */
clock_t logon; /* log on clock */
clock_t logout; /* log out clock */
}
RECORD;
/* I don't know how to clear the screen under Visual C++ */
void clrscr (void)
{
};
void onInitialization (void)
{
int i;
srand ((unsigned) time (NULL)); /* seed */
for (i = 0; i < totalMachine; ++i)
{
#if 0 /* -ed- Not C. */
statistics = UNOCCUPIED; /* initialize the array */
#else
statistics
= UNOCCUPIED; /* initialize the array */
#endif
}
}
/* randomly distribute the available machine ID */
int assignID (void)
{
int i, r; /* to each student log on */
int id = 0;
FILE *fp;
RECORD record;
fp = fopen (DATABASE, "rb");
if (fp == NULL)
{
/* -ed- Don't call functions without prototype.
* BTW, this function is not defined
*/
errorMessage ("assignID()");
}
for (i = 0; !feof (fp) && i < totalMachine; ++i)
/* -ed- feof() doesn't work as you think. Read the FAQ for details.
* Use the value returned by fread()
*/
{
fread (&record, sizeof (record), 1, fp);
#if 0 /* -ed- Not C. Hard to believe that this
compiles on your system */
statistics = record.machineID;
#else
statistics = record.machineID;
#endif
}
for (r = 0; r < i && statistics[r] != -1; ++r)
{
id = GENRAND (totalMachine);
while (statistics[r] == id)
{
id = GENRAND (totalMachine);
}
}
fclose (fp);
return 0;
}
void availableComputers (void)
{
int i;
FILE *fp;
RECORD record;
/* -ed- Missing prototype, undefined function. */
/* -ed- undefined 'true'. */
if (isFileExist (DATABASE) == true)
{
fp = fopen (DATABASE, "rb");
if (fp == NULL)
{
errorMessage ("availableComputer()");
}
for (i = 0; !feof (fp) && i <= totalMachine; ++i)
{
fread (&record, sizeof (record), 1, fp);
#if 0 /* -ed- Not C. Hard to believe that this
compiles on your system */
statistics = record.machineID;
if (statistics == UNOCCUPIED)
{
printf ("unoccupied machine #%d\n", statistics);
}
#else
statistics = record.machineID;
if (statistics == UNOCCUPIED)
{
printf ("unoccupied machine #%d\n", statistics);
}
#endif
}
fclose (fp);
}
else
{
errorMessage ("database doesn't exist");
}
}
FILE *lookupStudent (char *sid)
{
int flag = 0, result;
FILE *fp;
RECORD record;
fp = fopen (DATABASE, "rb");
if (fp == NULL)
{
errorMessage ("lookupStudent()");
}
while (!feof (fp))
{
fread (&record, sizeof (record), 1, fp);
result = strcmp (record.studentID, sid); /* if the student ID exist */
if (result == 0)
{
flag = 1;
break;
}
else
{
flag = 0;
continue;
}
}
return ((flag == 1) ? fp : NULL); /* return the specific record in the
data */
/* to which the FILE *fp points */
}
#if 0
/* BUG!! BUG !! */
bool isBlankLine (char *line)
{
char *tmp = line;
bool blank = true;
while (*tmp++ != '\0')
{
if ((*tmp != ' ') || (*tmp != '\n'))
{
blank = false;
break;
}
}
return blank;
}
#endif
void logStudentInfo (RECORD * record)
{
/* -ed- Missing prototype, undefined function. */
validateName (record->name); /* gets student name */
/* -ed- Missing prototype, undefined function. */
validateStudentID (record->studentID); /* gets student ID */
record->machineID = assignID (); /* gets machine ID */
/* -ed- Missing prototype, undefined function.
* Compiler error due to lack of prototype.
*/
strcpy (record->logonTime, currentTime ()); /* record log on time */
record->logon = clock (); /* clock the time */
}
void storeStudentInfo (RECORD * record)
{
FILE *fp;
if (isFileExist (DATABASE) == true)
{ /* if database file exist */
fp = fopen (DATABASE, "ab"); /* open in APPEND mode */
if (fp == NULL)
{
errorMessage ("storeStudentInfo()");
}
}
else
{
fp = fopen (DATABASE, "w+b"); /* if doesn't exist, CREATE it */
if (fp == NULL)
{
errorMessage ("storeStudentInfo()");
}
}
fwrite (record, sizeof (*record), 1, fp); /* write to the database */
fclose (fp);
}
double wholeCost (RECORD * record)
{
double duration;
duration = (double) ((record->logout) - (record->logout)) /
CLOCKS_PER_SEC; /* time elapsed */
return payPerHour * duration; /* money should pay */
}
void showRecordContent (RECORD * record)
{
printf ("Student name:\t%-8s\n", record->name);
printf ("Student ID :\t%-9s\n", record->studentID);
printf ("Machine ID :\t%-3d\n", record->machineID);
printf ("Log on time :\t%-25s", record->logonTime);
printf ("Log out time:\t%-25s", record->logoutTime);
}
void logoutSystem (char *sid)
{
char *timeIt;
FILE *fp;
RECORD record;
double totalPay;
fp = lookupStudent (sid);
if (fp == NULL)
{
errorMessage ("no such record exist");
}
if (!feof (fp))
{
fread (&record, sizeof (record), 1, fp);
}
strcpy (record.studentID, sid);
timeIt = currentTime ();
strcpy (record.logoutTime, timeIt);
record.logout = clock ();
totalPay = wholeCost (&record);
printf ("you are logging out system....\n");
showRecordContent (&record);
printf ("Payment:\t%-lf\n", totalPay);
fclose (fp);
}
void inquireRecord (char *sid)
{
FILE *fptr;
RECORD record;
double totalPay;
FILE *fp;
fp = fopen (DATABASE, "rb");
if (fp == NULL)
{
errorMessage ("assignID()");
}
fptr = lookupStudent (sid);
if (fptr == NULL)
{
printf ("no such record.\n");
exit (1);
}
if (!feof (fptr))
{
fread (&record, sizeof (record), 1, fptr);
}
if (record.logoutTime == NULL)
{ /* compare with there input student ID */
fprintf (stderr, "the student hasn't log out yet\n");
}
totalPay = wholeCost (&record);
showRecordContent (&record);
printf ("Payment:\t%-lf\n", totalPay);
fclose (fp);
}
void dataStatistics (void)
{ /* balance for the day */
FILE *fp;
RECORD record;
double individualPayment; /* individual balance */
double paySum = 0; /* balance */
fp = fopen (DATABASE, "rb");
if (fp == NULL)
{
errorMessage ("dataStatistics()");
}
while (!feof (fp))
{
fread (&record, sizeof (record), 1, fp);
individualPayment = wholeCost (&record); /* get individual balance */
paySum += individualPayment; /* gets sum */
}
printf ("today bill account:\n");
printf ("%f\n", paySum); /* ummmm......how much ? */
fclose (fp);
}
void showMode (int tag, char *mode)
{
printf ("%s %s mode...\n", ((tag == 1) ? "entering" : "exiting"), mode);
}
void logonSystem (void)
{
RECORD r =
{NULL, NULL, NULL, NULL, 0, 0, 0}; /* initialize the strucutre variable
*/
int ch;
while (1)
{
logStudentInfo (&r);
storeStudentInfo (&r);
printf ("more ?\n: "); /* more to log on ? */
#if 0 /* -ed- Not standard. Replaced with getchar() */
ch = getche (); /* wait for key press */
#else
ch = getchar (); /* wait for key press */
#endif
if (ch == 'y')
{
printf ("\n"); /* hurray ! */
continue; /* enter the next loop */
}
else
{
printf ("\n"); /* hey man, that sucks! */
showMode (EXIT, "");
break; /* exit from the loop */
}
}
}
/* input student ID for later process */
void inputStudentID (char *sid)
{
printf ("enter the student ID for inquire: ");
scanf ("%s", sid);
}
/* the main functionality reside here */
void showMainMenu (void)
{
int choice;
char sid[9]; /* temporarily storing the student ID */
/* -ed- Missing prototype, undefined function. */
menu ();
while (1)
{
#if 0 /* -ed- Not standard. Replaced with getchar() */
choice = getch ();
#else
choice = getchar (); /* wait for key press */
#endif
switch (choice)
{
case '1':
showMode (ENTER, "log on");
logonSystem ();
/* log on */
break;
case '2':
showMode (ENTER, "log out"); /* log out */
inputStudentID (sid);
logoutSystem (sid);
break;
case '3':
showMode (ENTER, "inquire"); /* inquire */
inputStudentID (sid);
inquireRecord (sid);
break;
case '4':
showMode (ENTER, "statistics"); /* balance */
dataStatistics ();
break;
case '5':
/* -ed- Missing prototype, undefined function. */
credits (); /* program credits */
break;
case '6':
/* -ed- Missing prototype, undefined function. */
menu ();
break;
case '7':
/* -ed- Missing prototype, undefined function. */
systemHelp (); /* system help */
break;
case '8':
#if 0 /* -ed- Not standard. Ignored */
clrscr (); /* bye bye ! */
#else
#endif
fprintf (stderr, "EXITING FROM SYSTEM...");
fprintf (stderr, "BYE BYE!\n");
exit (1);
default:
fprintf (stderr, "invalid option\n");
}
};
}
int main (void)
{
onInitialization ();
clrscr ();
showMainMenu ();
return 0;
}