Click here to Skip to main content
15,900,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

	}



	/** Repopulate the linked list based on the file **/

	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);

				//Add the fileValue data to the list 

			}



		}



		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)
		{
			//displayUser(HEAD)

		}



		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;

	//Creating the new node 

	temp = (node*)malloc(sizeof(node));

	//Reading in the data  
	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;



	//Finding the last node 

	temp2 = top;

	while (temp2->NEXT != NULL)

	{

		temp2 = temp2->NEXT;

	}



	temp2->NEXT = temp;

}



void addElementAttheStart(node** top, char studentID[])

{

	node* temp;



	//Creating the new node 

	temp = (node*)malloc(sizeof(node));



	//Reading in the data  

	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 currently has the address of the first node  

	//in the list which become the second node when temp is added infront of it 

	*top = temp;

}



void display(node* top)

{

	node* temp;

	if (top == NULL)

		printf("The list is empty\n");

	else

	{

		temp = top;



		while (temp != NULL)

		{

			//Display payload data 

			//Move to the next node 

			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)

		{

			//Read into this element 

			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)

			{

				//Display payload data 

				//Move to the next node 

				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)

		{

			//Display payload data 

			//Move to the next node 

			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)

		{

			//Display payload data 

			//Move to the next node 

			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 searchNumber; 

	int removed = 0;



	last = top;







	while (last->NEXT != NULL)

	{

		if (strcmp(last->id, searchID) == 0)

		{

			//This is the node to remove... 

			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");



			//Reading in the data 

			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);
}
Posted
Updated 27-Aug-21 21:39pm
v2

You have code to do the "incoming money" operation - the deposit function - and we have no idea what it is doing that you don't expect, or not doing that you do. And nobody wants to wade through 1000 lines of student grade code to try and work out what the heck is going on and how you use that app. Would you?

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Your deposit function is some insert function and the withdraw is some delete function. So the operation is very clear. Watch this linked list tutorial to learn about the details.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900