Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. /*
  2. Sean McGuire, G00330886.
  3. Rugby Assignment.
  4. */
  5.  
  6. #ifdef _MSC_VER
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9.  
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12. #include<conio.h>
  13.  
  14. // define struct
  15. struct node
  16. {
  17. int IRFU;
  18. char firstName[20];
  19. char secondName[20];
  20. int age;
  21. float height;
  22. float weight;
  23. char club[20];
  24. char email[30];
  25. char pos[10];
  26. int tacklesMissed;
  27. int meters;
  28.  
  29. struct node* NEXT;
  30. };
  31.  
  32. void menu();
  33. void parseChoice(int choice);
  34. void addPlayer(struct node* headptr, int newIRFU);
  35. void addElement_AtStart(struct node** head_ptr, int newIRFU);
  36. void displayPlayers(struct node* headptr);
  37. void displayPlayersDetails(struct node* headptr);
  38. int search(struct node* headptr, int IRFU);
  39.  
  40. void main()
  41. {
  42. // Initialize all local variables.
  43. char buffer[256] = { 0 };
  44. char username[6];
  45. char password[6];
  46. char c;
  47. char line[1000];
  48. int pos = 0;
  49. int found = 0;
  50. int lineNumber = 1;
  51. int choice;
  52. // Declare and open the files used.
  53. FILE * fp;
  54. fp = fopen("users.txt", "r");
  55.  
  56. // Firstly, ask the user for their username, if it's valid, it will ask for their password.
  57. printf("Enter username: ");
  58. scanf("%s", username);
  59.  
  60. // Enter this while loop for every line the file has,
  61. // The file contains a list of usernames followed by their passwords directly after.
  62. while (fgets(line, sizeof(line), fp))
  63. {
  64. lineNumber++;
  65. // Each line ends with an end of line \n, so using strtok removes it, allowing strcmp to compare the strings
  66. strtok(line, "\n");
  67. // Check if the username entered is equal to the current lines string,
  68. // And make sure the line number is divisible by 2 as the usernames in the file are always followed by the password.
  69. if (strcmp(username, line) == 0 && lineNumber % 2 == 0)
  70. {
  71. // If a username is found, call fgets to move to the next line, call strtok again, and assign the password to the current line.
  72. fgets(line, sizeof(line), fp);
  73. strtok(line, "\n");
  74. strcpy(password, line);
  75. // Then print that it was found, and break the while loop.
  76. printf("\nusername found, Please Enter password: ");
  77. found += 1;
  78. }
  79. }
  80.  
  81. if (found == 0)
  82. {
  83. printf("\nnot found\n");
  84. main();
  85. }
  86. // Enter the do while loop for the password
  87. do {
  88. c = _getch();
  89. // Check if the character entered is a valid char, and for that character, print a "*" character.
  90. if (isprint(c))
  91. {
  92. buffer[pos++] = c;
  93. printf("%c", '*');
  94. }
  95. else if (c == 8 && pos)
  96. {
  97. buffer[pos--] = '\0';
  98. printf("%s", "\b \b");
  99. }
  100. } while (c != 13);
  101. // If the buffer and password are equal, print a succesfull login, and call other methods
  102. if (!strcmp(buffer, password))
  103. {
  104. printf("\n%s\n", "Logged on succesfully!");
  105.  
  106. //###########################################
  107. menu();
  108. scanf("%d", &choice);
  109. parseChoice(choice);
  110.  
  111.  
  112. }
  113. else
  114. {
  115. printf("\n%s\n", "Incorrect login!, Please try again");
  116. main();
  117. }
  118.  
  119. fclose(fp);
  120. _getch();
  121. }
  122.  
  123. void menu()
  124. {
  125. printf("\n1) Add Player");
  126. printf("\n2) Display Players");
  127. printf("\n3) Display Players Details");
  128. printf("\n4) Update a Player’s statistic");
  129. printf("\n5) Delete a Player");
  130. printf("\n6) Generate Statistics");
  131. printf("\n7) Print Players Details to File");
  132. printf("\n8) Display Players order of Height");
  133. printf("\n9) Exit the program\n");
  134.  
  135. }
  136.  
  137. void parseChoice(int choice)
  138. {
  139. int newIRFU = 0;
  140. struct node* head_ptr;
  141. int option = 0;
  142.  
  143. head_ptr = NULL;
  144.  
  145. if (choice != 0 || choice > 0)
  146. {
  147. switch (choice)
  148. {
  149. case 1:
  150. printf("\nAdding Player...");
  151. printf("\nPlease Enter new IRFU Number: ");
  152. scanf("%d", &newIRFU);
  153.  
  154. if (head_ptr == NULL)
  155. {
  156. addElement_AtStart(&head_ptr, newIRFU);
  157. }
  158. else
  159. {
  160. if (!(search(head_ptr, newIRFU) == 1))
  161. {
  162. addPlayer(head_ptr, newIRFU);
  163. }
  164. else
  165. {
  166. printf("\nThis Player already exists\n");
  167. }
  168. }
  169.  
  170. break;
  171. case 2:
  172. printf("\nDisplaying Players");
  173. displayPlayers(head_ptr);
  174. break;
  175. case 3:
  176. printf("\nDisplaying Players Details");
  177. displayPlayersDetails(head_ptr);
  178.  
  179. break;
  180. case 4:
  181. printf("\nUpdating Players stats");
  182. break;
  183. case 5:
  184. printf("\nDeleting a Player");
  185. break;
  186. case 6:
  187. printf("\nGeneraing stats");
  188. break;
  189. case 7:
  190. printf("\nPrinting Players to File...");
  191. break;
  192. case 8:
  193. printf("\nDisplaying Players in order of Height\n");
  194. break;
  195. case 9:
  196. printf("\nExiting\n");
  197. exit(0);
  198. break;
  199. default:
  200. printf("\nInvalid\n");
  201. menu();
  202. scanf("%d", &choice);
  203.  
  204. parseChoice(choice);
  205. break;
  206. }
  207.  
  208. menu();
  209. scanf("%d", &choice);
  210.  
  211. parseChoice(choice);
  212. }
  213. else
  214. {
  215. printf("\nInvalid\n");
  216. menu();
  217. scanf("%d", &choice);
  218. parseChoice(choice);
  219. }
  220.  
  221.  
  222. }
  223.  
  224. void addPlayer(struct node* headptr, int newIRFU)
  225. {
  226. char email[25];
  227. int found = 0;
  228.  
  229. // add to end
  230. struct node *temp;
  231. temp = (struct node*)malloc(sizeof(struct node));
  232. temp = headptr;
  233.  
  234. while (temp->NEXT != NULL) // go to the last node
  235. {
  236. temp = temp->NEXT;
  237. }
  238.  
  239. struct node *newNode;
  240. newNode = (struct node*)malloc(sizeof(struct node));
  241.  
  242. newNode->IRFU = newIRFU;
  243.  
  244. printf("Please Enter The First Name: ");
  245. scanf("%s", newNode->firstName);
  246. printf("Please Enter The Last Name: ");
  247. scanf("%s", newNode->secondName);
  248. printf("Enter the Age: ");
  249. scanf("%d", &newNode->age);
  250. printf("Please Enter the Height: ");
  251. scanf("%f", &newNode->height);
  252. printf("Please Enter the Weight: ");
  253. scanf("%f", &newNode->weight);
  254. printf("Please Enter the Name of the club: ");
  255. scanf("%s", newNode->club);
  256. printf("Please Enter the Email of the Player: ");
  257. scanf("%s", newNode->email);
  258.  
  259. newNode->NEXT = NULL;
  260. temp->NEXT = newNode;
  261. }
  262.  
  263.  
  264. void addElement_AtStart(struct node** head_ptr, int newIRFU)
  265. {
  266. char email[25];
  267. int found = 0;
  268.  
  269. //Create new node and populate it with data
  270. struct node *newNode;
  271.  
  272. newNode = (struct node*)malloc(sizeof(struct node));
  273. newNode->IRFU = newIRFU;
  274.  
  275. printf("Enter The First Name: ");
  276. scanf("%s", newNode->firstName);
  277. printf("Enter The Last Name: ");
  278. scanf("%s", newNode->secondName);
  279. printf("Enter the Age: ");
  280. scanf("%d", &newNode->age);
  281. printf("Enter the Height: ");
  282. scanf("%f", &newNode->height);
  283. printf("Enter the Weight: ");
  284. scanf("%f", &newNode->weight);
  285. printf("Enter the Name of the club: ");
  286. scanf("%s", newNode->club);
  287. printf("Enter the Email of the Player: ");
  288. scanf("%s", newNode->email);
  289.  
  290. //Connect up the node so that the new node is the headpointer
  291. newNode->NEXT = *head_ptr;
  292. *head_ptr = newNode;
  293. }
  294.  
  295. void displayPlayers(struct node* headptr)
  296. {
  297. printf("\nPrinting all the players\n");
  298. struct node *temp;
  299. temp = (struct node*)malloc(sizeof(struct node));
  300. temp = headptr;
  301.  
  302. if (headptr != NULL) {
  303. printf("All Player Details\n");
  304. printf("Player Name IRFU Age Height Weight Club Email\n");
  305. }
  306.  
  307. while (temp != NULL)
  308. {
  309. printf("%-7s %-7s %-7d %-2d %6.2f %8.2f %13s %18s ", temp->firstName, temp->secondName, temp->IRFU, temp->age, temp->height, temp->weight, temp->club, temp->email);
  310. temp = temp->NEXT;
  311. }
  312. }
  313.  
  314. void displayPlayersDetails(struct node* headptr)
  315. {
  316. printf("\nPrinting all the players details\n");
  317. struct node *temp;
  318. temp = (struct node*)malloc(sizeof(struct node));
  319.  
  320. temp = headptr;
  321.  
  322. while (temp != NULL)
  323. {
  324. printf("The IRFU is: %d\n", temp->IRFU);
  325. printf("The name is: %s %s\n", temp->firstName, temp->secondName);
  326. printf("The age is: %d\n", temp->age);
  327. printf("The height is: %f\n", temp->height);
  328. printf("The weight is: %f\n", temp->weight);
  329. printf("The weight is: %f\n", temp->weight);
  330.  
  331. temp = temp->NEXT;
  332. }
  333. }
  334.  
  335. int search(struct node* headptr, int IRFU)
  336. {
  337. struct node *temp;
  338. int found = 0;
  339.  
  340. //serach all the values in the linked list
  341. temp = headptr;
  342. while (temp != NULL)
  343. {
  344. //if found return 1 if not return 0
  345. if (temp->IRFU == IRFU) {
  346. found = 1;
  347. }
  348.  
  349. temp = temp->NEXT;
  350. }
  351.  
  352. return found;
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement