Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sean McGuire, G00330886.
- Rugby Assignment.
- */
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- // define struct
- struct node
- {
- int IRFU;
- char firstName[20];
- char secondName[20];
- int age;
- float height;
- float weight;
- char club[20];
- char email[30];
- char pos[10];
- int tacklesMissed;
- int meters;
- struct node* NEXT;
- };
- void menu();
- void parseChoice(int choice);
- void addPlayer(struct node* headptr, int newIRFU);
- void addElement_AtStart(struct node** head_ptr, int newIRFU);
- void displayDetails(struct node* headptr);
- void displayPlayers(struct node* headptr);
- void displayPlayersDetails(struct node* headptr);
- int search(struct node* headptr, int IRFU);
- void main()
- {
- // Initialize all local variables.
- char buffer[256] = { 0 };
- char username[6];
- char password[6];
- char c;
- char line[1000];
- int pos = 0;
- int found = 0;
- int lineNumber = 1;
- int choice;
- // Declare and open the files used.
- FILE * fp;
- fp = fopen("users.txt", "r");
- if (fp == NULL)
- {
- //printf("Can not open the file to read");
- fp = fopen("users.txt", "wb");
- printf("\nPlease Enter a new username: ");
- scanf("%s", username);
- fprintf(fp,"%s", username);
- printf("\nPlease Enter a new password: ");
- scanf("%s", password);
- fprintf(fp, "%s", password);
- strcpy(buffer, password);
- found += 1;
- }
- else
- {
- // Firstly, ask the user for their username, if it's valid, it will ask for their password.
- printf("Enter username: ");
- scanf("%s", username);
- // Enter this while loop for every line the file has,
- // The file contains a list of usernames followed by their passwords directly after.
- while (fgets(line, sizeof(line), fp))
- {
- lineNumber++;
- // Each line ends with an end of line \n, so using strtok removes it, allowing strcmp to compare the strings
- strtok(line, "\n");
- // Check if the username entered is equal to the current lines string,
- // And make sure the line number is divisible by 2 as the usernames in the file are always followed by the password.
- if (strcmp(username, line) == 0 && lineNumber % 2 == 0)
- {
- // If a username is found, call fgets to move to the next line, call strtok again, and assign the password to the current line.
- fgets(line, sizeof(line), fp);
- strtok(line, "\n");
- strcpy(password, line);
- // Then print that it was found, and break the while loop.
- printf("\nusername found, Please Enter password: ");
- found += 1;
- }
- }
- }
- if (found == 0)
- {
- printf("\nnot found\n");
- main();
- }
- // Enter the do while loop for the password
- do {
- c = _getch();
- // Check if the character entered is a valid char, and for that character, print a "*" character.
- if (isprint(c))
- {
- buffer[pos++] = c;
- printf("%c", '*');
- }
- else if (c == 8 && pos)
- {
- buffer[pos--] = '\0';
- printf("%s", "\b \b");
- }
- } while (c != 13);
- // If the buffer and password are equal, print a succesfull login, and call other methods
- if (!strcmp(buffer, password))
- {
- printf("\n%s\n", "Logged on succesfully!");
- //###########################################
- menu();
- scanf("%d", &choice);
- parseChoice(choice);
- }
- else
- {
- printf("\n%s\n", "Incorrect login!, Please try again");
- main();
- }
- fclose(fp);
- _getch();
- }
- void menu()
- {
- printf("\n1) Add Player");
- printf("\n2) Display Players");
- printf("\n3) Display Players Details");
- printf("\n4) Update a Player’s statistic");
- printf("\n5) Delete a Player");
- printf("\n6) Generate Statistics");
- printf("\n7) Print Players Details to File");
- printf("\n8) Display Players order of Height");
- printf("\n9) Exit the program\n");
- }
- void parseChoice(int choice)
- {
- int newIRFU = 0;
- struct node* head_ptr;
- int option = 0;
- head_ptr = NULL;
- if (choice != 0 || choice > 0)
- {
- switch (choice)
- {
- case 1:
- printf("\nAdding Player...");
- printf("\nPlease Enter new IRFU Number: ");
- scanf("%d", &newIRFU);
- if (head_ptr == NULL)
- {
- addElement_AtStart(&head_ptr, newIRFU);
- }
- else
- {
- if (!(search(head_ptr, newIRFU) == 1))
- {
- addPlayer(head_ptr, newIRFU);
- }
- else
- {
- printf("\nThis Player already exists\n");
- }
- }
- break;
- case 2:
- printf("\nDisplaying Players");
- displayPlayers(head_ptr);
- break;
- case 3:
- printf("\nDisplaying Players Details");
- //displayPlayersDetails(head_ptr);
- displayDetails(head_ptr);
- break;
- case 4:
- printf("\nUpdating Players stats");
- break;
- case 5:
- printf("\nDeleting a Player");
- break;
- case 6:
- printf("\nGeneraing stats");
- break;
- case 7:
- printf("\nPrinting Players to File...");
- break;
- case 8:
- printf("\nDisplaying Players in order of Height\n");
- break;
- case 9:
- printf("\nExiting\n");
- exit(0);
- break;
- default:
- printf("\nInvalid\n");
- menu();
- scanf("%d", &choice);
- parseChoice(choice);
- break;
- }
- menu();
- scanf("%d", &choice);
- parseChoice(choice);
- }
- else
- {
- printf("\nInvalid\n");
- menu();
- scanf("%d", &choice);
- parseChoice(choice);
- }
- }
- void addPlayer(struct node* headptr, int newIRFU)
- {
- char email[25];
- int found = 0;
- // add to end
- struct node *temp;
- temp = (struct node*)malloc(sizeof(struct node));
- temp = headptr;
- while (temp->NEXT != NULL) // go to the last node
- {
- temp = temp->NEXT;
- }
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->IRFU = newIRFU;
- printf("Please Enter The First Name: ");
- scanf("%s", newNode->firstName);
- printf("Please Enter The Last Name: ");
- scanf("%s", newNode->secondName);
- printf("Enter the Age: ");
- scanf("%d", &newNode->age);
- printf("Please Enter the Height: ");
- scanf("%f", &newNode->height);
- printf("Please Enter the Weight: ");
- scanf("%f", &newNode->weight);
- printf("Please Enter the Name of the club: ");
- scanf("%s", newNode->club);
- printf("Please Enter the Email of the Player: ");
- scanf("%s", newNode->email);
- newNode->NEXT = NULL;
- temp->NEXT = newNode;
- }
- void addElement_AtStart(struct node** head_ptr, int newIRFU)
- {
- char email[25];
- int found = 0;
- //Create new node and populate it with data
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->IRFU = newIRFU;
- printf("Enter The First Name: ");
- scanf("%s", newNode->firstName);
- printf("Enter The Last Name: ");
- scanf("%s", newNode->secondName);
- printf("Enter the Age: ");
- scanf("%d", &newNode->age);
- printf("Enter the Height: ");
- scanf("%f", &newNode->height);
- printf("Enter the Weight: ");
- scanf("%f", &newNode->weight);
- printf("Enter the Name of the club: ");
- scanf("%s", newNode->club);
- printf("Enter the Email of the Player: ");
- scanf("%s", newNode->email);
- //Connect up the node so that the new node is the headpointer
- newNode->NEXT = *head_ptr;
- *head_ptr = newNode;
- }
- void displayPlayers(struct node* headptr)
- {
- printf("\nPrinting all the players\n");
- struct node *temp;
- temp = (struct node*)malloc(sizeof(struct node));
- temp = headptr;
- if (headptr != NULL) {
- printf("All Player Details\n");
- printf("Player Name IRFU Age Height Weight Club Email\n");
- }
- while (temp != NULL)
- {
- 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);
- temp = temp->NEXT;
- }
- }
- void displayPlayersDetails(struct node* headptr)
- {
- printf("\nPrinting all the players details\n");
- struct node *temp;
- temp = (struct node*)malloc(sizeof(struct node));
- temp = headptr;
- while (temp != NULL)
- {
- printf("The IRFU is: %d\n", temp->IRFU);
- printf("The name is: %s %s\n", temp->firstName, temp->secondName);
- printf("The age is: %d\n", temp->age);
- printf("The height is: %f\n", temp->height);
- printf("The weight is: %f\n", temp->weight);
- printf("The weight is: %f\n", temp->weight);
- temp = temp->NEXT;
- }
- }
- int search(struct node* headptr, int IRFU)
- {
- struct node *temp;
- int found = 0;
- //serach all the values in the linked list
- temp = headptr;
- while (temp != NULL)
- {
- //if found return 1 if not return 0
- if (temp->IRFU == IRFU) {
- found = 1;
- }
- temp = temp->NEXT;
- }
- return found;
- }
- void displayDetails(struct node* headptr)
- {
- char pos[25];
- char *ptr = NULL;
- char *ptr3 = NULL;
- char *ptr2 = NULL;
- struct node *temp;
- temp = (struct node*)malloc(sizeof(struct node));
- temp = headptr;
- if (headptr != NULL) {
- printf("All Player Details\n");
- printf("---===---===---===\n");
- printf("Player Name IRFU Age Height Weight Club Email Position Missed M-Travelled\n");
- }
- else {
- printf("No details Available");
- }
- while (temp != NULL) {
- if (temp->pos == 1)
- ptr = "Prop";
- else if (temp->pos == 2)
- ptr = "Hooker";
- else if (temp->pos == 3)
- ptr = "Second Row";
- else if (temp->pos == 4)
- ptr = ("Back Row");
- else if (temp->pos == 5)
- ptr = "Half Back";
- else if (temp->pos == 6)
- ptr = "Centre";
- else if (temp->pos == 7)
- ptr = "Wingers";
- else if (temp->pos == 8)
- ptr = "FullBack";
- if (temp->tacklesMissed == 1)
- ptr2 = "Never";
- else if (temp->tacklesMissed == 2)
- ptr2 = "<3";
- else if (temp->tacklesMissed == 3)
- ptr2 = "<5";
- else if (temp->tacklesMissed == 4)
- ptr2 = ">5";
- if (temp->meters == 1)
- ptr3 = "None";
- else if (temp->meters == 2)
- ptr3 = "< 10M";
- else if (temp->meters == 3)
- ptr3 = "< 20M";
- else if (temp->meters == 4)
- ptr3 = "> 20M";
- 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);
- printf("\n");
- temp = temp->NEXT;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement