fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_PRODUCTS 100
  6. #define FILENAME "products.txt"
  7.  
  8. // Struct for storing product information
  9. typedef struct {
  10. int id;
  11. char name[50];
  12. float price;
  13. int stock;
  14. } Product;
  15.  
  16. // Function declarations
  17. void addProduct(Product products[], int *productCount);
  18. void displayProducts(Product products[], int productCount);
  19. void editProduct(Product products[], int productCount);
  20. void deleteProduct(Product products[], int *productCount);
  21. void sellProduct(Product products[], int *productCount);
  22. void saveToFile(Product products[], int productCount);
  23. void loadFromFile(Product products[], int *productCount);
  24.  
  25. int main() {
  26. Product products[MAX_PRODUCTS];
  27. int productCount = 0;
  28. int choice;
  29.  
  30. // Load data from file
  31. loadFromFile(products, &productCount);
  32.  
  33. while (1) {
  34. printf("\n===== Shop Management System =====\n");
  35. printf("1. Add New Product\n");
  36. printf("2. Display Products\n");
  37. printf("3. Edit Product Information\n");
  38. printf("4. Delete Product\n");
  39. printf("5. Sell Product\n");
  40. printf("6. Save Data to File\n");
  41. printf("7. Exit Program\n");
  42. printf("Select an option: ");
  43. scanf("%d", &choice);
  44.  
  45. switch (choice) {
  46. case 1:
  47. addProduct(products, &productCount);
  48. break;
  49. case 2:
  50. displayProducts(products, productCount);
  51. break;
  52. case 3:
  53. editProduct(products, productCount);
  54. break;
  55. case 4:
  56. deleteProduct(products, &productCount);
  57. break;
  58. case 5:
  59. sellProduct(products, &productCount);
  60. break;
  61. case 6:
  62. saveToFile(products, productCount);
  63. break;
  64. case 7:
  65. printf("Saving data and exiting the program\n");
  66. saveToFile(products, productCount);
  67. return 0;
  68. default:
  69. printf("Invalid option!\n");
  70. }
  71. }
  72. }
  73.  
  74. void addProduct(Product products[], int *productCount) {
  75. if (*productCount >= MAX_PRODUCTS) {
  76. printf("Cannot add more products\n");
  77. return;
  78. }
  79.  
  80. Product newProduct;
  81. newProduct.id = *productCount + 1;
  82.  
  83. printf("Product Name: ");
  84. scanf(" %[^
  85. ]", newProduct.name);
  86.  
  87. printf("Price: ");
  88. scanf("%f", &newProduct.price);
  89.  
  90. printf("Stock Quantity: ");
  91. scanf("%d", &newProduct.stock);
  92.  
  93. products[*productCount] = newProduct;
  94. (*productCount)++;
  95.  
  96. printf("Product added successfully!\n");
  97. }
  98.  
  99. void displayProducts(Product products[], int productCount) {
  100. if (productCount == 0) {
  101. printf("No products available\n");
  102. return;
  103. }
  104.  
  105. printf("\nProduct List\n");
  106. printf("====================\n");
  107. for (int i = 0; i < productCount; i++) {
  108. printf("ID: %d | Name: %s | Price: %.2f | Stock: %d\n",
  109. products[i].id, products[i].name, products[i].price, products[i].stock);
  110. }
  111. }
  112.  
  113. void editProduct(Product products[], int productCount) {
  114. int id;
  115. printf("Enter the product ID to edit: ");
  116. scanf("%d", &id);
  117.  
  118. for (int i = 0; i < productCount; i++) {
  119. if (products[i].id == id) {
  120. printf("New Product Name: ");
  121. scanf(" %[^
  122. ]", products[i].name);
  123.  
  124. printf("New Price: ");
  125. scanf("%f", &products[i].price);
  126.  
  127. printf("New Stock Quantity: ");
  128. scanf("%d", &products[i].stock);
  129.  
  130. printf("Product information updated successfully!\n");
  131. return;
  132. }
  133. }
  134. printf("Product with ID %d not found\n", id);
  135. }
  136.  
  137. void deleteProduct(Product products[], int *productCount) {
  138. int id;
  139. printf("Enter the product ID to delete: ");
  140. scanf("%d", &id);
  141.  
  142. for (int i = 0; i < *productCount; i++) {
  143. if (products[i].id == id) {
  144. for (int j = i; j < *productCount - 1; j++) {
  145. products[j] = products[j + 1];
  146. }
  147. (*productCount)--;
  148. printf("Product deleted successfully!\n");
  149. return;
  150. }
  151. }
  152. printf("Product with ID %d not found\n", id);
  153. }
  154.  
  155. void sellProduct(Product products[], int *productCount) {
  156. int id, quantity;
  157. printf("Enter the product ID to sell: ");
  158. scanf("%d", &id);
  159.  
  160. for (int i = 0; i < *productCount; i++) {
  161. if (products[i].id == id) {
  162. printf("Quantity to sell: ");
  163. scanf("%d", &quantity);
  164.  
  165. if (quantity > products[i].stock) {
  166. printf("Not enough stock!\n");
  167. } else {
  168. products[i].stock -= quantity;
  169. printf("Product sold successfully. Total: %.2f\n", products[i].price * quantity);
  170. }
  171. return;
  172. }
  173. }
  174. printf("Product with ID %d not found\n", id);
  175. }
  176.  
  177. void saveToFile(Product products[], int productCount) {
  178. FILE *file = fopen(FILENAME, "w");
  179. if (file == NULL) {
  180. printf("Unable to save data to file\n");
  181. return;
  182. }
  183.  
  184. for (int i = 0; i < productCount; i++) {
  185. fprintf(file, "%d,%s,%.2f,%d\n", products[i].id, products[i].name, products[i].price, products[i].stock);
  186. }
  187.  
  188. printf("Data saved successfully!\n");
  189. }
  190.  
  191. void loadFromFile(Product products[], int *productCount) {
  192. FILE *file = fopen(FILENAME, "r");
  193. if (file == NULL) {
  194. printf("No data file found. Starting with an empty system\n");
  195. return;
  196. }
  197.  
  198. *productCount = 0;
  199. while (fscanf(file, "%d,%49[^,],%f,%d\n", &products[*productCount].id, products[*productCount].name, &products[*productCount].price, &products[*productCount].stock) != EOF) {
  200. (*productCount)++;
  201. }
  202.  
  203. printf("Data loaded successfully!\n");
  204. }
  205.  
Success #stdin #stdout 0.03s 25620KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PRODUCTS 100
#define FILENAME "products.txt"

// Struct for storing product information
typedef struct {
    int id;
    char name[50];
    float price;
    int stock;
} Product;

// Function declarations
void addProduct(Product products[], int *productCount);
void displayProducts(Product products[], int productCount);
void editProduct(Product products[], int productCount);
void deleteProduct(Product products[], int *productCount);
void sellProduct(Product products[], int *productCount);
void saveToFile(Product products[], int productCount);
void loadFromFile(Product products[], int *productCount);

int main() {
    Product products[MAX_PRODUCTS];
    int productCount = 0;
    int choice;

    // Load data from file
    loadFromFile(products, &productCount);

    while (1) {
        printf("\n===== Shop Management System =====\n");
        printf("1. Add New Product\n");
        printf("2. Display Products\n");
        printf("3. Edit Product Information\n");
        printf("4. Delete Product\n");
        printf("5. Sell Product\n");
        printf("6. Save Data to File\n");
        printf("7. Exit Program\n");
        printf("Select an option: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addProduct(products, &productCount);
                break;
            case 2:
                displayProducts(products, productCount);
                break;
            case 3:
                editProduct(products, productCount);
                break;
            case 4:
                deleteProduct(products, &productCount);
                break;
            case 5:
                sellProduct(products, &productCount);
                break;
            case 6:
                saveToFile(products, productCount);
                break;
            case 7:
                printf("Saving data and exiting the program\n");
                saveToFile(products, productCount);
                return 0;
            default:
                printf("Invalid option!\n");
        }
    }
}

void addProduct(Product products[], int *productCount) {
    if (*productCount >= MAX_PRODUCTS) {
        printf("Cannot add more products\n");
        return;
    }

    Product newProduct;
    newProduct.id = *productCount + 1;

    printf("Product Name: ");
    scanf(" %[^
]", newProduct.name);

    printf("Price: ");
    scanf("%f", &newProduct.price);

    printf("Stock Quantity: ");
    scanf("%d", &newProduct.stock);

    products[*productCount] = newProduct;
    (*productCount)++;

    printf("Product added successfully!\n");
}

void displayProducts(Product products[], int productCount) {
    if (productCount == 0) {
        printf("No products available\n");
        return;
    }

    printf("\nProduct List\n");
    printf("====================\n");
    for (int i = 0; i < productCount; i++) {
        printf("ID: %d | Name: %s | Price: %.2f | Stock: %d\n", 
               products[i].id, products[i].name, products[i].price, products[i].stock);
    }
}

void editProduct(Product products[], int productCount) {
    int id;
    printf("Enter the product ID to edit: ");
    scanf("%d", &id);

    for (int i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            printf("New Product Name: ");
            scanf(" %[^
]", products[i].name);

            printf("New Price: ");
            scanf("%f", &products[i].price);

            printf("New Stock Quantity: ");
            scanf("%d", &products[i].stock);

            printf("Product information updated successfully!\n");
            return;
        }
    }
    printf("Product with ID %d not found\n", id);
}

void deleteProduct(Product products[], int *productCount) {
    int id;
    printf("Enter the product ID to delete: ");
    scanf("%d", &id);

    for (int i = 0; i < *productCount; i++) {
        if (products[i].id == id) {
            for (int j = i; j < *productCount - 1; j++) {
                products[j] = products[j + 1];
            }
            (*productCount)--;
            printf("Product deleted successfully!\n");
            return;
        }
    }
    printf("Product with ID %d not found\n", id);
}

void sellProduct(Product products[], int *productCount) {
    int id, quantity;
    printf("Enter the product ID to sell: ");
    scanf("%d", &id);

    for (int i = 0; i < *productCount; i++) {
        if (products[i].id == id) {
            printf("Quantity to sell: ");
            scanf("%d", &quantity);

            if (quantity > products[i].stock) {
                printf("Not enough stock!\n");
            } else {
                products[i].stock -= quantity;
                printf("Product sold successfully. Total: %.2f\n", products[i].price * quantity);
            }
            return;
        }
    }
    printf("Product with ID %d not found\n", id);
}

void saveToFile(Product products[], int productCount) {
    FILE *file = fopen(FILENAME, "w");
    if (file == NULL) {
        printf("Unable to save data to file\n");
        return;
    }

    for (int i = 0; i < productCount; i++) {
        fprintf(file, "%d,%s,%.2f,%d\n", products[i].id, products[i].name, products[i].price, products[i].stock);
    }

    fclose(file);
    printf("Data saved successfully!\n");
}

void loadFromFile(Product products[], int *productCount) {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        printf("No data file found. Starting with an empty system\n");
        return;
    }

    *productCount = 0;
    while (fscanf(file, "%d,%49[^,],%f,%d\n", &products[*productCount].id, products[*productCount].name, &products[*productCount].price, &products[*productCount].stock) != EOF) {
        (*productCount)++;
    }

    fclose(file);
    printf("Data loaded successfully!\n");
}