Thread: ATM in C, what's the bug?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    Question ATM in C, what's the bug?

    okay, so i need to make a stimulated ATM in C for my programming class, i've made it, but it doesn't run the whole code properly, it stops at wherever there the 2nd scanf is.
    here my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int p(balance){
    	char option, trans, secop, secopd;
    	do{
    	printf("what transaction would you like to do\n");
    	printf("    W-Withdraw          D-Deposit\n");
    	printf("    C-Check Balance     P-Pay bills\n");
    	printf("    Q-Quit\n\n");
    	scanf("%c", &option);
    	if (option=='w'){
    		printf("what's your withdraw amount?\n");
    		printf("    A $20.00    D $80.00\n");
    		printf("    B $40.00    E $100.00\n");
    		printf("    C $60.00    F Cancel\n");
    		scanf("%c", &secop);
    		if(secop=='a'){
    			printf("$20.00 withdrawn\n");
    			balance-20;
    			printf("would you like to make another transaction?[Y/N]\n");
    			scanf("%c", &trans);
    			if(trans=='n'){
    				return balance;
    			}
    		}
    		if(secop=='b'){
    			printf("$40.00 withdrawn\n");
    			balance-40;
    			printf("would you like to make another transaction?[Y/N]\n");
    			scanf("%c", &trans);
    
    			if(trans=='n'){
    				return balance;
    			}
    		}
    		if(secop=='c'){
    			printf("$60.00 withdrawn\n");
    			balance-60;
    			printf("would you like to make another transaction?[Y/N]\n");
    			scanf("%c", &trans);
    
    			if(trans=='n'){
    				return balance;
    			}
    		}
    		if(secop=='d'){
    			printf("$80.00 withdrawn\n");
    			balance-80;
    			printf("would you like to make another transaction?[Y/N]\n");
    			scanf("%c", &trans);
    
    			if(trans=='n'){
    				return balance;
    			}
    		}
    		if(secop=='e'){
    			printf("$100.00 withdrawn\n");
    			balance-100;
    			printf("would you like to make another transaction?[Y/N]\n");
    			scanf("%c", &trans);
    
    			if(trans=='n'){
    				return balance;
    			}
    		}
    		if(secop=='f'){
    			return balance;
    		}
    	}
    
    	if (option=='d'){
    			printf("what's your deposit amount?\n");
    			printf("    A $20.00    D $80.00\n");
    			printf("    B $40.00    E $100.00\n");
    			printf("    C $60.00    F Cancel\n");
    			scanf("%c", &secopd);
    			if(secopd=='a'){
    				printf("$20.00 deposited\n");
    				balance+20;
    				printf("would you like to make another transaction?[Y/N]\n");
    				scanf("%c", &trans);
    
    				if(trans=='n'){
    					return balance;
    				}
    			}
    			if(secopd=='b'){
    				printf("$40.00 deposited\n");
    				balance+40;
    				printf("would you like to make another transaction?[Y/N]\n");
    				scanf("%c", &trans);
    
    				if(trans=='n'){
    					return balance;
    				}
    			}
    			if(secopd=='c'){
    				printf("$60.00 deposited\n");
    				balance+60;
    				printf("would you like to make another transaction?[Y/N]\n");
    				scanf("%c", &trans);
    
    				if(trans=='n'){
    					return balance;
    				}
    			}
    			if(option=='d'){
    				printf("$80.00 deposited\n");
    				balance+80;
    				printf("would you like to make another transaction?[Y/N]\n");
    				scanf("%c", &trans);
    
    				if(trans=='n'){
    					return balance;
    				}
    			}
    			if(secopd=='e'){
    				printf("$100.00 deposited\n");
    				balance+100;
    				printf("would you like to make another transaction?[Y/N]\n");
    				scanf("%c", &trans);
    
    				if(trans=='n'){
    					return balance;
    				}
    			}
    			if(option=='f'){
    				return balance;
    			}
    		}
    
    	if(option=='c'){
    		printf("%i", balance);
    		printf("\nwould you like to make another transaction?[Y/N]\n");
    		scanf("%c", &trans);
    		if(trans=='n'){
    			return balance;
    		}
    	}
    	if(option=='p'){
    		int account, amount;
    		printf("\nplease enter the account's number that you wish to pay your bills to:\n");
    		scanf("%i", &account);
    		printf("how much is your bill?\n");
    		scanf("%i", &amount);
    		printf("$%i.00 is paid to %i", amount, account);
    		printf("would you like to make another transaction?[Y/N]\n");
    		scanf("%c", &trans);
    		if(trans=='n'){
    			return balance;
    		}
    	}
    	if (option=='q'){
    		return balance;
    	}
    	}
    	while(trans=='y');
    	return balance;
    }
    
    int main(void) {
    	int balance;
    	balance = rand() % 500 + 270;
    	printf("    ++++++++\n");
    	printf("    +RBNWSS+\n");
    	printf("    ++++++++\n");
    	p(balance);
    	return 0;
    }
    so what's my error, i've removed a lot of code in order for it to run, for example, it's suppose to ask the user for there pin, and it should only proceed if the pin entered is 1234, but with the pin code in place, if statements seems to loose its function.

    help would be greatly appreciated

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Each time you're getting a char from the console. But each time you leave '\n' in the console buffer. So when your next scanf is called, it doesn't wait for the user because it finds data in the buffer to read. That data is most likely '\n', that's why it stops.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Quote Originally Posted by Sipher View Post
    Each time you're getting a char from the console. But each time you leave '\n' in the console buffer. So when your next scanf is called, it doesn't wait for the user because it finds data in the buffer to read. That data is most likely '\n', that's why it stops.
    what? isn't \n equivalent to <br> in in html?
    it just means change line, how can is mess up anything?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by evo85210 View Post
    what? isn't \n equivalent to <br> in in html?
    it just means change line, how can is mess up anything?
    Code:
    #include<stdio.h>
    int main( void )
    {
        int c;
        do
        {
            printf( "enter a character, or q to quit\n" );
            c = getchar();
            printf( "you entered \'%c\'", c );
        }
        while( c != 'q' );
        return 0;
    }
    That's how.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by evo85210 View Post
    what? isn't \n equivalent to <br> in in html?
    it just means change line, how can is mess up anything?
    Because it is sitting unprocessed in the input buffer.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yet another cross-poster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disappearing Bug
    By el-sid in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 12:12 PM
  2. I really need help with my ATM program
    By ash4741 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2009, 10:11 AM
  3. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  4. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  5. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM