Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<errno.h> //
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define BUFFER_SIZE
  6. constexpr auto max = 5;
  7. #define _CRT_SECURE_NO_WARNINGS 1
  8. #define _WINSOCK_DEPRECATED_NO_WARNINGS 1
  9. #pragma warning(disable:4996)
  10. struct vending_machine
  11. {
  12. char MN[20]; //alloc mem size of char
  13. char ML[20];
  14. int stat, pin, index; //ints for program
  15. };
  16. struct vending_machine Machine;
  17. int array_search = 0;
  18. struct vending_machine Add_New_Machine; //struct to add new vending machine
  19. struct vending_machine Max_machine[max];
  20. int menu() {
  21. int option;
  22. printf("***************************************************************\n");
  23. printf("* Please select a option *\n");
  24. printf("* 1. Add Machine *\n");
  25. printf("* 2. Show All Machines *\n");
  26. printf("* 3. Search Machine By Index *\n");
  27. printf("* 4. Delete Machine *\n");
  28. printf("* 5. Update Machine Status *\n");
  29. printf("* 6. Exit *\n");
  30. printf("***************************************************************\n");
  31. scanf_s("%d", &option);
  32. return option;
  33. }
  34. void Add_machine()
  35. {
  36. FILE * file_pointer;
  37. char mn[20], location[20];
  38. int Machine_pin, Machine_status;
  39. file_pointer = fopen("machine.txt", "w+");
  40. printf("Machine Name::\n");
  41. scanf_s("%s", mn, sizeof(mn)); //gets size of machine name stops bs when writing to a file
  42. fputs(mn, file_pointer);
  43. strcpy_s(Add_New_Machine.MN, 20, mn); //pushes name to assigned value
  44. printf("Location::\n");
  45. scanf_s("%s", location, sizeof(location));
  46. strcpy_s(Add_New_Machine.ML, 20, location);
  47. fputs(location, file_pointer);
  48. printf("Pin Number\n");
  49. scanf_s("%i", &Machine_pin);
  50. Add_New_Machine.pin = Machine_pin;
  51. printf("Status::\n");
  52. scanf_s("%i", &Machine_status);
  53. Add_New_Machine.index = array_search;
  54. Max_machine[array_search] = Add_New_Machine; //adds value to max assignmed machines
  55. fclose(file_pointer);
  56. }
  57. void List_machines()
  58. {
  59. for (int i = 0; i < array_search; i++) //FOR LOOP EVERYTHING NO NEED FOR A WHILE
  60. {
  61. printf("Name: %s Room: %s Machine Pin: %i Machine Status: %i Machine Index: %i\n",
  62. Max_machine[i].MN,Max_machine[i].ML,Max_machine[i].pin,Max_machine[i].stat,Max_machine[i].index);
  63. }
  64.  
  65. }
  66. void search()
  67. {
  68. int i;
  69. scanf_s("%i", &i);
  70. printf("search the index *the number no chars *\n");
  71. printf("%s %s %i %i %i\n",
  72. Max_machine[i].MN,
  73. Max_machine[i].ML,
  74. Max_machine[i].pin,
  75. Max_machine[i].stat,
  76. Max_machine[i].index);
  77. }
  78. void update()
  79. {
  80. int i = 0;
  81. printf("Enter index");
  82. scanf_s("%i", &i);
  83. //pinMode(machines[i].pin, OUTPUT);
  84. if (Max_machine[i].stat == 0)
  85. {
  86. Max_machine[i].stat = 1;
  87. //This turns pin on
  88. int pin = Max_machine[i].pin;
  89. //digitalWrite(pin,HIGH);
  90. printf("%s on \n", Max_machine[i].MN);
  91. }
  92. else
  93. {
  94. Max_machine[i].stat = 0;
  95. //Turning pin off
  96. int pin = Max_machine[i].pin;
  97. printf("%s off \n", Max_machine[i].MN);
  98.  
  99. }
  100. }
  101. void kill() {
  102.  
  103. int i, index;
  104. printf("Enter index to remove\n");
  105. scanf_s("%i", &index);
  106. for (i = index; i < array_search - 1; i++)
  107. {
  108. Max_machine[i] = Max_machine[i + 1];
  109. }
  110. printf(" Removed \n");
  111. array_search -= 1;
  112. }
  113. int machine_count()
  114. {
  115. FILE *fptr;
  116. errno_t err;
  117. int count = 0;
  118. char c;
  119. if ((err = fopen_s(&fptr, "machine.txt", "r")) != NULL)
  120. {
  121. printf("Error! opening file\n");
  122. return -1;
  123. }
  124. while (!feof(fptr))
  125. {
  126. c = fgetc(fptr);
  127. if (c == '\n')
  128. {
  129. count++;
  130. }
  131. }
  132. fclose(fptr);
  133. return count;
  134. }
  135. int main()
  136. {
  137. //Init Setup
  138.  
  139.  
  140. printf("Allocated Mem: %i\n", array_search);
  141. int option = 1;
  142. while (option != 0)
  143. {
  144. option = menu();
  145. switch (option)
  146. {
  147. case 1:
  148. Add_machine();
  149. break;
  150. case 2:
  151. List_machines();
  152. break;
  153. case 3:
  154. search();
  155. break;
  156. case 4:
  157. kill();
  158. break;
  159. case 5:
  160. update();
  161. break;
  162. case 6:
  163.  
  164. default:
  165. break;
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement