Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //updated 10:48pm 22/12 added few comments
- //updated 4:28pm 23/12 comment of how to use save load
- //updated 4:54pm added function declaration and function with parameters (with and without pointers)
- #include <stdio.h>
- typedef struct{
- char orderId[6], foodId[6], memberId[6];
- int quantity;
- } Order;
- //Global variable (you can change it to local, but need to pass by reference to each function)
- //Structure list (for faster read/write)
- Order orderList[100];
- //How many structure inside the list?
- int orderCount=0;
- //function declaration
- void del();
- void search();
- void modify();
- void add()
- void display();
- int userIsConfirm(char *print);
- void load();
- void save();
- void getUserInputOrderWithPointers(int isModify, Order *order);
- Order getUserInputOrder(int isModify);
- void main(){
- int choice;
- load(); //read data once enough, then no need read again, only write it back
- //load data to orderlist, then we modify the list, and save it back
- //so no need to read it as the data is stored in the list
- do{
- printf("\n\n1. Add\n2. Display\n3. Search\n4. Modify\n5. Delete\n6. Quit\nEnter: ");
- rewind(stdin);
- scanf("%d", &choice);
- if(choice == 1){
- add();
- }else if(choice == 2){
- display();
- }else if(choice == 3){
- search();
- }else if(choice == 4){
- modify();
- }else if(choice == 5){
- del();
- }
- }while(choice != 6);
- }
- void del(){
- //i use order to store data, so dont need to declare many string
- Order orderTemp;
- char userConfirm;
- int orderIndex, isFound, i;
- //initialize
- isFound=0; //0 = false, rest = true
- //display to user so they know which one to delete
- display();
- //get user input for which order they want to delete
- printf("Enter record of order ID to delete : ");
- rewind(stdin);
- gets(orderTemp.orderId);
- //find data from list
- for(i=0; i<orderCount; i++){
- //if order ID match
- if(strcmp(orderList[i].orderId, orderTemp.orderId) == 0){
- //save current index to orderIndex, for further reference
- orderIndex = i;
- //isFound = 1 or "true"
- isFound = 1;
- //this one is optional, but for long array (>10000) it is better to do this
- break;
- }
- }
- //found it! Get user input to update data
- if(isFound){
- if(userIsConfirm("Confirm to delete (Y/N)? ")){
- //reduced 1 structure
- orderCount--;
- for(i=orderIndex; i<orderCount; i++){
- //moving data one step forward, eg: 1 2 3 4 5, remove 2, become 1 3 4 5
- orderList[i] = orderList[i+1];
- }
- }
- }else{
- printf("No data found :(\n");
- }
- }
- void search(){
- Order orderTemp;
- int i;
- //give user option to which column they want to find
- //optional: use if/switch statement to get which column they want to find
- printf("Leave blank to skip\n");
- printf("Enter record of order ID to search: ");
- rewind(stdin);
- gets(orderTemp.orderId);
- printf("Enter record of food ID to search: ");
- rewind(stdin);
- gets(orderTemp.foodId);
- printf("Enter record of member ID to search: ");
- rewind(stdin);
- gets(orderTemp.memberId);
- //find data from list
- printf("ORDER ID | FOOD ID | MEMBER ID | QUANTITY\n");
- printf("+++++++++|+++++++++|+++++++++++|+++++++++\n");
- for(i=0; i<orderCount;i++){
- //check if it is not empty, if it is not empty, then check memberId is same with what user want
- //if it is empty, then the first character in the string is '\0'
- if((orderTemp.orderId[0] != '\0' && strcmp(orderList[i].orderId, orderTemp.orderId) == 0) ||
- (orderTemp.foodId[0] != '\0' && strcmp(orderList[i].foodId, orderTemp.foodId) == 0) ||
- (orderTemp.memberId[0] != '\0' && strcmp(orderList[i].memberId, orderTemp.memberId) == 0)){
- printf(" %-7s | %-7s | %-9s | %d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
- }
- }
- }
- void modify(){
- //i use order to store data, so dont need to declare many string
- Order orderTemp;
- char userConfirm;
- int orderIndex, isFound, i;
- //initialize
- isFound=0; //0 = false, rest = true
- //display to user so they know which one to modify
- display();
- //get user input for which order they want to modify
- printf("Enter record of order ID to modify : ");
- rewind(stdin);
- gets(orderTemp.orderId);
- //find data from list
- for(i=0; i<orderCount; i++){
- //if order ID is match
- if(strcmp(orderList[i].orderId, orderTemp.orderId) == 0){
- //save current index to orderIndex, for further reference
- orderIndex = i;
- //isFound = 1 or "true"
- isFound = 1;
- //this one is optional, but for long array (>10000) it is better to do this
- break;
- }
- }
- //found it! Get user input to update data
- if(isFound){
- //get all data into temporary variable (here is orderTemp)
- //we use pointer to edit as the orderId is in ordertemp
- getUserInputOrderWithPointers(1, &orderTemp);
- //get confirmation to change
- if(userIsConfirm("Confirm to modify (Y/N)? ")){
- //structure can use =
- orderList[orderIndex] = orderTemp;
- save();//save after updated
- printf("Successfully updated list!\n");
- }
- }else{
- printf("No data found :(\n");
- }
- }
- void add(){
- Order order;
- //using function with parameter to get user input
- order = getUserInputOrder(0);
- //put in the order to order list
- orderList[orderCount] = order;
- //how above line works:
- //imagine currently has 0 order, so orderCount is 0
- //if you want to store something, you would store it at the first index (0), as there is no data
- //which we store it at orderList[0] = order;
- //and we can directly do orderList[orderCount] = order;
- //add 1 to total count, eg: now has 0, added, then become 1
- orderCount++;
- //now has 1 order, and orderCount is 1
- //when next time you want to store a data, you would store it at the second index (1), as there is no data
- //which we store it at orderList[1] = order;
- //and we can directly do orderList[orderCount] = order;
- //or a shorter code, combing both line become orderList[orderCount++] = order;
- save();//save after updated
- }
- void getUserInputOrderWithPointers(int isModify, Order *order){
- //using pointers to directly modify the order
- //if is not modify, then it is new, so we get user input for order
- if(isModify == 0){
- printf("Enter order ID: ");
- rewind(stdin);
- gets(order->orderId); //or gets((*order).orderId);
- }
- printf("Enter %sfood ID: ", isModify ? "new " : "");
- rewind(stdin);
- gets(order->foodId); //or gets((*order).foodId);
- printf("Enter %smember ID: ", isModify ? "new " : "");
- rewind(stdin);
- gets(order->memberId); //or gets((*order).memberId);
- printf("Enter %squantity: ", isModify ? "new " : "");
- scanf("%d", &(order->quantity)); //or scanf("%d", &(*order).quantity);
- }
- Order getUserInputOrder(int isModify){
- //return a new order back to the line where it call
- Order order;
- //if is not modify, then it is new, so we get user input for order
- if(isModify == 0){
- printf("Enter order ID: ");
- rewind(stdin);
- gets(order.orderId);
- }
- //the isModify ? "new " : "" is same as if(isModify){ "new" } else { "" } in a simplier form
- printf("Enter %sfood ID: ", isModify ? "new " : "");
- rewind(stdin);
- gets(order.foodId);
- printf("Enter %smember ID: ", isModify ? "new " : "");
- rewind(stdin);
- gets(order.memberId);
- printf("Enter %squantity: ", isModify ? "new " : "");
- scanf("%d", &order.quantity);
- return order;
- }
- void display(){
- int i;
- printf("ORDER ID | FOOD ID | MEMBER ID | QUANTITY\n");
- printf("+++++++++|+++++++++|+++++++++++|+++++++++\n");
- for(i=0;i<orderCount;i++){
- printf(" %-7s | %-7s | %-9s | %d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
- }
- }
- void save(){
- FILE *fptr=fopen("order.txt", "w");
- int i;
- if(fptr==NULL){
- printf("Error! Unable open file!\n");
- return;
- }
- //write data from index 0 to orderCount-1 (which is from first to last)
- for(i=0; i<orderCount;i++){
- fprintf(fptr, "%s|%s|%s|%d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
- }
- fclose(fptr);
- }
- void load(){
- FILE *fptr=fopen("order.txt", "r");
- if(fptr==NULL){
- printf("Error! Unable open file!\n");
- return;
- }
- orderCount = 0;
- //read data
- while(fscanf(fptr, "%[^|]|%[^|]|%[^|]|%d\n", orderList[orderCount].orderId, orderList[orderCount].foodId, orderList[orderCount].memberId, &orderList[orderCount].quantity)!=EOF){
- //orderCount increase by one with each data has been written to orderList
- orderCount++;
- }
- fclose(fptr);
- }
- int userIsConfirm(char *print){
- char userConfirm;
- do{
- printf("%s", print);
- rewind(stdin);
- userConfirm = tolower(getchar());
- if(userConfirm != 'y' && userConfirm != 'n')
- printf("Error! Invalid input! Either Y or N!\n");
- }while(userConfirm != 'y' && userConfirm != 'n');
- return userConfirm == 'y';
- }
Advertisement
Add Comment
Please, Sign In to add comment