Thread: If statement isnt working

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    If statement isnt working

    Simple question if I may, in the following code why is this statement not working?:
    Code:
    if(account == -1)
    break;
    here' the full code:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
       float balance, charges, credit, limit;
       int   account = 0;
       for ( ;; )
    
    	  while(account != -1){
       
          printf("Enter account number (-1 to end): ");
          scanf("%d" , &account);
                  	  
          printf("Enter beginning balance: ");
          scanf("%f" , &balance);
    	  
          printf("Enter total charges: ");
          scanf("%f" , &charges);
    
          printf("Enter total credits: ");
          scanf("%f", &credit);
    
          printf("Enter credit limit: ");
          scanf("%f", &limit);
    	   
          if ( credit > limit )
    		printf("Credit Limit Exceeded\n");
    		
    		if(account == -1)
    			break;
    	      
       }
    	  
       return 0;
    }
    What I want is when the user does enter - 1 it ends the program, tried it a few different ways and cant't get it to work, thanks for all the help.
    Regards,
    Extro

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Have you tried printing account back using printf after the scanf that's supposed to change it? Just debug it a bit, you may find the answer yourself.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ya I did and it almost works, if I use a -1 right away it runs throough one more time, whwn it should just end it. Here's the changed code:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
       float balance, charges, credit, limit;
       int   account = 0;
       for ( ;; )
    
    	  while(account != -1){
       
          printf("Enter account number (-1 to end): ");
          scanf("%d" , &account);
               	  
          printf("Enter beginning balance: ");
          scanf("%f" , &balance);
    	  
          printf("Enter total charges: ");
          scanf("%f" , &charges);
    
          printf("Enter total credits: ");
          scanf("%f", &credit);
    
          printf("Enter credit limit: ");
          scanf("%f", &limit);
    
    	  
    	   
          if ( credit > limit )
    		printf("Credit Limit Exceeded\n");
    		
    		if(account == -1)
    			printf("%d", account);
    			break;
    	      
       }
    	  
       return 0;
    }
    So I'm still kinda at a loss as to how to end the program if account == -1, thanks for your help.
    regards,
    Extro

  4. #4
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    break exits while, but you've also got a infinite for there.
    i also think you should test if account is -1 immediately after you read it.
    Last edited by moonlord; 08-08-2005 at 09:10 AM.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    Cool, got the code to work:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
       float balance, charges, credit, limit;
       int   account = 0;
    
      	  while(account != -1){
       
          printf("Enter account number (-1 to end): ");
          scanf("%d" , &account);
    
    	  if(account == -1){
    			printf("%d", account);
    			break;
    	  }
    			else{
          printf("Enter beginning balance: ");
          scanf("%f" , &balance);
    	  
          printf("Enter total charges: ");
          scanf("%f" , &charges);
    
          printf("Enter total credits: ");
          scanf("%f", &credit);
    
          printf("Enter credit limit: ");
          scanf("%f", &limit);
    
    	  
    	   
          if ( credit > limit )
    		printf("Credit Limit Exceeded\n");
    		
    			}
    	      
       }
    	  
       return 0;
    }
    Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  3. crazy if statement
    By jamespond88 in forum C Programming
    Replies: 26
    Last Post: 03-19-2009, 04:02 PM
  4. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  5. Replies: 1
    Last Post: 08-31-2004, 04:07 AM