Started working on non-interactive mode
This commit is contained in:
parent
1037aef5f5
commit
705da77a3a
64
on-stock.c
64
on-stock.c
@ -24,9 +24,9 @@ char filename[FILEMAXLENGTH] = "storage.bin";
|
|||||||
|
|
||||||
/* Function prototype */
|
/* Function prototype */
|
||||||
void list(struct myData *datap, int numRec);
|
void list(struct myData *datap, int numRec);
|
||||||
void search(struct myData *datap, int numRec);
|
void search(struct myData *datap, int numRec, char *name);
|
||||||
void modify(struct myData *datap, int numRec);
|
void modify(struct myData *datap, int numRec, char *name, char *num);
|
||||||
void delete(struct myData *datap, int numRec);
|
void delete(struct myData *datap, int numRec, char *name);
|
||||||
int new(struct myData *datap, int numRec);
|
int new(struct myData *datap, int numRec);
|
||||||
void printUsage(char *arg);
|
void printUsage(char *arg);
|
||||||
void printHeader(void);
|
void printHeader(void);
|
||||||
@ -112,12 +112,18 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
if ( choice == 'l' )
|
if ( choice == 'l' )
|
||||||
list(data, numRec);
|
list(data, numRec);
|
||||||
else if ( choice == 's' )
|
else if ( choice == 's' && argc >= 2 )
|
||||||
search(data, numRec);
|
search(data, numRec, argv[2]);
|
||||||
else if ( choice == 'm' )
|
else if ( choice == 's' && argc <= 2)
|
||||||
modify(data, numRec);
|
search(data, numRec, NULL);
|
||||||
else if ( choice == 'd' )
|
else if ( choice == 'm' && argc >= 3)
|
||||||
delete(data, numRec);
|
modify(data, numRec, argv[2], argv[3]);
|
||||||
|
else if ( choice == 'm' && argc <= 4)
|
||||||
|
modify(data, numRec, NULL, NULL);
|
||||||
|
else if ( choice == 'd' && argc >= 2 )
|
||||||
|
delete(data, numRec, argv[2]);
|
||||||
|
else if ( choice == 'd' && argc <= 2 )
|
||||||
|
delete(data, numRec, NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ( choice == 'n' )
|
else if ( choice == 'n' )
|
||||||
@ -149,14 +155,20 @@ void list(struct myData *datap, int numRec)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void search(struct myData *datap, int numRec)
|
void search(struct myData *datap, int numRec, char *name)
|
||||||
{
|
{
|
||||||
char searchword[NAMEMAXLENGTH];
|
char searchword[NAMEMAXLENGTH];
|
||||||
|
if (name != NULL)
|
||||||
|
{
|
||||||
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
/* Replace the newline character with a null character */
|
/* Replace the newline character with a null character */
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
@ -170,15 +182,26 @@ void search(struct myData *datap, int numRec)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void modify(struct myData *datap, int numRec)
|
void modify(struct myData *datap, int numRec, char *name, char *num)
|
||||||
{
|
{
|
||||||
char searchword[NAMEMAXLENGTH];
|
char searchword[NAMEMAXLENGTH];
|
||||||
char what[10];
|
char what[10];
|
||||||
char quant[20];
|
char quant[20];
|
||||||
|
int interactive = 1;
|
||||||
|
|
||||||
|
if (name != NULL && num != NULL)
|
||||||
|
{
|
||||||
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
|
strncpy(what, "quantity", 9);
|
||||||
|
strncpy(quant, num, 19);
|
||||||
|
interactive = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
@ -190,18 +213,24 @@ void modify(struct myData *datap, int numRec)
|
|||||||
printf("%.2f\t", datap[i].price);
|
printf("%.2f\t", datap[i].price);
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
|
if (interactive == 1)
|
||||||
|
{
|
||||||
printf("What do you like to modify? (name, quantity, price): ");
|
printf("What do you like to modify? (name, quantity, price): ");
|
||||||
fgets(what, 10, stdin);
|
fgets(what, 10, stdin);
|
||||||
what[strcspn(what, "\n")] = '\0';
|
what[strcspn(what, "\n")] = '\0';
|
||||||
|
}
|
||||||
if ( strcmp(what, "name") == 0 )
|
if ( strcmp(what, "name") == 0 )
|
||||||
{
|
{
|
||||||
printf("Name: "); scanf("%29s", datap[i].name);
|
printf("Name: "); scanf("%29s", datap[i].name);
|
||||||
}
|
}
|
||||||
else if ( strcmp(what, "quantity") == 0 )
|
else if ( strcmp(what, "quantity") == 0 )
|
||||||
|
{
|
||||||
|
if (interactive == 1)
|
||||||
{
|
{
|
||||||
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 */
|
/* 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);
|
||||||
@ -226,14 +255,21 @@ void modify(struct myData *datap, int numRec)
|
|||||||
fclose(newfp);
|
fclose(newfp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete(struct myData *datap, int numRec)
|
void delete(struct myData *datap, int numRec, char *name)
|
||||||
{
|
{
|
||||||
char searchword[NAMEMAXLENGTH];
|
char searchword[NAMEMAXLENGTH];
|
||||||
int answer;
|
int answer;
|
||||||
|
|
||||||
|
if (name != NULL)
|
||||||
|
{
|
||||||
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
|
}
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[i].name) == 0 )
|
if ( strcmp(searchword, datap[i].name) == 0 )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user