C program - Kindly help

This is a discussion on C program - Kindly help within the C Programming forums, part of the General Programming Boards category; Hi ppl, This is my first post in CPROGRAMMIG.com I have this program with me : Programme performing banking operation ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Unhappy C program - Kindly help

    Hi ppl,

    This is my first post in CPROGRAMMIG.com



    I have this program with me : Programme performing banking operation

    Code:
    #include<stdio.h>
    #include<conio.h>
    void withdr(int acc,int n);
    void dep(int acc,int n);
    void bal(int acc,int n);
    struct bank
    {
    	int acc;
    	long int amt;
    }a[10];
    void main()
    {
    int i,j,w1=0,temp,n,op,acc,amt;
    clrscr();
    printf("\n Enter the number of acc numbers : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    w1=0;
    printf("\n Enter the acc number: ");
    scanf("%d",&temp);
    for(j=0;j<i;j++)
    {
    if(temp==a[j].acc)
    {
    w1=1;
    break;
    }
    }
    if(w1==1)
    {
    printf("\n Acc No already exist! ");
    i--;
    }
    else
    {
    a[i].acc=temp;
    printf("\n Enter the Amount: ");
    scanf("%ld",&a[i].amt);
    }
    }
    
    
    do
    {
    getch();
    clrscr();
    printf("\n Select the operation you want to perform: ");
    printf("\n 1.Balance \n 2.Withdraw \n 3.Deposit \n 4.Exit\n");
    printf("Enter your Option:");
    scanf("%d",&op);
    if(op!=4)
    {
    printf("\n Enter the acc no: ");
    scanf("%d",&acc);
    }
    switch(op)
    {
    case 1:
    withdr(acc,n);
    break;
    case 2:
    dep(acc,n);
    break;
    case 3:
    bal(acc,n);
    break;
    case 4:
    break;
    default:printf("\n Enter a value between 1 and 4");
    }
    }while(op!=4);
    getch();
    }
    void withdr(int acc,int n)
    {
    	int w=0,i;
    	for(i=0;i<n;i++)
    	{
    		if(acc==a[i].acc)
    		{
    			w=1;
    		printf("\n The balance in the acc no %d is "
    "%d",a[i].acc,a[i].amt);
    		}
    	 }
    	if(w==0)
    		printf("\n Wrong Account number! ");
    }
    void dep(int acc,int n)
    {
    	int i=0,w=0,amt;
    	for(i=0;i<n;i++)
    	{
    		if(acc==a[i].acc)
    		{
    			w=1;
                		break;
            		}
        	}
        	if(w==0)
        	{
    		printf("\n Wrong Account number! ");
        	}
        	else
        	{
                 	if(a[i].amt-amt<0)
    			printf("\n Negative Balance! ");
    		else
    		{
                		printf("\n Enter the Amt you want to withdraw: ");
    	        		scanf("%ld",&amt);
                		a[i].amt=a[i].amt-amt;
                		printf("\n %d Rs withdrawn ",amt);
    			printf("\n New balance is: %ld",a[i].amt);
    		}
        	}
    }
    void bal(int acc,int n)
    {
    	int i=0,w=0,amt;
        	for(i=0;i<n;i++)
        	    	if(acc==a[i].acc)
    	    	{
    		    	w=1;
                		break;
    	     	}
       	if(w==0)
    	        printf("\n Wrong Account number! ");
        	else
        	{
            		printf("\n Enter the amt you want to Deposit: ");
    	    	scanf("%ld",&amt);
            		a[i].amt=a[i].amt+amt;
            		printf("\n %d Rs Deposited ",amt);
            		printf("\n New balance is: %ld",a[i].amt);
        	}
    }

    This program can do only one operation per account ,i mean one account can perform only Withdraw money or deposit money or chk for balance .. if i try to withdraw ,after deposit .. it shows wrong account ..pls help me on this .. is loop needed for performing other operations? am very confused

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, in order to actually read through the code you should first indent properly. Pick an indenting style that works and stick with it. Most people don't care what type of style you use as long as you're consistent and it doesn't look like slop. Otherwise, it will be difficult for you and others to read and understand your code.

    For portability, and in general, good programming practices, I would suggest you consider the following:

    • Don't be dependent upon getch(), clrscr(), or conio.h. You will not be able to use them on all systems.
    • Don't use void main(). This is not allowed by the C standard except on specific systems (which if you're running Windows XP, that doesn't include your situation).
    • Don't use global variables except in places where you really need them. This limits your functions and is more open to error.
    • Don't mix and match types. If you have a long int, use a long int specifier in your printf() and scanf() functions. Mixing and matching is just inviting issues.


    As far as your actual problem goes, because of the lack of proper indention, I'm having trouble reading and understanding your code. Because of your lack of portability, I'm unable to currently run your code with my favorite compiler.

    BTW, why oh why would you do something so stupid as to name the functions totally different than what they actually do? Are you some sort of sadist that wants to torture programmers like me? This shouldn't be an obfuscation lesson here.

    Your problem is here:

    Code:
    if(a[i].amt-amt<0)
    			printf("\n Negative Balance! ");
    		else
    		{
                		printf("\n Enter the Amt you want to withdraw: ");
    	        		scanf("%ld",&amt);
                		a[i].amt=a[i].amt-amt;
                		printf("\n %ld Rs withdrawn ",amt);
    			printf("\n New balance is: %ld",a[i].amt);
    		}
    You're using the value of amt before amt is even set to anything valid. Therefore it contains random garbage that you're performing math operations with. This will randomly cause issues.

    Fixing a lot of the garbage in your program, this is what I came up with:

    Code:
    #include<stdio.h>
    /*#include<conio.h>*/
    void withdr(int acc,int n);
    void dep(int acc,int n);
    void bal(int acc,int n);
    struct bank
    {
    	int acc;
    	long int amt;
    }a[10];
    int main()
    {
    int i,j,w1=0,temp,n,op,acc/*,amt*/;
    /*clrscr();
    */
    printf("\n Enter the number of acc numbers : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    w1=0;
    printf("\n Enter the acc number: ");
    scanf("%d",&temp);
    for(j=0;j<i;j++)
    {
    if(temp==a[j].acc)
    {
    w1=1;
    break;
    }
    }
    if(w1==1)
    {
    printf("\n Acc No already exist! ");
    i--;
    }
    else
    {
    a[i].acc=temp;
    printf("\n Enter the Amount: ");
    scanf("%ld",&a[i].amt);
    }
    }
    
    
    do
    {
    /*getch();
    clrscr();
    */
    printf("\n Select the operation you want to perform: ");
    printf("\n 1.Balance \n 2.Withdraw \n 3.Deposit \n 4.Exit\n");
    printf("Enter your Option:");
    scanf("%d",&op);
    if(op!=4)
    {
    printf("\n Enter the acc no: ");
    scanf("%d",&acc);
    }
    switch(op)
    {
    case 1:
    withdr(acc,n);
    break;
    case 2:
    dep(acc,n);
    break;
    case 3:
    bal(acc,n);
    break;
    case 4:
    break;
    default:printf("\n Enter a value between 1 and 4");
    }
    }while(op!=4);
    /*getch();
    */
    return 0;
    }
    void withdr(int acc,int n)
    {
    	int w=0,i;
    	for(i=0;i<n;i++)
    	{
    		if(acc==a[i].acc)
    		{
    			w=1;
    		printf("\n The balance in the acc no %d is "
    "%ld",a[i].acc,a[i].amt);
    		}
    	 }
    	if(w==0)
    		printf("\n Wrong Account number! ");
    }
    void dep(int acc,int n)
    {
    	int i=0,w=0;
    	long int amt;
    	for(i=0;i<n;i++)
    	{
    		if(acc==a[i].acc)
    		{
    			w=1;
                		break;
            		}
        	}
        	if(w==0)
        	{
    		printf("\n Wrong Account number! ");
        	}
        	else
        	{
    		printf("\n Enter the Amt you want to withdraw: ");
    		scanf("%ld",&amt);
    		if(a[i].amt-amt<0)
    		{
    			printf("\n Negative Balance! ");
    		}
    		else
    		{
    			a[i].amt=a[i].amt-amt;
    			printf("\n %ld Rs withdrawn ",amt);
    			printf("\n New balance is: %ld",a[i].amt);
    		}
        	}
    }
    void bal(int acc,int n)
    {
    	int i=0,w=0;
    	long int amt;
        	for(i=0;i<n;i++)
        	    	if(acc==a[i].acc)
    	    	{
    		    	w=1;
                		break;
    	     	}
       	if(w==0)
    	        printf("\n Wrong Account number! ");
        	else
        	{
            		printf("\n Enter the amt you want to Deposit: ");
    	    	scanf("%ld",&amt);
            		a[i].amt=a[i].amt+amt;
            		printf("\n %ld Rs Deposited ",amt);
            		printf("\n New balance is: %ld",a[i].amt);
        	}
    }
    I'm not in a good enough mood to clean it up totally, but this is enough that it appears to work and it compiles with MinGW without any errors (that I could raise anyway).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    man.. very sorry for giving you lots of trouble .. this wont happen in future .Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21