So basically I'm making a bank application for a linked list and I'm having an issue as to how I'm supposed to create the withdraw and deposit function as you can see in my code I wish to be able to utilize this as part of my linked list.
The deposit function is in line 888 and im using else if option 4 in line 204 to call this function to work
If possible could somebody help me in my code as to get this function working please as i've had success with the other functions so far as of up till now
What I have tried:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <list>
typedef struct NodeT_
{
char name[30], lastName[20], accountNum[10], email[30];
char addy[10];
char id[30], service[20];
double balance;
float average;
struct NodeT_* NEXT;
}node;
void addElementAttheEnd(node* top, char studentID[]);
void addElementAttheStart(node** top, char studentID[]);
void display(node* top);
void displayUser(node* userArray, int size);
void displayToFile(node* top);
int search(node* top, char searchID[]);
int location(node* top, char searchID[]);
int length(node* top);
void deleteAtEnd(node* top);
void deleteAtStart(node** top);
void deleteintheMiddle(node* top, char searchID[]);
void deletedAtLocation(node* top, int loc);
void updateNodeValues(node* top, char ID[]);
void deposit(struct customer[], int, int, int);
int main()
{
struct customer ;
int n, choice, account_no, amount, index;
node* HEAD = NULL;
node* temp;
int option;
int sizeDatabase = 30;
int searchNum;
int result;
int foundLocation;
char searchTerm[20];
char username[20], password[20];
FILE* inp;
node fileValue;
printf("User name: ");
gets_s(username);
printf("Password: ");
gets_s(password);
if (strcmp(username, "Akeem") == 0)
{
if (strcmp(password, "Login100") == 0)
{
printf("Login succesful\n");
}
else
{
printf("Invalid password\n");
return 0;
}
}
else
{
printf("Inavild name\n");
return 0;
}
inp = fopen("Report.txt", "r");
if (inp == NULL)
printf("Sorry the file could not be opened\n");
else
{
while (!feof(inp))
{
result = fscanf(inp, "%s %s %s %s %s %s %s\n", fileValue.accountNum, fileValue.name, fileValue.lastName, fileValue.addy, fileValue.email, fileValue.service, fileValue.balance);
if (result == 7)
{
printf("%s %s %s %s %s %s %s\n", fileValue.accountNum, fileValue.name, fileValue.lastName, fileValue.addy, fileValue.email, fileValue.service, fileValue.balance);
}
}
fclose(inp);
}
printf("1) Add customer(Note: Account Number must be unique).\n");
printf("2) Display all account whose balance is less than €100 to screen \n");
printf("3) Display account holder details \n");
printf("4) Lodge to Account \n");
printf("5) Withdraw from Account \n");
printf("6) Delete Account \n");
printf("7) Generate statistics \n");
printf("8) Print all account holders into a report file \n");
scanf("%d", &option);
while (option != -1)
{
if (option == 1)
{
printf("Please enter the PPSN\n");
scanf("%s", searchTerm);
if (HEAD == NULL)
addElementAttheStart(&HEAD, searchTerm);
else
{
result = location(HEAD, searchTerm);
if (result == -1)
addElementAttheEnd(HEAD, searchTerm);
else
printf("Sorry this ppsn already exists\n");
}
}
else if (option == 2)
{
}
else if (option == 3)
{
display(HEAD);
}
else if (option == 4)
{
printf("Enter account number : ");
scanf("%d", &account_no);
printf("Enter amount to deposit : ");
scanf("%d", &amount);
deposit(data, n, account_no, amount);
}
else if (option == 5)
{
}
else if (option == 6)
{
if (HEAD == NULL)
{
printf("The list is empty so this option can not be completed\n");
}
else
{
printf("Please enter the Account you wish to delete\n");
scanf("%d", &searchNum);
foundLocation = location(HEAD, searchTerm);
printf("Deleted at location %d\n", foundLocation);
if (foundLocation < 0)
printf("This Account does not exist\n");
else if (foundLocation == 1)
deleteAtStart(&HEAD);
else if (foundLocation == length(HEAD))
deleteAtEnd(HEAD);
else
deletedAtLocation(HEAD, foundLocation);
}
}
else if (option == 7)
{
}
else if (option == 8)
{
displayToFile(HEAD);
}
printf("1) Add customer(Note: Account Number must be unique).\n");
printf("2) Display all account whose balance is less than €100 to screen \n");
printf("3) Display account holder details \n");
printf("4) Lodge to Account \n");
printf("5) Withdraw from Account \n");
printf("6) Delete Account \n");
printf("7) Generate statistics \n");
printf("8) Print all account holders into a report file \n");
scanf("%d", &option);
}
}
void addElementAttheEnd(node* top, char studentID[])
{
node* temp;
node* temp2;
temp = (node*)malloc(sizeof(node));
strcpy(temp->accountNum, studentID);
printf("Please enter your first name\n");
scanf("%s", temp->name);
printf("Please enter your surname\n");
scanf("%s", &temp->lastName);
printf("Please enter the year you were born\n");
scanf("%s", &temp->addy);
printf("Enter your initial balance");
scanf("%d", temp->balance);
printf("Please enter your email\n");
scanf("%s", temp->email);
printf("Which of the following areas did you travel from?\n");
printf(" Galway City");
printf(" Galway County");
printf(" Connacht");
printf(" Other Areas of Ireland");
printf(" Outside Ireland");
scanf("%s", &temp->service);
printf("What type of ticket did you purchase\n");
printf(" Normal\n");
printf(" Luxury\n");
scanf("%s", &temp->balance);
temp->NEXT = NULL;
temp2 = top;
while (temp2->NEXT != NULL)
{
temp2 = temp2->NEXT;
}
temp2->NEXT = temp;
}
void addElementAttheStart(node** top, char studentID[])
{
node* temp;
temp = (node*)malloc(sizeof(node));
strcpy(temp->accountNum, studentID);
printf("Please enter your first name\n");
scanf("%s", temp->name);
printf("Please enter your surname\n");
scanf("%s", &temp->lastName);
printf("Please enter the year you were born\n");
scanf("%s", &temp->addy);
printf("Please enter your email\n");
scanf("%s", temp->email);
printf("Which of the following areas did you travel from?\n");
printf(" Galway City\n");
printf(" Galway County\n");
printf(" Connacht\n");
printf(" Other Areas of Ireland\n");
printf(" Outside Ireland\n");
scanf("%s", &temp->service);
printf("What type of ticket did you purchase\n");
printf(" Normal\n");
printf(" Luxury\n");
scanf("%s", &temp->balance);
temp->NEXT = *top;
*top = temp;
}
void display(node* top)
{
node* temp;
if (top == NULL)
printf("The list is empty\n");
else
{
temp = top;
while (temp != NULL)
{
printf("==================================================================================================================\n");
printf("PPSN: %s\n", temp->accountNum);
printf("Firstname: %s\n", temp->name);
printf("Surname: %s\n", temp->lastName);
printf("Year: %s\n", temp->addy);
printf("Email: %s\n", temp->email);
printf("Area: %s\n", temp->service);
printf("Ticket Tyoe: %s\n", temp->balance);
printf("==================================================================================================================\n");
temp = temp->NEXT;
}
}
}
void displayUser(node* userArray, int size)
{
char searchReg[30];
int i;
int found = 0;
printf("Please enter the PPSN your looking for\n");
scanf("%s", searchReg);
for (i = 0; i < size; i++)
{
if (strcmp((userArray + i)->accountNum, searchReg) == 0)
{
printf("%s %s %s %s %s %s %s\n", (userArray + i)->accountNum, (userArray + i)->name, (userArray + i)->lastName, (userArray + i)->addy, (userArray + i)->email, (userArray + i)->service, (userArray + i)->balance);
found = 1;
break;
}
}
if (found == 0)
{
printf("The search did not exist\n");
}
}
void displayToFile(node* top)
{
FILE* outp;
node* temp;
if (top == NULL)
printf("The list is empty\n");
else
{
temp = top;
outp = fopen("customer.txt", "w");
if (outp == NULL)
printf("Sorry the file could not be opened\n");
else
{
while (temp != NULL)
{
fprintf(outp, "PPSN: %20s\n", temp->accountNum);
fprintf(outp, "Firstname: %20s\n", temp->name);
fprintf(outp, "Surname: %20s\n", temp->lastName);
fprintf(outp, "Year: %20s\n", temp->addy);
fprintf(outp, "Email: %20s\n", temp->email);
fprintf(outp, "Area: %20s\n", temp->service);
fprintf(outp, "Ticket Type: %20s\n", temp->balance);
temp = temp->NEXT;
}
fclose(outp);
}
}
}
int search(node* top, char searchID[])
{
node* temp;
if (top == NULL)
printf("The list is empty\n");
else
{
temp = top;
while (temp != NULL)
{
if (strcmp(temp->name, searchID) == 0)
{
printf("Found the element");
return 1;
}
temp = temp->NEXT;
}
}
return 0;
}
int length(node* top)
{
node* temp;
int length = 0;
if (top == NULL)
return 0;
else
{
temp = top;
while (temp != NULL)
{
length++;
temp = temp->NEXT;
}
}
return length;
}
void deleteAtEnd(node* top)
{
node* last;
node* secondlast{};
last = top;
while (last->NEXT != NULL)
{
secondlast = last;
last = last->NEXT;
}
secondlast->NEXT = NULL;
free(last);
}
void deleteAtStart(node** top)
{
node* temp;
temp = *top;
*top = temp->NEXT;
free(temp);
}
void deleteintheMiddle(node* top, char searchID[])
{
node* last;
node* secondlast{};
int removed = 0;
last = top;
while (last->NEXT != NULL)
{
if (strcmp(last->id, searchID) == 0)
{
secondlast->NEXT = last->NEXT;
free(last);
removed = 1;
break;
}
secondlast = last;
last = last->NEXT;
}
if (removed == 0)
{
printf("The node could not be found\n");
}
}
void updateNodeValues(node* top, char ID[])
{
node* temp;
int search = 0;
temp = top;
while (temp != NULL)
{
if (strcmp(temp->accountNum, ID) == 0 || strcmp(temp->name, ID) == 0)
{
printf("Please enter the ppsn you wish to update\n");
printf("Please enter new ppsn\n");
scanf("%s", temp->accountNum);
printf("Please enter new first name\n");
scanf("%s", temp->name);
printf("Please enter new surname\n");
scanf("%s", &temp->lastName);
printf("Please enter the new year you were born\n");
scanf("%s", &temp->addy);
printf("Please enter your new email\n");
scanf("%s", temp->email);
search = 1;
break;
}
}
if (search == 0)
printf("The value of the ppsn was never found\n");
}
void deposit(NodeT_[], int s, int number, int amt)
{
int i = search(list, s, number);
if (i == -1)
{
printf("Record not found");
}
else
{
list[i].balance += amt;
}
}
int location(node* top, char searchID[])
{
node* temp;
int location = -1;
int i = 1;
temp = top;
while (temp != NULL)
{
if (strcmp(temp->accountNum, searchID) == 0)
{
location = i;
break;
}
i++;
temp = temp->NEXT;
}
return location;
}
void deletedAtLocation(node* top, int loc)
{
node* temp;
node* prevTemp{};
int i;
temp = top;
for (i = 0; i < loc - 1; i++)
{
prevTemp = temp;
temp = temp->NEXT;
}
prevTemp->NEXT = temp->NEXT;
free(temp);
}