Hello,
I have an assignment to make which i have finished most of except that i have a bug (or so i think)
The program works like a database where you can add, edit, delete persons and display their info that you add.
3 different files: main.c, final.c, header.h
the final.c file contains all the functions, i used the first one to add a person and save the details to a binary file. and function 5 to display the details i entered in a list, the only problem is that the year of birth and salary outputs are always huge (for example 423547) even if i input a small number such as 100.
Could anyone help me find the problem? thank you
FINAL.C
MAIN.CCode:#include <stdio.h> #include <stdlib.h> #include <string.h> #include "header.h" /* ---------------------------------- 1. ADD + SAVE FUCNTION -------------------------- */ int add(PERSON *p) { i = count; count++; system("cls"); printf("New Entry\n"); printf("---------\n"); printf("\nFirst Name: "); scanf("%s", &p[i].fname); printf("\nLast Name: "); scanf("%s", &p[i].lname); printf("\nYear of birth: "); scanf("%d", &p[i].year); printf("\nNationality: "); scanf("%s", &p[i].nation); printf("\nProfession: "); scanf("%s", &p[i].prof); printf("\nSalary: "); scanf("%d", &p[i].salary); /* below the given data gets saved into data.bin */ FILE *f; f = fopen("data.bin", "wb"); if(f != NULL) { fwrite(p, sizeof(PERSON), 100, f); } else printf("Can't write the file..."); fclose(f); printf("\nData has been saved!\n"); system("PAUSE"); getchar(); return 0; } /* ---------------------------------- 2. EDIT FUNCTION -------------------------- */ int edit(PERSON *p){ system("cls"); printf("Enter the number of the person whos data you want to edit: "); i = 0; while (i < count){ printf("\n%1d %4s %8s\n", i+1, p[i].fname, p[i].lname); i++; } scanf("%d", &ch); ch--; system("cls"); printf("Enter new person data\n\n"); printf("\nFirst Name: "); scanf("%s", &p[ch].fname); printf("\nLast Name: "); scanf("%s", &p[ch].lname); printf("\nYear of birth: "); scanf("%d", &p[ch].year); printf("\nNationality: "); scanf("%s", &p[ch].nation); printf("\nProfession: "); scanf("%s", &p[ch].prof); printf("\nSalary: "); scanf("%d", &p[ch].salary); printf("\nData was changed. Press enter to continue..."); getchar(); getchar(); return 0; } /* ---------------------------------- 3. DELETE FUNCTION -------------------------- */ int del(PERSON *p){ system("cls"); printf("Enter the number of the person you'd like to delete: \n"); i = 0; while (i < count){ printf("%1d %4s %8s\n", i+1, p[i].fname, p[i].lname); i++; } scanf("%d", &ch); ch--; while (ch < count){ p[ch] = p[ch+1]; ch++; } count--; printf("\n\nData was deleted. press enter to continue..."); getchar(); getchar(); return 0; } /* ---------------------------------- 4. STATISTICS FUNCTION -------------------------- */ int stat(PERSON *p){ i = 0; while (i < count-1){ if (p[i].year > p[i+1].year){ p[99] = p[i]; p[i] = p[i+1]; p[i+1] = p[99]; i = 0; } if (p[i].year <= p[i+1].year) i++; } system("cls"); printf("-------------------------Statistics---------------------------------\n"); printf("\nOldest person: %s %s \n", p[count-1].fname, p[count-1].lname); printf("\nYoungets person: %s %s \n", p[0].fname, p[0].lname); /* loop to find average year of birth */ i = 0; ch = 0; while (i < count){ ch = ch + p[i].year; i++; } printf("\nAverage year of birth: %d\n", ch / count-1); /* loop to find average salary */ i = 0; ch = 0; while (i < count){ ch = ch + p[i].salary; i++; } printf("\nThe average income is: %d\n", ch / count-1); printf("\nPress enter to continue..."); getchar(); getchar(); return 0; } /* ---------------------------------- 5. DISPLAY FUNCTION -------------------------- */ int disp(PERSON *p){ system("cls"); printf("# First name | Last name | Year of Birth | Nationality | Profession | Salary\n"); printf("----------------------------------------------------------------------------\n\n"); i = 0; while (i < count){ printf("%1d %s %s %d %s %s %d\n", i+1, p[i].fname, p[i].lname, p[i].year, p[i].nation, p[i].prof, p[i].salary); i++; } system("PAUSE"); getchar(); return 0; } /* ---------------------------------- 6. DOCUMENTATION -------------------------- */ int doc(PERSON *p){ system("cls"); printf("-------------- Documentation ------------------\n\n"); printf("User Guide: In the main menu, select a service by\n"); printf("typing its number then pressing enter.\n\n"); printf("Types of data structures used are 'int' and 'char'\n"); printf("integer part used for year of birth and salary data\n"); printf("while char is used for the rest.\n\n"); printf("5 Different functions are availible for usage:\n"); printf("To add, edit, delete, persons data and to display\n"); printf("statistics (such as average salary) or personal data\n"); printf("of each person in a list.\n\n"); printf("All declarations are inside the header.h file and all\n"); printf("the function bodies are inside final.c including this\n"); printf("documentation!\n\n"); system("PAUSE"); getchar(); return 0; }
HEADER.H#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[]) {
do {
system("cls");
printf("-----------Main Menu-----------\n");
printf("1. Add new person\n");
printf("2. Edit persons data\n");
printf("3. Delete person\n");
printf("4. Show statistics\n");
printf("5. Display personal data\n");
printf("6. Documentation\n");
scanf("%d", &choice);
switch (choice) {
case 1: add(p);
break;
case 2: edit(p);
break;
case 3: del(p);
break;
case 4: stat(p);
break;
case 5: disp(p);
break;
case 6: doc(p);
break;
default: printf("Invalid Choice!\n");
break;
}
} while(choice != 7);
system("PAUSE");
return 0;
}
Code:typedef struct { /* data in struct, incl. variables that will be used /w pointer */ char fname[20]; char lname[20]; char nation[20]; char prof[20]; int year[20]; int salary[20]; } PERSON; /* struct name */ PERSON p[100]; /* pointer declared to work with struct PERSON (p) */ /* declaring functions */ int add(PERSON*); int edit(PERSON*); int del(PERSON*); int stat(PERSON*); int disp(PERSON*); int doc(PERSON*); /* other declarations */ int choice; int count; int i; int ch; int count;



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.