Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: ATM simulation C program help please

  1. #11
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    The first problem I found was that the program does not compile without errors. If you had reported the error you were getting "undefined label 'Loop'' it would have been helpful.

    Anyway, your problem is that the goto statement and the label are not in the same function. You can not use goto to jump from one function to another. The goto statement and the label have to be in the same function.

  2. #12
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    When posting code fragment it's sometimes good to use CODE as opposed to QUOTE tags. It makes your code a bit easier to read.

    If you're following Jesse code example you wouldn't need to use GOTO
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  3. #13
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    yes exactly what sking said, labels have scope just like variables... u tried to use the label where is is not defined.

    the problem is u try to trigger loop (control structure) in the sub function and not the function controlling it.

    basically
    Code:
     
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    // pause the screen until a key is pressed
    void atmPause(void);
    
    // function verifies the user pin and tells whether access should be granted
    int verifyUser(void);
    
    // controls transaction processing for verified user... until user exits
    int atmMain(void);
    
    // function accepts amount of funds in account and returns the amount after withdraw transaction
    float	withdrawFunds(float balance);
    /*
    // function accepts amount of funds in account and returns the amount after deposit transaction 
    float	depositFunds(float balance);
    // function accepts amount of funds in account, displays it and returns it 
    float	queryFunds(float balance);
    */
    
    int	main()
    {
    	int	user, result;
    	
    	do
    	{
    		user = verifyUser();
    		result = 0;
    
    		atmPause();
    
    		if ( user != 0 ) result = atmMain();
    		if ( result != 0 ) return result;		
    	}
    	while( user != 0 );
    
        return 0;
    } 
    
    // pause the screen until a key is pressed
    void atmPause(void)
    {
    	printf("Processing will continue in a second");
    	sleep(1);
    }
    
    // function verifies the user pin and tells whether access should be granted
    int verifyUser()
    {
    	int	pin;
    
    	printf("The	Bank ATM\n");
    	printf("Please enter your Personal Identification Number");
    	//fflush(stdin);			// input stream may have errors... flush it
    	scanf("&#37;d", &pin);
    
    	if ( pin == 11 )
    	{
    		printf("ACCESS GRANTED!");
    		return 1;                   // u can return a different number for a different user, etc...  (just not 0)
    	}
    	else
    	{ 
    		printf("ACCESS DENIED!");
    		return 0;
    	}
    }
    
    // controls transaction processing for verified user... until user exits
    int atmMain()
    {
    	float current = 0.0f;
    	int option;
    
    	do
    	{
    		clrscr();
    
    		printf("enter an option	from the menu below\n\n");
    		printf("\t\t 1.	Withdrawal \n");
    		printf("\t\t 2.	Deposit\n");
    		printf("\t\t 3.	Inquiry\n");
    		printf("\t\t 4.	Pay	Bills/Credit Card\n");
    		printf("\t\t 5.	Exit\n");
    
    		printf("Please enter your choice\n");
    		//fflush(stdin);			// input stream may have errors... flush it
    		scanf("%d",	&option );		// i would use getch() ... but remember it returns a char
    
    		// if ( option < 1 || option > 5 ) continue;
    
    		switch (option)
    		{
    		case 1:
    			current = withdrawFunds(current);
    			break;
    		case 2:
    			break;
    		case 3:
    			break;
    		case 4:
    			break;
    		case 5:
    			printf("\nThank You for Banking with us\n");
    			break;
    		default: 
    			printf("\nYou have entered an invalid option!");
    		}
    			
    		atmPause();
    	}
    	while( option != 5 );
    
    	return 0; // normal exit... u can return different numbers in the prog to indicate error
    }
    
    // function accepts amount of funds in account and returns the balance after withdraw transaction
    float	withdrawFunds(float balance)
    {
    	float	newbalance, request;
    
    	printf("Enter amount to	withdraw: $\n");
    	//fflush(stdin);			// input stream may have errors... flush it
    	scanf("%f", &request);
    
    	if( balance >= request )
    	{
    		newbalance = balance - request;
    
    		printf("Your balance after	withdrawal is: $%.2f\n", newbalance);
    		return newbalance;
    	}
    	else
    	{
    		printf("Sorry, You do not have sufficient funds\n");
    		return balance;
    	}
    }
    as usual, i didnt run it... i just rearranged the code (mostly).. to put in spacing and the control structures... if, do/while, functions, return

    note... the returns actually return information, they mark for that (when i was there anyway)
    Last edited by icymint3; Apr 16, 2007 at 10:59 AM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #14
    Join Date
    Oct 2004
    Posts
    3,198
    Rep Power
    0

    Default

    Quote Originally Posted by Sking
    The first problem I found was that the program does not compile without errors. If you had reported the error you were getting "undefined label 'Loop'' it would have been helpful.

    Anyway, your problem is that the goto statement and the label are not in the same function. You can not use goto to jump from one function to another. The goto statement and the label have to be in the same function.
    Thank you for pointing that out. Very useful observation.

    Quote Originally Posted by icymint3
    yes exactly what sking said, labels have scope just like variables... u tried to use the label where is is not defined.

    the problem is u try to trigger loop (control structure) in the sub function and not the function controlling it.
    Thank you for advice and suggestion. The misconceptions i had are all cleared up now. Trust me, i grasped a lot. You guys are good.

  5. #15
    Join Date
    Oct 2004
    Posts
    3,198
    Rep Power
    0

    Default

    Ok, so the program is finished but there is a bug in it. (See pic below). The ATM console executes fine but i get an error when using certain features. Could somebody add my address to messenger, alfx7@hotmail.com and offer any possible resolutions to the problem? thanks in advance.


  6. #16
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    The first thing I would do is fix all of those compiler warnings I am seeing in the picture. It is very likely that the compiler is trying to warn you about problems with your program.

  7. #17
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Quote Originally Posted by sking View Post
    The first thing I would do is fix all of those compiler warnings I am seeing in the picture. It is very likely that the compiler is trying to warn you about problems with your program.
    yes i agree do what sking said first, pay specific attention to :

    a. possibly incorrect assignment ... usually happens when u assign instead of check for equality or assign a pointer with the wrong data
    b. possible use of 'variable' before definition... default ur pointers to null before use
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  8. #18
    Join Date
    Oct 2004
    Posts
    3,198
    Rep Power
    0

    Default

    Most of the warnings were addressed. The program is functioning better now but look at this portion of the code and tell me if there are any obvious faults.

    Code:
    void deposit()
    {
    	FILE*cfptr;
    	cust_data cData ={0,0.0,"","","",0.0,0.0};
       float deposit;
       int acc_num;
    
       cfptr=fopen("MasterMode.txt","r+");
       printf("\nPlease enter the account number you wish to deposit to\n");
       printf("---> ");
       scanf("&#37;d",&acc_num);
    
       fseek(cfptr,(acc_num-1)*sizeof(cust_data), SEEK_SET);
       fread(&cData,sizeof(cust_data),1,cfptr);
    
          	if ((cData.acc_num !=0)||(cData.acc_num!=NULL))
          	{
             	textcolor(LIGHTCYAN);
                cprintf("\nPlease enter the amount you wish to deposit\n$");
                scanf("%f",&deposit);
                cData.curr_bal +=deposit;
                cprintf("Your current balance is %.2f",cData.curr_bal);
             }//end of if
             else {
                  		printf("Invalid account number");
                      textcolor(WHITE);
                      cprintf("\r\nReturning to menu....");
                      mainMenu();
                      sleep(2);
                  }//end of else
    The files were created and written to with customer data. Using the deposit function, the extra amount is not added successfully to the initial account balance.

  9. #19
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    I don't see where you write the updated customer data back to the file (but then you didn't show the end of the function so I don't know what happens there).

    NOTE: It might help if you showed the definition of the type "cust_data".

    The "if" statement is unnecessarily complicated. One of the tests is redundant, but I don't know the type of the field "acc_num" so I don't know which test is most appropriate.

    You don't do enough error checking (in general when you call a library function you need to check whether the call succeeded before using the results).

  10. #20
    Join Date
    Oct 2004
    Posts
    3,198
    Rep Power
    0

    Default

    Ok, thanks for suggestions. Check your PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •