fix scanf to item_inputer

This commit is contained in:
stmctommyau 2023-09-05 14:39:27 +08:00
parent d61a2abc6a
commit 7587d48557

View File

@ -499,66 +499,22 @@ void add_tran(){
struct transaction update_tran(struct transaction db,int index){
char temp[100];
printf("Update transaction(empty value to not change the value)\n");
printf("Please input the Date:day(original:%d)\n>",db.row[index].date.day);
fflush(stdin);
scanf("%[^\n]", temp);//input until /n
if(strcmp(temp,"") != 0){//check if temp is not empty
db.row[index].date.day = atoi(temp);
}//else preserve the original value
printf("Please input the Date:month(original:%d)\n>",db.row[index].date.month);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].date.month = atoi(temp);
}
printf("Please input the Date:year(original:%d)\n>",db.row[index].date.year);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].date.year = atoi(temp);
}
printf("Please input the Time:hour(original:%d)\n>",db.row[index].time.hour);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].time.hour = atoi(temp);
}
printf("Please input the Time:minute(original:%d)\n>",db.row[index].time.minute);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].time.minute = atoi(temp);
}
printf("Please input the Time:second(original:%d)\n>",db.row[index].time.second);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].time.second = atoi(temp);
}
printf("Please input the Price(original:%.1lf)\n>",db.row[index].price);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].price = atof(temp);
}
printf("Please input the Quantity(original:%d)\n>",db.row[index].quantity);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].quantity = atoi(temp);
}
printf("Please input the ID(original:%d)\n>",db.row[index].id);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].id = atol(temp);
}
printf("Please input the Barcode(original:%d)\n>",db.row[index].barcode);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].barcode = atol(temp);
}
sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year);
sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second);
printf("%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total");
printf("%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total);
item_inputer("Date:day",INT,&db.row[index].date.day);
item_inputer("Date:month",INT,&db.row[index].date.month);
item_inputer("Date:year",INT,&db.row[index].date.year);
item_inputer("Time:hour",INT,&db.row[index].time.hour);
item_inputer("Time:minute",INT,&db.row[index].time.minute);
item_inputer("Time:second",INT,&db.row[index].time.second);
item_inputer("Price",DOUBLE,&db.row[index].price);
item_inputer("Quantity",INT,&db.row[index].quantity);
item_inputer("ID",LONG,&db.row[index].id);
item_inputer("Barcode",LONG,&db.row[index].barcode);
if(!update_db_tran(db)){
printf("Failed to update transaction\n");
exit(1);//exit program to prevent errors
@ -702,24 +658,13 @@ struct user add_user(struct user db){
}
struct user edit_user(struct user db,int index){
char temp[100];
printf("Please input the username(original:%s)\n>",db.row[index].name);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
strcpy(db.row[index].name,temp);
}
printf("Please input the id(original:%d)\n>",db.row[index].id);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].id = atol(temp);
}
printf("Please input the role(original:%s)\n>",db.row[index].role);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
strcpy(db.row[index].role,temp);
}
printf("Update transaction(empty value to not change the value)\n");
printf("%-20s%-10s%-10s\n","Username","ID","Role");
printf("%-20s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role);
item_inputer("username",STRING,db.row[index].name);
item_inputer("id",LONG,db.row[index].id);
item_inputer("role",STRING,db.row[index].role);
if(!update_db_user(db)){
printf("Failed to update user\n");
exit(1);//exit program to prevent errors