Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to add, modify and remove data about workers.
- //Do you approve it?
- //P.S.: Compile with -lm option if you got a linker error.
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- struct data{
- char first_name[255];
- char second_name[255];
- char position[255];
- char data_read[65535];
- int age;
- int days_since_employement;
- int days_until_next_vacation;
- int salary;
- int years_of_experience;
- };
- void get_string(char[], int);
- int get_number();
- int save_data(struct data, char[]);
- int main(){
- char input[255];
- int i,c;
- int exit = 0;
- struct data worker;
- while(!exit){
- if(c != '1'){
- printf("Workers assignement program.\nThis program is used to add or to remove data about workers.\n\n");
- printf("\t\t-To add a new worker, insert 1\n\t\t-To remove a worker, insert 2\n\t\t-To modify data about a worker, insert 3\n");
- printf("\t\t-To seek help, insert 4\n\t\t-To quit, insert 'q'\n\n");
- printf("\\>");
- c = getchar();
- }
- switch(c){
- case '1':
- while(c == '1'){
- //second getchar to flush stdin.
- getchar();
- printf("First name: ");
- get_string(worker.first_name, 255);
- printf("Second name: ");
- get_string(worker.second_name, 255);
- printf("Position: ");
- get_string(worker.position, 255);
- printf("Age: ");
- worker.age = get_number();
- printf("Days since employement: ");
- worker.days_since_employement = get_number();
- printf("Days until next vacation: ");
- worker.days_until_next_vacation = get_number();
- printf("Salary(in USD): ");
- worker.salary = get_number();
- printf("Years of experience: ");
- worker.years_of_experience = get_number();
- printf("\nWorker's data:\n");
- printf("\tFirst name: %s\n", worker.first_name);
- printf("\tSecond name: %s\n", worker.second_name);
- printf("\tAge: %d\n", worker.age);
- printf("\tDays since employement: %d\n", worker.days_since_employement);
- printf("\tDays until next vacation: %d\n", worker.days_until_next_vacation);
- printf("\tYears of experience: %d\n", worker.years_of_experience);
- printf("Is this correct?(Y/n)");
- while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
- printf("Is this correct?(Y/n)");
- if(c == 'Y' || c == 'y'){
- getchar();
- printf("Please re-insert the first name of the worker: ");
- get_string(input, 255);
- save_data(worker, input);
- printf("Information saved. Press enter to proceed\n\n");
- getchar();
- }
- else if(c == 'N' || c == 'n'){
- printf("Operation reset.\n");
- //this is supposed to make the case start over.
- c = '1';
- //again, flushing stdin. Would appreciate an explanation with a solution.
- getchar();
- }
- }
- break;
- case '2':
- getchar();
- printf("Please insert the worker's name: ");
- get_string(input, 255);
- printf("\nDo you confirm the deletion of the data of this worker?(Y/n)");
- while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
- printf("\nDo you confirm the deletion of the data of this worker?(Y/n)");
- if(c == 'Y' || c == 'y'){
- if(remove(input))
- printf("Failed to remove the file\n");
- else
- printf("File removed successfully.\n\n");
- getchar();
- }
- else if(c == 'N' || c == 'n')
- printf("Operation aborted.\n");
- break;
- case '3':
- FILE *fp;
- getchar();
- printf("Please insert the worker's name: ");
- get_string(input, 255);
- printf("\n\n\n");
- if((fp = fopen(input, "r+")) == NULL){
- printf("Error while reading data");
- return -1;
- }
- else
- while((c = getc(fp)) != EOF)
- printf("%c",c);
- printf("\nModify this file?(Y/n)");
- //Pretty lazy, isn't?
- while((c = getchar()) != 'Y' && c != 'y' && c != 'N' && c != 'n')
- printf("\nModify this file?(Y/n)");
- if(c == 'Y' || c == 'y')
- c = '1';
- else if(c == 'N' || c == 'n'){
- printf("Operation aborted.\n");
- getchar();
- }
- break;
- case '4':
- getchar();
- printf("This program is used to create, modify and delete info about the workers.\n");
- printf("When data is created, it will be saved in a file with a desired name, it is preferred that the name of the file matches the first name of the employee for easier access.\n\n\n");
- break;
- case 'q':
- exit++;
- break;
- default:
- printf("Unknown selection.\n");
- break;
- }
- }
- return 0;
- }
- void get_string(char string[], int max){
- int c, i;
- for(c = i = 0; (c = getchar()) != EOF && c != '\n' && i < max;)
- string[i++] = c;
- string[i] = 0;
- if(i >= max)
- printf("Notice from get_string(): max amount of characters reached.\n");
- return;
- }
- int get_number(){
- int c, i, j, k;
- char inputted_number_in_char[255];
- for(c = i = 0; (c = getchar()) != EOF && c != '\n' && i < 255;)
- if(isdigit(c))
- inputted_number_in_char[i++] = c;
- else{
- printf("Notice from get_number(): input contains a non-digit character\n.");
- return 0;
- }
- inputted_number_in_char[i--] = 0;
- j = k = 0;
- while(i >= 0)
- j += ((inputted_number_in_char[i--] - '0') * pow(10,k++));
- return j;
- }
- int save_data(struct data employee, char file_name[]){
- FILE *fp;
- char part0[] = "\t EMPLOYEE INFORMATION \n\n\0";
- char part1[] = "\t\tFirst name: \0";
- char part2[] = "\t\tSecond name: \0";
- char part3[] = "\t\tPosition: \0";
- char part4[] = "\t\tAge: \0";
- char part5[] = "\t\tDays since employement: \0";
- char part6[] = "\t\tDays until next vacation: \0";
- char part7[] = "\t\tSalary: \0";
- char part8[] = "\t\tYears of experience: \0";
- char input[255];
- if((fp = fopen(file_name, "w")) == NULL){
- printf("Error: can't create file %s", file_name);
- return -1;
- }
- int i = 0;
- while(part0[i] != '\0'){
- putc(part0[i++], fp);
- }
- i = 0;
- while(part1[i] != '\0'){
- putc(part1[i++], fp);
- }
- i = 0;
- while(employee.first_name[i] != '\0'){
- putc(employee.first_name[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part2[i] != '\0'){
- putc(part2[i++], fp);
- }
- i = 0;
- while(employee.second_name[i] != '\0'){
- putc(employee.second_name[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part3[i] != '\0'){
- putc(part3[i++], fp);
- }
- i = 0;
- while(employee.position[i] != '\0'){
- putc(employee.position[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part4[i] != '\0'){
- putc(part4[i++], fp);
- }
- //Thanks, Dave Jarvis and cnicutar from Stack Overflow
- sprintf(input, "%d", employee.age);
- i = 0;
- while(input[i] != '\0'){
- putc(input[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part5[i] != '\0'){
- putc(part5[i++], fp);
- }
- sprintf(input, "%d", employee.days_since_employement);
- i = 0;
- while(input[i] != '\0'){
- putc(input[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part6[i] != '\0'){
- putc(part6[i++], fp);
- }
- sprintf(input, "%d", employee.days_until_next_vacation);
- i = 0;
- while(input[i] != '\0'){
- putc(input[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part7[i] != '\0'){
- putc(part7[i++], fp);
- }
- sprintf(input, "%d", employee.salary);
- i = 0;
- while(input[i] != '\0'){
- putc(input[i++], fp);
- }
- putc('\n', fp);
- i = 0;
- while(part8[i] != '\0'){
- putc(part8[i++], fp);
- }
- sprintf(input, "%d", employee.years_of_experience);
- i = 0;
- while(input[i] != '\0'){
- putc(input[i++], fp);
- }
- putc('\n', fp);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment