!!!! I got stucked with this !!!!! Question part

D

dreamcatcher

I have posted in ¡°problem with generating random numbers¡± I still
having many trouble with my program,



[First] : when program starts, screen display as below



+=======================================================+

| 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 |

+=======================================================+

entering log on mode... // when I press 1

gary // entering student name

02045109 // student id

error: assignID() // then I gets this





so it¡¯s obviously that the function assignID() having problem, but I
just can¡¯t find where the problem is.





So I commented all other lines except for the return-statement





int assignID(void) { // distribute machine id randomly

return 0;

}





[the second problem] comes when press 1 entered the log on mode:



entering log on mode...

gary // get first student name

02045109 // student id

more ?

: y // more to log on

steven // the second student

02045110

more ?

: y

Jason // the third

02045111

more ?

: y

emilie // the fourth

02045112

more ?

: n

exiting mode...

entering log out mode... // press 2 to log out

enter the student ID for inquire: 02045109 //PROBLEM:the ID:02045109

you are logging out system.... //belongs to gary but to display

Student name: steven //the steven¡¯s info,I don¡¯t know why

Student ID : 02045109

Machine ID : 0

Log on time : Sat Sep 13 22:44:55 2003

Sat Sep 13 22:45:36 2003 //PROBLEM:Where the hell does this come from ?

Log out time: Sat Sep 13 22:45:36 2003

Payment: 0.000000



And [the third problem]:



As I have initialized the structure with





RECORD r={NULL,NULL,NULL,NULL,0,0,0};





In function logonSystem() but

The following code in inquireRecord(),

to determine whether a logged on student have logged out or not





if(record.logoutTime==NULL) { // compare with there input student ID

fprintf(stderr,"the student hasn't log out yet\n");

}





turned out that it never work the way I wanted, just don¡¯t know why!



And in function isBlankLine(), which I have commented it, i wanted to
determined whether an input line, was a blank line or just totally blank
spaces. It doesn¡¯t work either.



[Problem four]:



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 !
 
N

Nick Austin

I have posted in ¡°problem with generating random numbers¡± I still
having many trouble with my program,

Apparently you have yet to learn the technique for getting help
from others.

Produce a minimal compilable program that shows each of the problems
that you are having.

The following is a list of mistakes that you should try to avoid
repeating:

1. You posted the questions separately from the source.

2. You didn't post the source for functions validateName() and
validateStudentID().

3. You define an array:
int statistics[totalMachine];
But there are several places where you attempt to access an
element from this array but you forgot to supply the required
index, e.g:
function onInitialization():
statistics=UNOCCUPIED; // initialize the array
function assignID():
statistics=record.machineID;
function availableComputers():
statistics=record.machineID;
if(statistics==UNOCCUPIED)
This won't compile.

4. The comment opened thus:
const char *mainMenu[]={
/*
is never closed.

5. Non-ANSI extensions included via:
#include <io.h>
#include <conio.h>

6. Long lines with // comment lines that were split by the posting
software.

Nick.
 
E

Emmanuel Delahaye

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top