input update
This commit is contained in:
parent
8d4f8297ad
commit
7620584f4b
59
admin_user.h
59
admin_user.h
@ -198,66 +198,21 @@ struct inventory item_control(struct inventory db,int index){
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* prompt_item(char* prompt){
|
|
||||||
char* item = malloc(sizeof(char) * 100);
|
|
||||||
do{
|
|
||||||
printf("%s",prompt);
|
|
||||||
fflush(stdin);
|
|
||||||
scanf("%[^\n]",item);
|
|
||||||
if(strcmp(item,"") == 0){
|
|
||||||
printf("Invalid input, try again?(y/n)\n");
|
|
||||||
char temp[100];
|
|
||||||
fflush(stdin);
|
|
||||||
scanf("%[^\n]",temp);
|
|
||||||
if(strcmp(temp,"n") == 0){
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}while(strcmp(item,"") == 0);
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_item(){
|
void add_item(){
|
||||||
char* temp;
|
|
||||||
system("cls");
|
system("cls");
|
||||||
printf("Add new item\n");
|
printf("Add new item\n");
|
||||||
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
|
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
|
||||||
temp = prompt_item("Please input the name: \n>");
|
if(!item_inputer("name",INPUT_TYPE::STRING,item->product) ||
|
||||||
if(temp == NULL){
|
!item_inputer("brand",INPUT_TYPE::STRING,item->brand) ||
|
||||||
|
!item_inputer("category",INPUT_TYPE::STRING,item->category) ||
|
||||||
|
!item_inputer("stock",INPUT_TYPE::FLOAT,item->price) ||
|
||||||
|
!item_inputer("barcode",INPUT_TYPE::LONG,item->barcode)
|
||||||
|
){
|
||||||
free(item);
|
free(item);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strcpy(item->product,temp);
|
|
||||||
temp = prompt_item("Please input the brand: \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
strcpy(item->brand,temp);
|
|
||||||
temp = prompt_item("Please input the category: \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
strcpy(item->category,temp);
|
|
||||||
temp = prompt_item("Please input the price: \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
item->price = atof(temp);
|
|
||||||
temp = prompt_item("Please input the stock: \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
item->stock = atoi(temp);
|
|
||||||
temp = prompt_item("Please input the barcode: \n>");
|
|
||||||
if(temp == NULL){
|
|
||||||
free(item);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
item->barcode = atol(temp);
|
|
||||||
if(item == NULL || !append_inventory(item)){
|
if(item == NULL || !append_inventory(item)){
|
||||||
printf("Failed to add item\n");
|
printf("Failed to add item\n");
|
||||||
}else{
|
}else{
|
||||||
|
50
utils.h
50
utils.h
@ -38,7 +38,55 @@ int choices_selecter(char * Items[],int items,char * welcome_messages ){
|
|||||||
}while(choice < 1 || choice > items+1);
|
}while(choice < 1 || choice > items+1);
|
||||||
return choice;
|
return choice;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* prompt_item(char* prompt){
|
||||||
|
char* item = malloc(sizeof(char) * 100);
|
||||||
|
do{
|
||||||
|
printf("%s",prompt);
|
||||||
|
fflush(stdin);
|
||||||
|
scanf("%[^\n]",item);
|
||||||
|
if(strcmp(item,"") == 0){
|
||||||
|
printf("Invalid input, try again?(y/n)\n");
|
||||||
|
char temp[100];
|
||||||
|
fflush(stdin);
|
||||||
|
scanf("%[^\n]",temp);
|
||||||
|
if(strcmp(temp,"n") == 0){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}while(strcmp(item,"") == 0);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
typedef enum{
|
||||||
|
INT=1,STRING=2,LONG=3,FLOAT=4
|
||||||
|
}INPUT_TYPE;
|
||||||
|
bool item_inputer(char * varname,INPUT_TYPE type,void * item){
|
||||||
|
char output[1024] = "Please input the ";
|
||||||
|
strcat(output,varname);
|
||||||
|
strcat(output,": \n>");
|
||||||
|
char* temp = prompt_item(output);
|
||||||
|
if(temp == NULL){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
switch(type){
|
||||||
|
case INT:
|
||||||
|
item = atoi(temp);
|
||||||
|
break;
|
||||||
|
case STRING:
|
||||||
|
strcpy(item,temp);
|
||||||
|
break;
|
||||||
|
case LONG:
|
||||||
|
item = atol(temp);
|
||||||
|
break;
|
||||||
|
case FLOAT:
|
||||||
|
item = atof(temp);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
//check if strcasestr is define in current environment
|
//check if strcasestr is define in current environment
|
||||||
//if not, define it
|
//if not, define it
|
||||||
#ifndef HAVE_STRCASESTR
|
#ifndef HAVE_STRCASESTR
|
||||||
|
Loading…
x
Reference in New Issue
Block a user