Added some more comments to the code
This commit is contained in:
parent
3cc197dbc9
commit
54e6168672
11
on-stock.c
11
on-stock.c
@ -88,8 +88,10 @@ int main(int argc, char* argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The structure to contain our database */
|
||||||
struct myData *data;
|
struct myData *data;
|
||||||
|
|
||||||
|
/* Continue processesing the command-line arugments */
|
||||||
if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' )
|
if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' )
|
||||||
{
|
{
|
||||||
/* Open file in read-mode */
|
/* Open file in read-mode */
|
||||||
@ -103,7 +105,6 @@ int main(int argc, char* argv[])
|
|||||||
fread(data, sizeof(struct myData), numRec, fp);
|
fread(data, sizeof(struct myData), numRec, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
|
||||||
if ( choice == 'l' )
|
if ( choice == 'l' )
|
||||||
list(data, numRec);
|
list(data, numRec);
|
||||||
else if ( choice == 's' )
|
else if ( choice == 's' )
|
||||||
@ -129,6 +130,7 @@ int main(int argc, char* argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void list(struct myData *datap, int numRec)
|
void list(struct myData *datap, int numRec)
|
||||||
{
|
{
|
||||||
printHeader();
|
printHeader();
|
||||||
@ -148,6 +150,7 @@ void search(struct myData *datap, int numRec)
|
|||||||
|
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
|
/* Replace the newline character with a null character */
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
@ -195,10 +198,12 @@ void modify(struct myData *datap, int numRec)
|
|||||||
printf("Quantity (absolute value or +/-NUMBER: ");
|
printf("Quantity (absolute value or +/-NUMBER: ");
|
||||||
fgets(quant, 20, stdin);
|
fgets(quant, 20, stdin);
|
||||||
quant[strcspn(quant, "\n")] = '\0';
|
quant[strcspn(quant, "\n")] = '\0';
|
||||||
|
/* Process the first character */
|
||||||
if (quant[0] == '+')
|
if (quant[0] == '+')
|
||||||
datap[i].quantity = datap[i].quantity + atoi(quant);
|
datap[i].quantity = datap[i].quantity + atoi(quant);
|
||||||
else if (quant[0] == '-')
|
else if (quant[0] == '-')
|
||||||
{
|
{
|
||||||
|
/* We need to replace the - with a space before atoi */
|
||||||
quant[0] = ' ';
|
quant[0] = ' ';
|
||||||
datap[i].quantity = datap[i].quantity - atoi(quant);
|
datap[i].quantity = datap[i].quantity - atoi(quant);
|
||||||
}
|
}
|
||||||
@ -240,6 +245,8 @@ void delete(struct myData *datap, int numRec)
|
|||||||
if ( answer == 'y' )
|
if ( answer == 'y' )
|
||||||
{
|
{
|
||||||
FILE *newfp = fopen(filename, "wb");
|
FILE *newfp = fopen(filename, "wb");
|
||||||
|
/* Write out all records, except the one we cant to delete,
|
||||||
|
back to the file, one record at a time */
|
||||||
for (int j = 0; j<numRec; j++)
|
for (int j = 0; j<numRec; j++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[j].name) == 0 )
|
if ( strcmp(searchword, datap[j].name) == 0 )
|
||||||
@ -262,8 +269,10 @@ int new(struct myData *datap, int numRec)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Ask for, and write out, one record at a time */
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
/* Clear the input buffer (from the previous iteration) */
|
||||||
setbuf(stdin, NULL);
|
setbuf(stdin, NULL);
|
||||||
printf("Name ('done' when finished): ");
|
printf("Name ('done' when finished): ");
|
||||||
fgets(datap->name, NAMEMAXLENGTH, stdin);
|
fgets(datap->name, NAMEMAXLENGTH, stdin);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user