Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 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.  
  37. void displayDetails(struct node* headptr);
  38.  
  39.  
  40. void displayPlayers(struct node* headptr);
  41. void displayPlayersDetails(struct node* headptr);
  42. int search(struct node* headptr, int IRFU);
  43.  
  44. void main()
  45. {
  46. // Initialize all local variables.
  47. char buffer[256] = { 0 };
  48. char username[6];
  49. char password[6];
  50. char c;
  51. char line[1000];
  52. int pos = 0;
  53. int found = 0;
  54. int lineNumber = 1;
  55. int choice;
  56. // Declare and open the files used.
  57. FILE * fp;
  58. fp = fopen("users.txt", "r");
  59.  
  60. if (fp == NULL)
  61. {
  62. //printf("Can not open the file to read");
  63. fp = fopen("users.txt", "wb");
  64. printf("\nPlease Enter a new username: ");
  65. scanf("%s", username);
  66.  
  67. fprintf(fp,"%s", username);
  68. printf("\nPlease Enter a new password: ");
  69. scanf("%s", password);
  70.  
  71. fprintf(fp, "%s", password);
  72. strcpy(buffer, password);
  73.  
  74.  
  75. found += 1;
  76.  
  77. }
  78. else
  79. {
  80. // Firstly, ask the user for their username, if it's valid, it will ask for their password.
  81. printf("Enter username: ");
  82. scanf("%s", username);
  83.  
  84. // Enter this while loop for every line the file has,
  85. // The file contains a list of usernames followed by their passwords directly after.
  86. while (fgets(line, sizeof(line), fp))
  87. {
  88. lineNumber++;
  89. // Each line ends with an end of line \n, so using strtok removes it, allowing strcmp to compare the strings
  90. strtok(line, "\n");
  91. // Check if the username entered is equal to the current lines string,
  92. // And make sure the line number is divisible by 2 as the usernames in the file are always followed by the password.
  93. if (strcmp(username, line) == 0 && lineNumber % 2 == 0)
  94. {
  95. // If a username is found, call fgets to move to the next line, call strtok again, and assign the password to the current line.
  96. fgets(line, sizeof(line), fp);
  97. strtok(line, "\n");
  98. strcpy(password, line);
  99. // Then print that it was found, and break the while loop.
  100. printf("\nusername found, Please Enter password: ");
  101. found += 1;
  102. }
  103. }
  104. }
  105.  
  106.  
  107. if (found == 0)
  108. {
  109. printf("\nnot found\n");
  110. main();
  111. }
  112. // Enter the do while loop for the password
  113. do {
  114. c = _getch();
  115. // Check if the character entered is a valid char, and for that character, print a "*" character.
  116. if (isprint(c))
  117. {
  118. buffer[pos++] = c;
  119. printf("%c", '*');
  120. }
  121. else if (c == 8 && pos)
  122. {
  123. buffer[pos--] = '\0';
  124. printf("%s", "\b \b");
  125. }
  126. } while (c != 13);
  127. // If the buffer and password are equal, print a succesfull login, and call other methods
  128. if (!strcmp(buffer, password))
  129. {
  130. printf("\n%s\n", "Logged on succesfully!");
  131.  
  132. //###########################################
  133. menu();
  134. scanf("%d", &choice);
  135. parseChoice(choice);
  136.  
  137.  
  138. }
  139. else
  140. {
  141. printf("\n%s\n", "Incorrect login!, Please try again");
  142. main();
  143. }
  144.  
  145. fclose(fp);
  146. _getch();
  147. }
  148.  
  149. void menu()
  150. {
  151. printf("\n1) Add Player");
  152. printf("\n2) Display Players");
  153. printf("\n3) Display Players Details");
  154. printf("\n4) Update a Player’s statistic");
  155. printf("\n5) Delete a Player");
  156. printf("\n6) Generate Statistics");
  157. printf("\n7) Print Players Details to File");
  158. printf("\n8) Display Players order of Height");
  159. printf("\n9) Exit the program\n");
  160.  
  161. }
  162.  
  163. void parseChoice(int choice)
  164. {
  165. int newIRFU = 0;
  166. struct node* head_ptr;
  167. int option = 0;
  168.  
  169. head_ptr = NULL;
  170.  
  171. if (choice != 0 || choice > 0)
  172. {
  173. switch (choice)
  174. {
  175. case 1:
  176. printf("\nAdding Player...");
  177. printf("\nPlease Enter new IRFU Number: ");
  178. scanf("%d", &newIRFU);
  179.  
  180. if (head_ptr == NULL)
  181. {
  182. addElement_AtStart(&head_ptr, newIRFU);
  183. }
  184. else
  185. {
  186. if (!(search(head_ptr, newIRFU) == 1))
  187. {
  188. addPlayer(head_ptr, newIRFU);
  189. }
  190. else
  191. {
  192. printf("\nThis Player already exists\n");
  193. }
  194. }
  195.  
  196. break;
  197. case 2:
  198. printf("\nDisplaying Players");
  199. displayPlayers(head_ptr);
  200. break;
  201. case 3:
  202. printf("\nDisplaying Players Details");
  203. //displayPlayersDetails(head_ptr);
  204. displayDetails(head_ptr);
  205.  
  206. break;
  207. case 4:
  208. printf("\nUpdating Players stats");
  209. break;
  210. case 5:
  211. printf("\nDeleting a Player");
  212. break;
  213. case 6:
  214. printf("\nGeneraing stats");
  215. break;
  216. case 7:
  217. printf("\nPrinting Players to File...");
  218. break;
  219. case 8:
  220. printf("\nDisplaying Players in order of Height\n");
  221. break;
  222. case 9:
  223. printf("\nExiting\n");
  224. exit(0);
  225. break;
  226. default:
  227. printf("\nInvalid\n");
  228. menu();
  229. scanf("%d", &choice);
  230.  
  231. parseChoice(choice);
  232. break;
  233. }
  234.  
  235. menu();
  236. scanf("%d", &choice);
  237.  
  238. parseChoice(choice);
  239. }
  240. else
  241. {
  242. printf("\nInvalid\n");
  243. menu();
  244. scanf("%d", &choice);
  245. parseChoice(choice);
  246. }
  247.  
  248.  
  249. }
  250.  
  251. void addPlayer(struct node* headptr, int newIRFU)
  252. {
  253. char email[25];
  254. int found = 0;
  255.  
  256. // add to end
  257. struct node *temp;
  258. temp = (struct node*)malloc(sizeof(struct node));
  259. temp = headptr;
  260.  
  261. while (temp->NEXT != NULL) // go to the last node
  262. {
  263. temp = temp->NEXT;
  264. }
  265.  
  266. struct node *newNode;
  267. newNode = (struct node*)malloc(sizeof(struct node));
  268.  
  269. newNode->IRFU = newIRFU;
  270.  
  271. printf("Please Enter The First Name: ");
  272. scanf("%s", newNode->firstName);
  273. printf("Please Enter The Last Name: ");
  274. scanf("%s", newNode->secondName);
  275. printf("Enter the Age: ");
  276. scanf("%d", &newNode->age);
  277. printf("Please Enter the Height: ");
  278. scanf("%f", &newNode->height);
  279. printf("Please Enter the Weight: ");
  280. scanf("%f", &newNode->weight);
  281. printf("Please Enter the Name of the club: ");
  282. scanf("%s", newNode->club);
  283. printf("Please Enter the Email of the Player: ");
  284. scanf("%s", newNode->email);
  285.  
  286. newNode->NEXT = NULL;
  287. temp->NEXT = newNode;
  288. }
  289.  
  290.  
  291. void addElement_AtStart(struct node** head_ptr, int newIRFU)
  292. {
  293. char email[25];
  294. int found = 0;
  295.  
  296. //Create new node and populate it with data
  297. struct node *newNode;
  298.  
  299. newNode = (struct node*)malloc(sizeof(struct node));
  300. newNode->IRFU = newIRFU;
  301.  
  302. printf("Enter The First Name: ");
  303. scanf("%s", newNode->firstName);
  304. printf("Enter The Last Name: ");
  305. scanf("%s", newNode->secondName);
  306. printf("Enter the Age: ");
  307. scanf("%d", &newNode->age);
  308. printf("Enter the Height: ");
  309. scanf("%f", &newNode->height);
  310. printf("Enter the Weight: ");
  311. scanf("%f", &newNode->weight);
  312. printf("Enter the Name of the club: ");
  313. scanf("%s", newNode->club);
  314. printf("Enter the Email of the Player: ");
  315. scanf("%s", newNode->email);
  316.  
  317. //Connect up the node so that the new node is the headpointer
  318. newNode->NEXT = *head_ptr;
  319. *head_ptr = newNode;
  320. }
  321.  
  322. void displayPlayers(struct node* headptr)
  323. {
  324. printf("\nPrinting all the players\n");
  325. struct node *temp;
  326. temp = (struct node*)malloc(sizeof(struct node));
  327. temp = headptr;
  328.  
  329. if (headptr != NULL) {
  330. printf("All Player Details\n");
  331. printf("Player Name IRFU Age Height Weight Club Email\n");
  332. }
  333.  
  334. while (temp != NULL)
  335. {
  336. 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);
  337. temp = temp->NEXT;
  338. }
  339. }
  340.  
  341. void displayPlayersDetails(struct node* headptr)
  342. {
  343. printf("\nPrinting all the players details\n");
  344. struct node *temp;
  345. temp = (struct node*)malloc(sizeof(struct node));
  346.  
  347. temp = headptr;
  348.  
  349. while (temp != NULL)
  350. {
  351. printf("The IRFU is: %d\n", temp->IRFU);
  352. printf("The name is: %s %s\n", temp->firstName, temp->secondName);
  353. printf("The age is: %d\n", temp->age);
  354. printf("The height is: %f\n", temp->height);
  355. printf("The weight is: %f\n", temp->weight);
  356. printf("The weight is: %f\n", temp->weight);
  357.  
  358. temp = temp->NEXT;
  359. }
  360. }
  361.  
  362. int search(struct node* headptr, int IRFU)
  363. {
  364. struct node *temp;
  365. int found = 0;
  366.  
  367. //serach all the values in the linked list
  368. temp = headptr;
  369. while (temp != NULL)
  370. {
  371. //if found return 1 if not return 0
  372. if (temp->IRFU == IRFU) {
  373. found = 1;
  374. }
  375.  
  376. temp = temp->NEXT;
  377. }
  378.  
  379. return found;
  380.  
  381. }
  382.  
  383. void displayDetails(struct node* headptr)
  384. {
  385. char pos[25];
  386. char *ptr = NULL;
  387. char *ptr3 = NULL;
  388. char *ptr2 = NULL;
  389. struct node *temp;
  390. temp = (struct node*)malloc(sizeof(struct node));
  391.  
  392. temp = headptr;
  393. if (headptr != NULL) {
  394. printf("All Player Details\n");
  395. printf("---===---===---===\n");
  396. printf("Player Name IRFU Age Height Weight Club Email Position Missed M-Travelled\n");
  397. }
  398. else {
  399. printf("No details Available");
  400. }
  401.  
  402. while (temp != NULL) {
  403.  
  404.  
  405. if (temp->pos == 1)
  406. ptr = "Prop";
  407. else if (temp->pos == 2)
  408. ptr = "Hooker";
  409. else if (temp->pos == 3)
  410. ptr = "Second Row";
  411. else if (temp->pos == 4)
  412. ptr = ("Back Row");
  413. else if (temp->pos == 5)
  414. ptr = "Half Back";
  415. else if (temp->pos == 6)
  416. ptr = "Centre";
  417. else if (temp->pos == 7)
  418. ptr = "Wingers";
  419. else if (temp->pos == 8)
  420. ptr = "FullBack";
  421.  
  422. if (temp->tacklesMissed == 1)
  423. ptr2 = "Never";
  424. else if (temp->tacklesMissed == 2)
  425. ptr2 = "<3";
  426. else if (temp->tacklesMissed == 3)
  427. ptr2 = "<5";
  428. else if (temp->tacklesMissed == 4)
  429. ptr2 = ">5";
  430.  
  431. if (temp->meters == 1)
  432. ptr3 = "None";
  433. else if (temp->meters == 2)
  434. ptr3 = "< 10M";
  435. else if (temp->meters == 3)
  436. ptr3 = "< 20M";
  437. else if (temp->meters == 4)
  438. ptr3 = "> 20M";
  439.  
  440.  
  441. printf("%-7s %-7s %-7d %-2d %6.2f %8.2f %13s %-18s %-12s %-5s %10s", temp->firstName, temp->secondName, temp->IRFU, temp->age, temp->height, temp->weight, temp->club, temp->email, ptr, ptr2, ptr3);
  442. printf("\n");
  443.  
  444. temp = temp->NEXT;
  445. }
  446.  
  447.  
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement