Hi there,
Kindly see my attached code below. This is already running however, I do have a little problem with the WITHDRAWAL part.

For example:
My current balance is 500 and I withdraw 501. It will tell me at first that I have insufficient funds and would prompt me to enter another amount. However, If I enter 501. It will tell me that I have -1 as my New balance which should't be. What I want is it will keep me prompting an mount either less than or equal to my current balance so that it will not display a negative new balance.

#include <stdio.h>
#include <stdlib.h>
int dep=0,ab=0,wdraw=0;
void acctbal()
{
	printf ("\n\n---Account Balance---\n\n");
	printf ("\n\nYour Current Balance is: %d\n\n",ab);
}
void deposit ()
{
	printf ("\n\n---Deposit---\n\n");
	printf ("Enter Amount     :");
	scanf ("%d",&dep);
	ab=ab+dep;
	printf ("\n\nAmount deposited: %d",dep);
	printf ("\nNew Balance: %d\n\n",ab);
}
void withdraw ()
{
	if (ab==0)
	{
		system("ds");
		printf ("\n\nplease be reminded that your current balance is: %d\n", ab);
		return;
	}
	else
	{
		printf ("\n\n--Withdraw--\n\n");
		printf ("Enter Amount :");
		scanf ("%d", &wdraw);
	}
	if (ab>=wdraw)
	{
		ab=ab-wdraw;
		printf ("\n\nAmount withdraw: %d",wdraw);
		printf ("\nNew balance: %d\n\n", ab);
	}
	else
	{
		printf ("\n\n Insufficient Funds--\n");
		printf ("please be reminded that your current balance is: %d\n",ab);
		printf ("\n\n---Withdraw---\n\n");
		printf ("Enter new amount: ");
		scanf ("%d",&wdraw);
		ab=ab-wdraw;
		printf ("\n\namount withdraw: %d",wdraw);
		printf ("\n\nNew balance: %d\n\n",ab);
	}
}
void main()
{
	int ans;
	char oper;
a:
	system ("ds");
		printf ("\n\nTransactions\n\n");
	printf ("D-		Deposit\n");
	printf ("W-		Withdraw\n");
	printf ("A-		Account Balance\n");
x:
	printf ("Enter desired transcation:");
	scanf ("%s",&oper);
	if (oper=='D'||oper=='d')
		deposit();
	else if (oper=='W'||oper=='w')
		withdraw();
	else if (oper=='A'||oper=='a')
		acctbal();
else
{
	printf("\n\n--Invalid Input---\n");
	printf("***PLEASE ENTER A VALID TRANSACTION***\n\n");
	goto x;
}
c:
printf ("\ndo you want to continue? Press [1]-for yes [2] for no:");
scanf ("%d", &ans);
if (ans==1)
goto a;
else if (ans==2)
{
	printf ("\n---THANK YOU FOR BANKING WITH US---\n\n");
	return;
}
else
{
	printf ("\n\n---INVALID INPUT---\n\n");
	goto c;
}
}

The if statement (line 32 - 48) needs to be inside the else block (line 27 - 31) because that code should only be run if ab != 0. The return at line 24 then becomes redundant (it is not uncommon for coding standards to ban returns in the middle of functions).

Then you need to put a big loop round the new contents of the else block, remove the second request the amount and go round the loop if !(ab>=wdraw) to get more input.

The basic design should be that there is only a single place that requests the input of the value that you then test and use if it is valid. if it is not valid you output an error and start the process again.

Instead of asking for input again inside your function, you should print out a message (insufficient funds) and then call your withdraw function again recursively. Then the second prompt is identical to the first.

The if statement (line 32 - 48) needs to be inside the else block (line 27 - 31) because that code should only be run if ab != 0. The return at line 24 then becomes redundant (it is not uncommon for coding standards to ban returns in the middle of functions).

Then you need to put a big loop round the new contents of the else block, remove the second request the amount and go round the loop if !(ab>=wdraw) to get more input.

The basic design should be that there is only a single place that requests the input of the value that you then test and use if it is valid. if it is not valid you output an error and start the process again.

Can you please tell me how? I'm not that good yet with C. Thanks.

Instead of asking for input again inside your function, you should print out a message (insufficient funds) and then call your withdraw function again recursively. Then the second prompt is identical to the first.

I do not know how to do that actually. I'm so sorry.

I do not know how to do that actually. I'm so sorry.

Well, its pretty simple. Just call withdraw() again. Something like this:

if (ab>=wdraw)
{
ab=ab-wdraw;
printf ("\n\nAmount withdraw: %d",wdraw);
printf ("\nNew balance: %d\n\n", ab);
}
else
{
withdraw();
}

You see, in my example, if you try to withdraw too much, then it goes back to the beginning of the withdraw() function.
I'm assuming this is a homework assignment. You may not have learned recursion yet in your class, its very useful. However, if your teacher/professor does not allow you to use concepts that you haven't learned yet, you may need to look for an alternate answer. If that is the case, think about something like a while loop, a for loop could also work, but the syntax would look a little silly. If this is not homework, then I recommend recursion, as that is the cleanest way to do it. If you have learned recursion before, use it, professors love students that use recursion.

Well, its pretty simple. Just call withdraw() again. Something like this:

if (ab>=wdraw)
{
ab=ab-wdraw;
printf ("\n\nAmount withdraw: %d",wdraw);
printf ("\nNew balance: %d\n\n", ab);
}
else
{
withdraw();
}

You see, in my example, if you try to withdraw too much, then it goes back to the beginning of the withdraw() function.
I'm assuming this is a homework assignment. You may not have learned recursion yet in your class, its very useful. However, if your teacher/professor does not allow you to use concepts that you haven't learned yet, you may need to look for an alternate answer. If that is the case, think about something like a while loop, a for loop could also work, but the syntax would look a little silly. If this is not homework, then I recommend recursion, as that is the cleanest way to do it. If you have learned recursion before, use it, professors love students that use recursion.

Yeah, it is one of the exercises given...I have my solution to code below but still trying to figure out the problem with the code above...Here is my complete code to the working one:

#include <stdio.h>
int d, w, cb, trans,res;

Deposit()
{
	printf("\nYour current balance is %d\n",cb);
	printf("\nPlease enter amount to be deposited:");
	scanf("%d",&d);
	cb+=d;
	printf("\nYou have successfully deposit to your account!!!");
	printf ("\nYour previous balance is %d\n", cb-d);
	printf("\nYour current balance is %d\n",cb);
	return(0);
}

Withdraw()
{
n:
	if(cb>0)
	{
		printf("\nEnter Withdraw Amount:");
		scanf("%d",&w);

		if(w>cb)
		{
			printf("\nSorry!You have Entered an Insufficient amount!\n");
			printf("\nPlease Enter another Amount.\n");
			goto n;
		}
		else
		{
			cb-=w;
			printf("\nYou have succesfully withdraw"); 
			printf("\nYour New Balance is :%d\n",cb);
		}
	}
	else
	{
		printf("\nYou do not have any money moron.\n");
	}
		
	return(0);
}

Inquiry()
{
	printf("\nYour Current Balance is:%d\n\n",cb);
	return(0);
}


void main()
{
start:
	printf ("\n*************Welcome to LBC Easy Banking System*************\n");
	printf("\n\n************* M E N U *************\n\n");
	printf("[1]-Deposit\n[2]-Withdraw\n[3]-Account Balance\n\n");
	printf("Enter Your Transaction type:");
	scanf("%d",& trans);

	if(trans==1)
	{
		Deposit();
	}
	else if(trans==2)
	{
		Withdraw();
	}
	else if(trans==3)
	{
		Inquiry();
	}
	else
	{
		printf("\nYour have entered the wrong code.\n");
		printf("Please try again!!!\n");
		goto start;
	}
	


	printf("\nDo you want to continue[1]-Yes [2]-No:");
	scanf("%d",&res);
	
	if(res==1)
	{
	
		goto start;
	}
	else 
	{
		printf("\nThank you for banking with us. Have a nice day!\n\n\n");
		
	}
}

Yeah, it is one of the exercises given...I have my solution to code below but still trying to figure out the problem with the code above...Here is my complete code to the working one:

#include <stdio.h>
int d, w, cb, trans,res;

Deposit()
{
	printf("\nYour current balance is %d\n",cb);
	printf("\nPlease enter amount to be deposited:");
	scanf("%d",&d);
	cb+=d;
	printf("\nYou have successfully deposit to your account!!!");
	printf ("\nYour previous balance is %d\n", cb-d);
	printf("\nYour current balance is %d\n",cb);
	return(0);
}

Withdraw()
{
n:
	if(cb>0)
	{
		printf("\nEnter Withdraw Amount:");
		scanf("%d",&w);

		if(w>cb)
		{
			printf("\nSorry!You have Entered an Insufficient amount!\n");
			printf("\nPlease Enter another Amount.\n");
			goto n;
		}
		else
		{
			cb-=w;
			printf("\nYou have succesfully withdraw"); 
			printf("\nYour New Balance is :%d\n",cb);
		}
	}
	else
	{
		printf("\nYou do not have any money moron.\n");
	}
		
	return(0);
}

Inquiry()
{
	printf("\nYour Current Balance is:%d\n\n",cb);
	return(0);
}


void main()
{
start:
	printf ("\n*************Welcome to LBC Easy Banking System*************\n");
	printf("\n\n************* M E N U *************\n\n");
	printf("[1]-Deposit\n[2]-Withdraw\n[3]-Account Balance\n\n");
	printf("Enter Your Transaction type:");
	scanf("%d",& trans);

	if(trans==1)
	{
		Deposit();
	}
	else if(trans==2)
	{
		Withdraw();
	}
	else if(trans==3)
	{
		Inquiry();
	}
	else
	{
		printf("\nYour have entered the wrong code.\n");
		printf("Please try again!!!\n");
		goto start;
	}
	


	printf("\nDo you want to continue[1]-Yes [2]-No:");
	scanf("%d",&res);
	
	if(res==1)
	{
	
		goto start;
	}
	else 
	{
		printf("\nThank you for banking with us. Have a nice day!\n\n\n");
		
	}
}

That is essentially what I suggested, except with a goto statement instead of recursion. I personally never use goto in C because it can make your code look sloppy and hard to follow.
If you can't figure out what was wrong with your original code, follow the code line by line manually. Take special notice of what will happen when the user enters a bad value the second time. Following you code manually is almost always a good way to find bugs like that.

That is essentially what I suggested, except with a goto statement instead of recursion. I personally never use goto in C because it can make your code look sloppy and hard to follow.
If you can't figure out what was wrong with your original code, follow the code line by line manually. Take special notice of what will happen when the user enters a bad value the second time. Following you code manually is almost always a good way to find bugs like that.

Honestly, I do know how to use one. And I'm not prohibited to use either. Can you please tech me how?

Ok, well lets take a quick look at your original code. I have quoted your original post because it includes the full code and your original post where you gave some good example values. Your full post is below:

Hi there,
Kindly see my attached code below. This is already running however, I do have a little problem with the WITHDRAWAL part.

For example:
My current balance is 500 and I withdraw 501. It will tell me at first that I have insufficient funds and would prompt me to enter another amount. However, If I enter 501. It will tell me that I have -1 as my New balance which should't be. What I want is it will keep me prompting an mount either less than or equal to my current balance so that it will not display a negative new balance.

#include <stdio.h>
#include <stdlib.h>
int dep=0,ab=0,wdraw=0;
void acctbal()
{
	printf ("\n\n---Account Balance---\n\n");
	printf ("\n\nYour Current Balance is: %d\n\n",ab);
}
void deposit ()
{
	printf ("\n\n---Deposit---\n\n");
	printf ("Enter Amount     :");
	scanf ("%d",&dep);
	ab=ab+dep;
	printf ("\n\nAmount deposited: %d",dep);
	printf ("\nNew Balance: %d\n\n",ab);
}
void withdraw ()
{
	if (ab==0)
	{
		system("ds");
		printf ("\n\nplease be reminded that your current balance is: %d\n", ab);
		return;
	}
	else
	{
		printf ("\n\n--Withdraw--\n\n");
		printf ("Enter Amount :");
		scanf ("%d", &wdraw);
	}
	if (ab>=wdraw)
	{
		ab=ab-wdraw;
		printf ("\n\nAmount withdraw: %d",wdraw);
		printf ("\nNew balance: %d\n\n", ab);
	}
	else
	{
		printf ("\n\n Insufficient Funds--\n");
		printf ("please be reminded that your current balance is: %d\n",ab);
		printf ("\n\n---Withdraw---\n\n");
		printf ("Enter new amount: ");
		scanf ("%d",&wdraw);
		ab=ab-wdraw;
		printf ("\n\namount withdraw: %d",wdraw);
		printf ("\n\nNew balance: %d\n\n",ab);
	}
}
void main()
{
	int ans;
	char oper;
a:
	system ("ds");
		printf ("\n\nTransactions\n\n");
	printf ("D-		Deposit\n");
	printf ("W-		Withdraw\n");
	printf ("A-		Account Balance\n");
x:
	printf ("Enter desired transcation:");
	scanf ("%s",&oper);
	if (oper=='D'||oper=='d')
		deposit();
	else if (oper=='W'||oper=='w')
		withdraw();
	else if (oper=='A'||oper=='a')
		acctbal();
else
{
	printf("\n\n--Invalid Input---\n");
	printf("***PLEASE ENTER A VALID TRANSACTION***\n\n");
	goto x;
}
c:
printf ("\ndo you want to continue? Press [1]-for yes [2] for no:");
scanf ("%d", &ans);
if (ans==1)
goto a;
else if (ans==2)
{
	printf ("\n---THANK YOU FOR BANKING WITH US---\n\n");
	return;
}
else
{
	printf ("\n\n---INVALID INPUT---\n\n");
	goto c;
}
}

Now, lets only look at your withdraw function, since that is where the bug is:

void withdraw ()
{
	if (ab==0)
	{
		system("ds");
		printf ("\n\nplease be reminded that your current balance is: %d\n", ab);
		return;
	}
	else
	{
		printf ("\n\n--Withdraw--\n\n");
		printf ("Enter Amount :");
		scanf ("%d", &wdraw);
	}
	if (ab>=wdraw)
	{
		ab=ab-wdraw;
		printf ("\n\nAmount withdraw: %d",wdraw);
		printf ("\nNew balance: %d\n\n", ab);
	}
	else
	{
		printf ("\n\n Insufficient Funds--\n");
		printf ("please be reminded that your current balance is: %d\n",ab);
		printf ("\n\n---Withdraw---\n\n");
		printf ("Enter new amount: ");
		scanf ("%d",&wdraw);
		ab=ab-wdraw;
		printf ("\n\namount withdraw: %d",wdraw);
		printf ("\n\nNew balance: %d\n\n",ab);
	}
}

Lets use your original example input values for reference (500 and 501). ab is = to 500, so the first if statement skips right to the else part where it asks the user for input. The user enters 501, which then causes wdraw to be set to 501. The next line to be executed checks to see if ab<=wdraw. Since this is true, we go to the else section again.
This is where your bug is, it prints "insufficient funds", the current balance, and the asks for a new amount to withdraw. wdraw gets set to whatever the user entered (501 again). Then the next line subtracts from the balance, it doesn't check if there are sufficient funds again, it just blindly withdraws the funds. Instead of asking for a new amount, what you should do it replace that second question with another call to withdraw(), that way, if the funds are insufficient, we just start the withdraw function over again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.