Thread: Wont go past while statement

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

    Wont go past while statement

    Here's the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int		account;		
    	float	balance;		
    	float	current;		
    	float	items;			
    	float	credit_total;
    	float	limit;			
    
    
    
    		printf("Enter account number (-1 to end): ");  /*	Ask for account number */
    		scanf("%d" , &account);
    	
    	while (account != -1);{
    
    		printf("Enter beginning balance: ");
    		scanf( "%.2f" , &balance);
    
    		printf("Enter total charges: ");
    		scanf("%.2f", &items);
    
    		printf("Enter credit limit: ");
    		scanf(".2f" , &limit);
    
    		printf("Enter total credits: ");
    		scanf(".2f" , &credit_total);
    
    
    		printf("Account: " , account);
    		
    		current = balance + items - credit_total;
    
    		printf("Credit Limit: " , current);
    
    		printf("Enter account number (-1 to end): ");
    		scanf("%d" , &account);
    
    	if(current > limit){
    		printf("Credit Limit Exceeded\n");
    		}
    	}
    	return 0;
    }
    It wont go past the while staement so all I get is:
    Enter account number:

    Maybe someone can shed some light on why.
    thank you so much,
    Extro

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Code:
    while (account != -1);{
    you dont need that semi-colon there.
    get rid of that and it should go past the while statement.
    good luck buddy

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    whoa, one litle wayward semicolon, thanks.
    Now it will let me input account balance but wont wait for input
    after anything else.
    Enter credit limit, enter beginning balance and so on all run into each other.
    Any feedback is appreciated,
    Extro

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    I'm sorry but this didnt help I'm still confused why my input statements are running into one another. Any feedback is appreciated.
    Thanks,
    Extro

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know it's a lot of words to read, but it does in fact help. If you actually read it. I'll quote the relevant portion, since you apparently don't like to read much.
    There are two problems with using scanf() to get a number:

    First, validation/error handling is poor. If the user enters 1234ZZ as their number, scanf() will read 1234 into the int variable, and leave the ZZ in the input buffer. Although the user has entered what appears to be an invalid number, the program hasn't actually noticed, and will continue processing as if all is well.

    The second problem is that of leaving characters in the buffer, in particular the newline character tends to catch people out. To show what I mean, here is some sample code that uses scanf() to get a number, followed by fgets() to get a string. As you may of heard already, fgets() is designed for reading strings, but cannot read numbers directly like scanf() can.
    If you'd care to read the fix, go read the section titled Option 2.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM