input update

This commit is contained in:
AU KA LONG 2023-07-18 13:03:34 +08:00
parent 8d4f8297ad
commit 7620584f4b
2 changed files with 56 additions and 53 deletions

View File

@ -198,66 +198,21 @@ struct inventory item_control(struct inventory db,int index){
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(){
char* temp;
system("cls");
printf("Add new item\n");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
temp = prompt_item("Please input the name: \n>");
if(temp == NULL){
if(!item_inputer("name",INPUT_TYPE::STRING,item->product) ||
!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);
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)){
printf("Failed to add item\n");
}else{

50
utils.h
View File

@ -38,7 +38,55 @@ int choices_selecter(char * Items[],int items,char * welcome_messages ){
}while(choice < 1 || choice > items+1);
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
//if not, define it
#ifndef HAVE_STRCASESTR