Thread: Messed up output

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

    Messed up output

    Here's the code:

    Code:
    	/*	Programming In C Assignment 4, Project 1
    
    		Program Name: 	Mileage Calculator
    
    		Date:			July 10th
    
      		Developed by:   Bryan Bucknell
    
    		Description:  	This program will calculate  miles per 
    						gallon for a car's gas tank
    	*/
    
    
    #include <stdio.h>
    
    int main()
    {
    		int		account;	
    		float	balance;		
    		float	charges;
    		float	credit;
    		float	limit;
    	
    		
    
    			while(account != -1){
    			printf("Enter account number (-1 to end): ");
    			scanf("%d" , &account);
    
    			printf("Enter beginning balance: ");
    
    		
    			printf("Enter total charges: ");
    			scanf("%.2f" , &charges);
    
    			printf("Enter total credits: ");
    			scanf("%.2f", &credit);
    
    			printf("Enter credit limit: ");
    				scanf(".2f", &limit);
    			
    			if(credit > limit){
    				printf("Credit Limit Exceeded\n");
    			}				
    		}
    		return 0;
    		
    }
    All the enter total charges, enter credits..etc run together without waiting for input, I wondered in someone could help me sort this mess out.
    Thanks,
    Extro

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Account is uninitialized so attempting to compare it's value to -1 is undefined behavior. You are missing a magical '%' character in your scanf's.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Though account should be initialized, it is very rarely going to influence this program. The check is whether or not account equals -1. Since account gets set to the random garbage memory that was left behind by another program when declared, it will never (except for a very very slim chance) start off as -1.

    The problem comes when you use your scanf.

    Say you enter:
    4456
    for your account number. When you hit "enter", this gets sent to the input buffer (i.e. data from the keyboard waiting to be processed):
    4456\n
    scanf() reads in the integer: 4456
    However, you're still left with a '\n' (note, this is 1 character, not two, it sybolizes a new line) character. So, the very next scanf() (for total charges) reads in the '\n' character, and all of a sudden you're at "total credits".

    Now, there are several ways to clear the input buffer so that this doesn't happen. After every scanf() you have one of these options:

    Option 1
    fflush(stdin);
    NEVER USE THIS OPTION (Read why here).

    Option 2
    Create your own code. Along the lines of:
    Code:
    int ch;
    
    while(((ch = getchar()) != '\n') && (ch != EOF));
    Note: stdio.h must be included (for the EOF macro definition), but since you're using scanf(), you'll usually be okay (unless you turn this into its own function). This can also be used for waiting for a "keypress" as you can read about here (from the faq).

    This option systematically reads from the input buffer until a newline is encountered.

    Hope this helps some
    Last edited by Epo; 08-03-2005 at 09:56 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ok I fixed up a few minor errrors but the input questions are still running into one another from "enter total charges" forward. No idea whats causeing this to happen. here's the code again:
    Code:
    #include <stdio.h>
    
    int main()
    {
    		int		account;	
    		float	balance;		
    		float	charges;
    		float	credit;
    		float	limit;
    	
    		
    	account = 0;
    			while(account != -1){
    			printf("Enter account number (-1 to end): ");
    			scanf("%d" , &account);
    
    			printf("Enter beginning balance: ");
    			scanf("%.2f" , &balance);
    
    				printf("Enter total charges: ");
    			scanf("%.2f" , &charges);
    
    			printf("Enter total credits: ");
    			scanf("%.2f", &credit);
    
    			printf("Enter credit limit: ");
    				scanf(".%2f", &limit);
    			
    			if(credit > limit){
    				printf("Credit Limit Exceeded\n");
    			}				
    		}
    		return 0;
    		
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Both "%.2f" and ".%2f" are incorrect, for "one" thing (scanf is not identical to printf). Input does not have precision and the decimal point preceding a directive is significant.

    I'd recommend reading user input as a string and then attempting to convert. Something similar is posted in the FAQ.

    [edit]That said, this may be closer to what you are trying to do.
    Code:
    #include <stdio.h>
    
    int main()
    {
       float balance, charges, credit, limit;
       int   account = 0;
       for ( ;; )
       {
          printf("Enter account number (-1 to end): ");
          scanf("%d" , &account);
    
          if ( account == -1 )
          {
             break;
          }
    
          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;
    }
    But I'd really take the "input as string, then convert" approach. And maybe add several fflush(stdout)'s.
    Last edited by Dave_Sinkula; 08-03-2005 at 10:15 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM