Thread: Help me debug this code

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    Help me debug this code

    my if else statement has an error it said that there is no if before else in my loop
    here is my code:

    Code:
    
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    	int choice, deposit_amount, withdraw_amount, pin, x, i, trial=0;
    	float initial_amount = 20000, current_amount;
    	char width, trans;
    	printf("#######################################################################################################################");
    	printf("\n#                                                                                                                     #");
    	printf("\n#                                               WELCOME TO LANDBANK                                                   #");
    	printf("\n#     ATM 24 - HOUR SERVICES                                                                 VISA|BANCHET|PAYMAYA     #");
    	printf("\n#######################################################################################################################");	
    
    
    	while (pin != 012345)
    	{
    		for(x=1; x<=3; x++)
    		{
    			printf("\n");
    			printf("%45c ENTER YOUR SECRET PIN NUMBER: ", width);
    			scanf("%d", &pin);
    					
    			if (pin == 012345);
    			{
    				besugo:
    				printf("\n\n%37c*************Welcome to ATM Service*************", width);
    				printf("\n%49c 1. Balance Inquiry ", width);
    				printf("\n%50c 2. Withdraw Cash ", width);
    				printf("\n%50c 3. Deposit Cash ", width);
    				printf("\n%50c 4. Quit ", width);
    				printf("\n%37c************************************************", width);
    				printf("\n\n%45c Enter your choice: ", width);
    				scanf("%d", &choice);
    			
    				switch(choice)
    				{					
    					case 1:
    					printf("%45c YOUR BALANCE IS: %.0f", width ,initial_amount);
    					whodoes:
    					printf("\n%45c Do you wish to have another transaction?(Y/N): ",width);
    					scanf("%c", &trans);	
    					scanf("%c", &trans);
    				
    					switch (trans)
    					{				
    					case 'Y': 	
    					goto besugo;
    					break;
    											
    					case 'N':	
    					printf("\n\n%45c THANK YOU FOR USING ATM SERVICE!", width);
    					break;
    					}
    								{
    								break;
    								}
    					case 2:
    					printf("%45c ENTER AMOUNT TO WITHDRAW: ",width);
    					scanf("%d", &withdraw_amount);
    					current_amount = initial_amount - withdraw_amount;				
    					if (withdraw_amount>10000)
    					{
    					printf("\n%45c You can only withdraw maximum amount of 10,000.00", width);
    					goto whodoes;
    					}
    					else
    					{
    							printf("\n%45c PLEASE COLLECT CASH.......", width); 	
    					printf("\n%45c YOUR CURRENT BALANCE IS %.0f", width, current_amount);
    					goto whodoes;
    					break;
    					}
    								{
    								break;
    								}
    					case 3:	
    					printf("%45c ENTER THE AMOUNT TO DEPOSIT: ", width);
    					scanf("%d", &deposit_amount);
    					current_amount = initial_amount + deposit_amount;
    					printf("%45c YOUR CURRENT BALANCE IS %.0f", width, current_amount);
    					goto whodoes;
    					break;					
    							{
    							break;
    							}
    					case 4:
    					printf("\n\n%45c THANK YOU FOR USING ATM SERVICE!", width);
    					break;
    		
    					default:
    					printf("\n\n%45cINVALID TRANSACTION!", width); 
    					break;			
    							{							
    							break;
    							}					
    				}
    			}
    			else 
    			printf("PLEASE ENTER A CORRECT PIN NUMBER")
    			scanf("%d", &trial);
    			printf("%d trial left", trial++);
    			if(trial == 3)
    			{
    				printf("ACCOUNT IS TEMPORARILY BANNED");
    				break;
    			}					
    		}
    		
    	}
    getch();
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    * moved to C programming forum *
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    Code:
    if (pin == 012345);
    Look for semi colons like the above that should not be there!

    My experience is to not add any semi colon that I do not know is needed.

    The compiler does great on telling about missing semi colons; but, a very poor job telling you about the ones that should not be there.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,804
    So you pretty much ignored everything in your last thread.
    My pincode is having an error, it was supposed to have a 3 attempts.

    Still using goto's
    Still using octal constants
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339

    Cool Debug

    Code:
    line 98||99 else with no open bracket {
    line 99 line not ended with semicolon ;
    Code:
    }
                else 
                printf("PLEASE ENTER A CORRECT PIN NUMBER")
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please debug the following code....
    By progmateur in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2013, 11:57 AM
  2. Help debug palindrome code
    By mckinnley in forum C Programming
    Replies: 11
    Last Post: 01-02-2011, 11:00 AM
  3. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  4. Help me debug this code
    By Blgihted in forum C Programming
    Replies: 4
    Last Post: 10-28-2005, 07:39 AM
  5. DEBUG code
    By myer_784 in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 11:45 PM

Tags for this Thread