This basically what i've done so far.. For the display client list, i need to sort all the user base on their id. I cant figure it out so far how to do it. Kinda hard for me..
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int cid;
char name[40];
char address[80];
double deposit;
}client;
typedef struct
{
char line[300];
}data;
typedef struct
{
int cid;
}finds;
typedef struct
{
double money;
}deposit;
void menu();
int calculateMax(void);
void addClient();
void find();
void display();
void total();
void main()
{
menu();
}
void menu()
{
int selection;
printf("%s", "Program menu :\n");
printf("%s", "1. Add a new client\n");
printf("%s", "2. Find/retrieve information for a particular client\n");
printf("%s", "3. Display client list\n");
printf("%s", "4. Display the total amount of clients' deposit\n");
printf("%s", "5. Find a client that has the lowest and the highest deposit\n");
printf("%s", "6. Quit\n");
printf("%s", "Selection : ");
scanf("%d", &selection);
if(selection == 1)
{
addClient();
}
else if(selection == 2)
{
find();
}
else if(selection == 3)
{
display();
}
else if(selection == 4)
{
total();
}
else if(selection == 5)
{
}
else if(selection == 6)
{
}
else {
printf("%s", "\n\aError, please key in correct input!\n\n");
menu();
}
}
void addClient()
{
int max, i;
FILE *f, *id, *name, *address, *deposit;
printf("%s", "\n\nADD CLIENT\n");
printf("%s", "How many client you would like to add : ");
scanf("%d", &max);
client *client_info;
client_info = (client *) malloc(max * sizeof(client)); /*Allocate Memory For Client's Info*/
if(client_info == NULL)
{
printf("Unable to allocate space for client\n\n");
exit (EXIT_FAILURE);
}
if ((id = fopen("id.txt", "a+")) == NULL) /*Open file id.txt*/
{
printf("Error, the file cannot be opened\n");
}
if ((name = fopen("name.txt", "a+")) == NULL) /*Open file name.txt*/
{
printf("Error, the file cannot be opened\n");
}
if ((address = fopen("address.txt", "a+")) == NULL) /*Open file address.txt*/
{
printf("Error, the file cannot be opened\n");
}
if ((deposit = fopen("deposit.txt", "a+")) == NULL) /*Open file deposit.txt*/
{
printf("Error, the file cannot be opened\n");
}
if ((f = fopen("clients.txt", "a+")) == NULL) /*Open file clients.txt*/
{
printf("Error, the file cannot be opened\n");
}
else {
for (i = 0; i < max ; i++) { /*Get the input data*/
printf("\nClient %d\n", i + 1);
printf("%s", "Client's Id : ");
scanf("%d", &client_info[i].cid);
printf("%s", "Client's Name : ");
fflush(stdin);
gets(client_info[i].name);
printf("%s", "Client's Address : ");
fflush(stdin);
gets(client_info[i].address);
printf("%s", "Client's Deposit : ");
scanf("%lf", &client_info[i].deposit);
printf("\n");
}
}
for(i = 0; i < max; i++) { /*Store input data into files*/
fprintf(id, "%d\n", client_info[i].cid);
fputs(client_info[i].name, name);
fprintf(name, "\n");
fputs(client_info[i].address, address);
fprintf(address, "\n");
fprintf(deposit, "%lf\n", client_info[i].deposit);
}
for(i = 0; i < max; i++) {
fprintf(f, "%d", client_info[i].cid, "");
fprintf(f, "%3s", "");
fputs(client_info[i].name, f);
fprintf(f, "%5s", "");
fputs(client_info[i].address, f);
fprintf(f, "%7s", "");
fprintf(f, "%lf\n", client_info[i].deposit);
}
fclose(f);
fclose(id);
fclose(name);
fclose(address);
fclose(deposit);
free(client_info);
client_info = NULL;
printf("\n");
menu();
}
void find()
{
int find_id = 0, i = 0, client_cid, n = 0, max, selection;
FILE *id, *f;
max = calculateMax();
finds *result;
result = (finds *) malloc(max * sizeof(finds));
if(result == NULL)
{
printf("Unable to allocate space for client\n\n");
exit (EXIT_FAILURE);
}
if ((id = fopen("id.txt", "r")) == NULL)
{
printf("Error, the file cannot be opened\n");
}
else {
while (!feof(id)) { /*Get all clients' id number and store to array*/
fscanf(id, "%d", &result[i].cid);
i++;
}
}
fclose(id);
printf("%s", "\n\nFIND CLIENT\n");
printf("%s", "Key in client's id : "); /*Get the client's id that user want to query*/
scanf("%d", &client_cid);
data *clients_info;
clients_info = (data *) malloc(max * sizeof(data));
if(clients_info == NULL)
{
printf("Unable to allocate space for client\n\n");
exit (EXIT_FAILURE);
}
if ((f = fopen("clients.txt", "r")) == NULL)
{
printf("Error, the file cannot be opened\n");
}
i = 0;
while (!feof(f)) { /*Get all the clients' data from clients.txt and stored to array*/
fflush(stdin);
fgets(clients_info[i].line, 300, f);
i++;
}
fclose (f);
for(i = 0; i < max + 1; i++)
{
if(client_cid == result[i].cid)
{
printf("\n\n%s%6s%20s%17s\n", "ID", "NAME", "ADDRESS", "DEPOSIT");
puts(clients_info[i].line);
free(clients_info);
clients_info = NULL;
free(result);
result = NULL;
printf("Would you like to find another client?\nKey in 1 for yes or 2 to go to menu : ");
scanf("%d",&selection);
if(selection == 1) {
find();
}
else if(selection == 2) {
menu();
}
break;
}
else if(i == max)
{
printf("\nSory, there's no client with that id in our database.\n");
free(clients_info);
clients_info = NULL;
free(result);
result = NULL;
printf("Would you like to find another client?\nKey in 1 for yes or 2 to go to menu : ");
scanf("%d",&selection);
if(selection == 1) {
find();
}
else if(selection == 2) {
printf("\n\n");
menu();
}
}
}
}
void display()
{
int max = 0, i;
FILE *f;
printf("\n\nDISPLAY CLIENTS");
max = calculateMax();
data *clients_info;
clients_info = (data *) malloc(max * sizeof(data));
if(clients_info == NULL)
{
printf("Unable to allocate space for client\n\n");
exit (EXIT_FAILURE);
}
if ((f = fopen("clients.txt", "r")) == NULL)
{
printf("Error, the file cannot be opened\n");
}
i = 0;
while (!feof(f)) {
fflush(stdin);
fgets(clients_info[i].line, 200, f);
i++;
}
fclose (f);
printf("\n\n%s%6s%20s%17s\n", "ID", "NAME", "ADDRESS", "DEPOSIT");
for(i = 0; i < max; i++)
{
puts(clients_info[i].line);
}
free(clients_info);
clients_info = NULL;
}
void total()
{
int find_id = 0, max = 0, i = 0;
double total = 0;
FILE *deposits;
max = calculateMax();
deposit *result;
result = (deposit *) malloc(max * sizeof(deposit));
if(result == NULL)
{
printf("Unable to allocate space for client\n\n");
exit (EXIT_FAILURE);
}
if ((deposits = fopen("deposit.txt", "r")) == NULL)
{
printf("Error, the file cannot be opened\n");
}
else {
while (!feof(deposits)) { /*Get all clients' id number and store to array*/
fscanf(deposits, "%lf", &result[i].money);
i++;
}
}
fclose(deposits);
for(i = 0; i < max; i++)
{
total += result[i].money;
}
printf("\n\nTOTAL OF CLIENTS' DEPOSIT\n\n");
printf("Total Deposit is RM%.2lf\n\n\n", total);
menu();
}
int calculateMax(void)
{
char str[150];
int maximum = 0;
FILE *f;
if ((f = fopen("clients.txt", "r")) == NULL)
{
printf("Error, the file cannot be opened\n");
}
else {
while (!feof(f)) {
maximum++;
fgets (str , 200 , f);
}
}
fclose (f);
maximum = maximum - 1;
return maximum;
}