Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- menu();
- return 0;
- }
- void menu(){
- int choice = -1;
- while (choice != 0){
- printf("what do you want to do:\n");
- printf("[0] exit\n");
- printf("[1] add new worker\n");
- printf("[2] list workers\n");
- printf("[3] delete worker\n");
- printf("[4] exit\n");
- scanf("%d", &choice);
- switch(choice){
- case 1:
- newWorker();
- break;
- case 2:
- listWorkers();
- break;
- case 3:
- printf("delete worker");
- break;
- case 0:
- printf("exit");
- break;
- default:
- printf("invalid input\n");
- }
- }
- }
- void newWorker(){
- int ndays;
- char days[5][10];
- char firstName[100];
- char lastName[100];
- char address[100];
- printf("how many days will the worker work: ");
- scanf("%d", &ndays);
- if(0 < ndays && ndays < 6){
- for(int i = 0; i < ndays; i++){
- printf("Input the %d. day: ", i);
- scanf("%s", &days[i]);
- }
- }else{
- printf("worker can only work 1-5 days a week\n");
- newWorker();
- }
- printf("what is the worker's first name: ");
- scanf("%s", &firstName);
- printf("what is the worker's last name:");
- scanf("%s", &lastName);
- printf("what is the worker's address(no space allowed):");
- scanf("%s", &address);
- char dataLine[50]; //a string to concate the days into
- strcpy(dataLine, days[0]); //copying the first day into the string
- strcat(dataLine, " ");
- for(int i = 1; i < ndays; i++){
- strcat(dataLine, days[i]);
- strcat(dataLine, " ");
- }
- printf("%s\n", dataLine);
- FILE *workerList = fopen("workerlist.txt", "a");
- fprintf(workerList, "%s %s %s %d %s\n", firstName, lastName, address, ndays, dataLine);
- fclose(workerList);
- }
- void listWorkers(){
- FILE * f;
- f=fopen("workerlist.txt","r");
- if (f==NULL){perror("File opening error\n"); exit(1);}
- char line[350];
- while (!feof(f)){
- fgets(line,sizeof(line),f);
- char lastName[100], firstName[100], address[100];
- strcpy(lastName, strtok(line, " "));
- strcpy(firstName, strtok(NULL, " "));
- strcpy(address, strtok(NULL, " "));
- printf("%s %s lives at %s\n they are available on: ", lastName, firstName, address);
- char cnDays[100];
- strcpy(cnDays, strtok(NULL, " "));
- int nDays = atoi(cnDays);
- char days[5][10];
- for (int i = 0; i < nDays; i++){
- strcpy(days[i], strtok(NULL, " "));
- printf("%s ", days[i]);
- }
- }
- printf("\n");
- fclose(f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement