fix user and buffer \n bugs,

fflush multi OS compat
This commit is contained in:
stmctommyau 2023-09-07 23:37:19 +08:00
parent 11249fc563
commit 70e46087c2
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
5 changed files with 200 additions and 229 deletions

View File

@ -43,7 +43,7 @@ void admin_menu(){
char welcome[256];
bool exit = false;
do{
sprintf(welcome,"welcome %s(%s)",user->name,user->role);
sprintf(welcome,"welcome %s(%s)\n",user->name,user->role);
switch (choices_selecter(Items,4,welcome))
{
@ -79,27 +79,28 @@ struct user_row *prompt_user(){
if(user_id == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return NULL;
}
struct user_row *user = get_user(user_id);
Cls();
if(user == NULL){
printf("User not found\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return NULL;
}else if(!is_admin(user->role)){
printf("You aren't an admin\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return NULL;
}else{
printf("Welcome %s(%s)\n", user->name,user->role);
printf("Welcome %s %s\n", user->name , user->role);
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
return user;
@ -131,14 +132,13 @@ void inv_control(){
db.db.row_count += 1;
strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM");
db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
char * SortItemss[] = {
char * SortItems[] = {
"Name",
"Price",
"Brand",
"Category",
};
lister(&db,NULL,db.db.row_count,"Inventory",SortItemss,4,Print_page,SortItems,item_control);
// list_page(db,NULL,db.db.row_count,item_control);
lister(&db,NULL,db.db.row_count,"Inventory",SortItems,4,print_page,sortItems,item_control);
break;
case 2://add item
add_item();
@ -229,7 +229,7 @@ void add_item(){
printf("Item added\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
@ -254,7 +254,7 @@ void update_item(struct inventory db,int index){
printf("Item updated\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
@ -262,7 +262,7 @@ void remove_item(struct inventory db,int index){
printf("Remove item\n");
printf("Are you sure you want to remove this item? (y/n)\n");
char choice;
fflush(stdin);
fflush_stdin();
scanf("%c", &choice);
db.row[index].isdeleted = true;
if(choice == 'y'){
@ -275,7 +275,7 @@ void remove_item(struct inventory db,int index){
printf("Item not removed\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
@ -293,6 +293,13 @@ void tran_control(){
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nTransaction control");
switch(choice){
case 1:
char * SortItems[] = {
"Date&Time",
"product(barcode)",
"quantity",
"total",
};
lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran);)
list_tran(tran);
break;
case 2:
@ -348,7 +355,7 @@ void list_tran(struct transaction db){
do{
valid = true;
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
if(choice <=8 && choice > 0){
printf("sorting...\n");
@ -369,7 +376,7 @@ void list_tran(struct transaction db){
}
}else if(choice == current_page_size+3){
printf("Enter page size: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &page_size);
total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){
@ -436,7 +443,7 @@ struct transaction showTran(struct transaction db,int index){
bool valid = true;
do{
printf("Please input your choice: ");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
switch(choice){
case 1:
@ -466,7 +473,7 @@ void add_tran(){
struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row));
char check;
printf("Use current date time? (y/n): ");
fflush(stdin);
fflush_stdin();
scanf("%c",&check);
if(check == 'y'){
row->date = get_date();
@ -498,7 +505,7 @@ void add_tran(){
printf("Item added\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
@ -531,7 +538,7 @@ struct transaction update_tran(struct transaction db,int index){
printf("Transaction updated\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return db;
@ -546,7 +553,7 @@ struct transaction remove_tran(struct transaction db,int index){
printf("Transaction deleted\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return read_db_tran();
@ -619,7 +626,7 @@ void user_control(){
printf("2. List User\n");
printf("3. Exit\n");
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
switch(choice){
case 1:
@ -633,7 +640,7 @@ void user_control(){
default:
printf("Invalid choice\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
break;
}
@ -660,7 +667,7 @@ struct user add_user(struct user db){
printf("User added\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return read_db_user();
@ -681,7 +688,7 @@ struct user edit_user(struct user db,int index){
printf("User updated\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return db;
@ -695,7 +702,7 @@ struct user delete_user(struct user db,int index){
printf("User deleted\n");
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return read_db_user();
@ -738,7 +745,7 @@ struct user list_user(struct user db){
do{
valid = true;
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
if(choice <=6 && choice > 0){
printf("sorting...\n");
@ -759,7 +766,7 @@ struct user list_user(struct user db){
}
}else if(choice == current_page_size+3){
printf("Enter page size: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &page_size);
total_pages = ceil((double)row / page_size);
}else if(choice >= 7 && choice <= current_page_size){
@ -782,6 +789,7 @@ void print_user(struct user db, int cur, int end,struct Map* map){
for(int i = cur; i < end; i++){
if(map == NULL){
bool isadmin = is_admin(db.row[i].role);
// printf("isadmin%s\n",db.row[i].id);
printf("%-5d%-20s%-10ld%-10s%-10c\n",i+7,db.row[i].name,db.row[i].id,db.row[i].role,isadmin?'Y':'N');
}else{
int index = map[i].key;
@ -850,7 +858,7 @@ struct user showUser(struct user db,int index){
printf("1 edit user\n");
printf("2 delete user\n");
printf("Enter choice: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &choice);
switch (choice){
case 0:
@ -864,7 +872,7 @@ struct user showUser(struct user db,int index){
default:
printf("Invalid choice\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
break;
}
@ -904,18 +912,18 @@ void role_control(){
void add_role(){
char role[100];
printf("Enter Admin role name: ");
fflush(stdin);
fflush_stdin();
scanf("%s", role);
if(is_admin(role)){//check if role exist in admin file
printf("Admin role already exist\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}else{
add_admin(role);
printf("role added\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
return;
@ -962,7 +970,7 @@ void list_role(){
do{
valid = true;
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d", &choice);
if(choice <=4 && choice > 0){
printf("sorting...\n");
@ -983,7 +991,7 @@ void list_role(){
}
}else if(choice == current_page_size+3){
printf("Enter page size: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &page_size);
total_pages = ceil( (double)list_size / page_size);
page = 0;
@ -1032,7 +1040,7 @@ void showRole(struct linkedlist* list,int index){
do{
valid = true;
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d", &choice);
if(choice == 1){
if(is_admin(name)){
@ -1042,7 +1050,7 @@ void showRole(struct linkedlist* list,int index){
}
printf("Admin role toggled\n");
printf("Press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}else if(choice != 2){
printf("Invalid choice\n");

View File

@ -127,7 +127,7 @@ bool is_admin(char* role){
while(fgets(line,100,fp) != NULL){
if(line[0] == '#') continue;
if(line[strlen(line)-1] == '\n')
while(line[strlen(line)-1] == '\n' || (int)line[strlen(line)-1] == 13)
line[strlen(line)-1] = '\0';
if(strcmp(line,role) == 0){
@ -169,7 +169,7 @@ void remove_admin(char* role){
fprintf(fp2,"%s",line);
continue;
}
if(line[strlen(line)-1] == '\n')//remove newline for compare
while(line[strlen(line)-1] == '\n' || (int)line[strlen(line)-1] == 13)//remove newline for compare
line[strlen(line)-1] = '\0';
if(strcmp(line,role) != 0){
fprintf(fp2,"%s\n",line);
@ -222,10 +222,12 @@ struct inventory read_db_invt(){//please open file in read mode
if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)] = '\0';
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0';
}
switch(j){
case category:
strcpy(db.row[i].category,buffer);
@ -282,9 +284,12 @@ struct transaction read_db_tran(){
if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)] = '\0';
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0';
}
switch(j){
case date:
db.row[i].date = convert_to_date(buffer);
@ -338,9 +343,12 @@ struct user read_db_user(){
if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)] = '\0';//prevent any garbage value
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0';
}
switch(j){
case name:
strcpy(db.row[i].name,buffer);
@ -732,7 +740,8 @@ struct user_row* get_user(long userid){
if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
if(buffer[strlen(buffer)-1] == '\n'){
buffer[strlen(buffer)] = '\0';
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0';
}

2
main.c
View File

@ -51,7 +51,7 @@ int main(){
break;
}
fflush(stdin);
fflush_stdin();
} while (check);
return 0;

View File

@ -25,7 +25,8 @@
void list_items();
void search_item();
void self_help_sale_system();
void print_page(struct inventory db, int cur, int end,struct Map* map);
void print_page(void * ddb, int cur, int end,struct Map* map);
struct Map* sortItems(void * ddb, int sort);
//main of normal user
void normal_menu(){
@ -55,8 +56,8 @@ void normal_menu(){
}
//universal functions for normal user
struct Map* sortItems(struct inventory db, int sort);
struct inventory show_item(struct inventory db,int index){
void * show_item(void * ddb,int index){
struct inventory db = *((struct inventory*)ddb);
Cls();
printf("Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category);
@ -65,105 +66,105 @@ struct inventory show_item(struct inventory db,int index){
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
printf("Press any key to return to the list\n");
fflush(stdin);
fflush_stdin();
getchar();
return db;
return ddb;
}
//user for showing list result and search result
void list_page(struct inventory db,struct Map* map,int row,struct inventory (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty
int choice = -1;
int page = 0;
int page_size = PAGE_SIZE;
int total_pages = ceil((double)row / page_size);
do{
// void list_page(struct inventory db,struct Map* map,int row,struct inventory (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty
// int choice = -1;
// int page = 0;
// int page_size = PAGE_SIZE;
// int total_pages = ceil((double)row / page_size);
// do{
//options
Cls();
printf("0 exit\n");
printf("1 sort name decending\n");
printf("2 sort name ascending\n");
printf("3 sort price decending\n");
printf("4 sort price ascending\n");
printf("5 sort brand decending\n");
printf("6 sort brand ascending\n");
printf("7 sort category decending\n");
printf("8 sort category ascending\n");
printf("List of items:\n");
// //options
// Cls();
// printf("0 exit\n");
// printf("1 sort name decending\n");
// printf("2 sort name ascending\n");
// printf("3 sort price decending\n");
// printf("4 sort price ascending\n");
// printf("5 sort brand decending\n");
// printf("6 sort brand ascending\n");
// printf("7 sort category decending\n");
// printf("8 sort category ascending\n");
// printf("List of items:\n");
//print items
if(page + 1 >= total_pages){
print_page(db, page * page_size, row,map);
}else{
print_page(db, page * page_size, (page + 1) * page_size,map);
}
// //print items
// if(page + 1 >= total_pages){
// print_page(db, page * page_size, row,map);
// }else{
// print_page(db, page * page_size, (page + 1) * page_size,map);
// }
//page control
int current_page_size = 8 + page_size;
if(page + 1 >= total_pages){
current_page_size = row - page * page_size+8;
}
printf("%d next page\n", current_page_size+1);
printf("%d previous page\n", current_page_size+2);
printf("%d set page size\n", current_page_size+3);
printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages);
// //page control
// int current_page_size = 8 + page_size;
// if(page + 1 >= total_pages){
// current_page_size = row - page * page_size+8;
// }
// printf("%d next page\n", current_page_size+1);
// printf("%d previous page\n", current_page_size+2);
// printf("%d set page size\n", current_page_size+3);
// printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages);
//prompt user to select an item
bool valid = true;
do{
valid = true;
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
if(choice <=8 && choice > 0){
printf("sorting...\n");
map = sortItems(db,choice);
}else if(choice == current_page_size+1 ){
if(page + 1 < total_pages){
page++;
}else{
printf("Already at last page\n");
valid = false;
}
}else if(choice == current_page_size+2){
if(page > 0){
page--;
}else{
printf("Already at first page\n");
valid = false;
}
}else if(choice == current_page_size+3){
printf("Enter page size: ");
fflush(stdin);
scanf("%d", &page_size);
total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page);
}else{
db = (*showitem)(db,map[choice - 9 + page_size*page].key);
}
}else if(choice != 0){
printf("Invalid choice\n");
valid = false;
}
}while(!valid);
// //prompt user to select an item
// bool valid = true;
// do{
// valid = true;
// printf("Enter your choice: ");
// fflush_stdin();
// scanf("%d", &choice);
// if(choice <=8 && choice > 0){
// printf("sorting...\n");
// map = sortItems(db,choice);
// }else if(choice == current_page_size+1 ){
// if(page + 1 < total_pages){
// page++;
// }else{
// printf("Already at last page\n");
// valid = false;
// }
// }else if(choice == current_page_size+2){
// if(page > 0){
// page--;
// }else{
// printf("Already at first page\n");
// valid = false;
// }
// }else if(choice == current_page_size+3){
// printf("Enter page size: ");
// fflush_stdin();
// scanf("%d", &page_size);
// total_pages = ceil((double)row / page_size);
// }else if(choice >= 9 && choice <= current_page_size){
// if(map == NULL){
// db = (*showitem)(db,choice - 9 + page_size*page);
// }else{
// db = (*showitem)(db,map[choice - 9 + page_size*page].key);
// }
// }else if(choice != 0){
// printf("Invalid choice\n");
// valid = false;
// }
// }while(!valid);
}while(choice != 0);
}
// }while(choice != 0);
// }
//print the list for the page
void print_page(struct inventory db, int cur, int end,struct Map* map){
for (int i = cur; i < end; i++)
{
if(map != NULL){
printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product);
}
else{
printf("%d item %d: %s\n", i + 9,i, db.row[i].product);
}
}
}
void Print_page(void * ddb, int cur, int end,struct Map* map){
// void print_page(struct inventory db, int cur, int end,struct Map* map){
// for (int i = cur; i < end; i++)
// {
// if(map != NULL){
// printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product);
// }
// else{
// printf("%d item %d: %s\n", i + 9,i, db.row[i].product);
// }
// }
// }
void print_page(void * ddb, int cur, int end,struct Map* map){
struct inventory db = *((struct inventory*)ddb);
for (int i = cur; i < end; i++)
{
@ -194,66 +195,12 @@ void list_items(){
"Brand",
"Category",
};
// FIXME:function for normal list page
// lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,item_control);
list_page(db,map,db.db.row_count,show_item);
lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,show_item);
}
//for sorting
struct Map* sortItems(struct inventory db, int sort){
// struct Map* sortItems(void * ddb, int sort){
// struct inventory db = *((struct inventory*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
for (int i = 0; i < db.db.row_count-1; i++){
map[i].key = i;
switch(sort){
case 1:
case 2:
map[i].value = (void*)db.row[i].product;
break;
case 3:
case 4:;
double price = db.row[i].price * 100;
double * price_star = malloc(sizeof(long));
*price_star = price;
map[i].value = (void*)price_star;//presume there is no price contain 0.001
break;
case 5:
case 6:
map[i].value = (void*)db.row[i].brand;
break;
case 7:
case 8:
map[i].value = (void*)db.row[i].category;
break;
}
}
switch (sort){
case 1:
case 5:
case 7:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending_str);
break;
case 2:
case 6:
case 8:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending_str);
break;
case 3:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending);
break;
case 4:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending);
break;
}
map = realloc(map, sizeof(struct Map) * db.db.row_count);
map[db.db.row_count-1].key = db.db.row_count-1;
return map;
}
struct Map* SortItems(void * ddb, int sort){
struct Map* sortItems(void * ddb, int sort){
struct inventory db = *((struct inventory*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
for (int i = 0; i < db.db.row_count-1; i++){
@ -332,19 +279,17 @@ void search_item(){
map = searchItems(db,searchstr);
if(map[0].value > 0){
int temp_row = *((int*)map[0].value);
// char * SortItems[] = {
// "Name",
// "Price",
// "Brand",
// "Category",
// };
//FIXME
// lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,item_control);//ofset map, as it is use to store the size
list_page(db,map+1,temp_row,show_item);//ofset map, as it is use to store the size
char * SortItems[] = {
"Name",
"Price",
"Brand",
"Category",
};
lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,show_item);//ofset map, as it is use to store the size
}else{//empty search
printf("No result found\n");
printf("Press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}
@ -356,7 +301,7 @@ void search_item(){
char* prompt_search(){
printf("Enter search string:(largest length 100)(case insesitive)\n");
char* searchstr = malloc(sizeof(char) * 100);
fflush(stdin);
fflush_stdin();
scanf("%s", searchstr);
return searchstr;
}
@ -521,23 +466,23 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
printf("product: %s\n",row->product);
printf("price: %.1f\n",row->price);
printf("Enter quantity(0 to cancel)\n>");\
fflush(stdin);
fflush_stdin();
scanf("%d", &quantity);
fflush(stdin);
fflush_stdin();
if(quantity == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}
printf("Are you sure you want to add this item to cart?(y/n)\n>");
fflush(stdin);
fflush_stdin();
scanf("%c", &choice);
if(row->stock - quantity < 0 && choice == 'y'){
printf("WARNING:not enough stock\n");
printf("Are you sure the quantity is correct?\n(y to ignore and continue/n)\n>");
fflush(stdin);
fflush_stdin();
scanf("%c", &choice);
}
}while(choice != 'y');
@ -550,7 +495,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
}
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}
@ -559,7 +504,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
long prompt_barcode(){
printf("Please scan the qr code(or input the barcode ) ");
long barcode;
fflush(stdin);
fflush_stdin();
scanf("%ld", &barcode);
return barcode;
}
@ -611,7 +556,7 @@ struct cart* list_cart(struct cart* cart){
}
do{
printf("input the corresponding value for more action\n>");
fflush(stdin);
fflush_stdin();
scanf("%d", &choice);
if(choice >0 && choice < i){
cart = cart_control(cart,choice);
@ -644,13 +589,13 @@ struct cart* cart_control(struct cart* cart,int index){
cart = Cartremove(cart,index-1);
printf("Remove successful\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
}else if(choice == 2){
cart = update_cart(cart,index);
printf("Update successful\n");
printf("press any key to continue");
fflush(stdin);
fflush_stdin();
getchar();
}
}while(choice != 1 || choice != 2 || choice !=3 );
@ -664,10 +609,10 @@ struct cart* update_cart(struct cart* cart,int index){
do{
int quantity = 0;
printf("enter the new quantity: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &quantity);
printf("Are you sure you want to update this item?(y/n/e to exit)");
fflush(stdin);
fflush_stdin();
scanf("%c", &choice);
if(choice == 'y'){
@ -695,7 +640,7 @@ struct cart* checkout(struct cart* cart){
if(user_id == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}
@ -705,33 +650,33 @@ struct cart* checkout(struct cart* cart){
if(user == NULL){
printf("Fail to indentify your card or error occurs\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}else{
printf("Welcome %s(%s)\n",user->name,user->role);
printf("Are you sure you want to checkout?(y/n)\n>");
char choice;
fflush(stdin);
fflush_stdin();
scanf("%c", &choice);
if(choice == 'y'){
if(update_stock_N_checkout(cart,user)){//catch err
printf("Checkout successful\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return NULL;
}else{
printf("Checkout failed(files maybe corrputed)\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}
}else{
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
fflush_stdin();
getchar();
return cart;
}

25
utils.h
View File

@ -9,7 +9,10 @@
#include<ctype.h>
#include <stddef.h>
#endif
#ifdef _WIN32
#else
#include <stdio_ext.h>
#endif
void Cls(){
#ifdef _WIN32
system("cls");
@ -17,6 +20,13 @@ void Cls(){
system("clear");
#endif
}
void fflush_stdin(){
#ifdef _WIN32
fflush(stdin);
#else
__fpurge(stdin);
#endif
}
void welcome_message(){
printf("><Welcome to the Student Union POS system><\n");//welcome messgae
@ -37,11 +47,11 @@ int choices_selecter(char * Items[],int items,char * welcome_messages){
}
printf("%d. Exit\n",i+1);
printf(">");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
if(choice < 1 || choice > items+1){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
fflush_stdin();
getchar();
}
}while(choice < 1 || choice > items+1);
@ -53,12 +63,12 @@ char* prompt_item(char* prompt,bool empty_allowed){
char* item = malloc(sizeof(char) * 100);
do{
printf("%s",prompt);
fflush(stdin);
fflush_stdin();
scanf("%[^\n]",item);//input until /n
if(!empty_allowed&&(item,"") == 0){//check if temp is not empty
printf("Invalid input, try again?(y/n)\n");
char temp[100];
fflush(stdin);
fflush_stdin();
scanf("%[^\n]",temp);
if(strcmp(temp,"n") == 0){
return NULL;
@ -97,7 +107,6 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed)
return true;
}
//TODO: lister still useable
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct Map*) , struct Map* (*sorter)(void *,int),void * (*showitem)(void *,int)){
int choice = -1;
int page = 0;
@ -131,7 +140,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
do{
valid = true;
printf("Please input your choice\n>");
fflush(stdin);
fflush_stdin();
scanf("%d",&choice);
if(choice <=NoSortItems && choice > 0){
printf("sorting...\n");
@ -152,7 +161,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
}
}else if(choice == current_page_size+3){
printf("Enter page size: ");
fflush(stdin);
fflush_stdin();
scanf("%d", &page_size);
total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){