Thread: decimal being stored as 0

  1. #1
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9

    decimal being stored as 0

    Hi, this is only the second program I have ever done in C. It is for my college class, and it is driving me crazy. I think I got it all right, but for some reason on the first two imputs, it acts like you are imputting a 0, when you are just inputing a decimal number. I can't figure out why it isn't using the decimal number you enter.
    Please help if you can thanks

    Note: it is the profrate and trate that seem to be the problem. The input values for the two are .1 for the trate, and .2 for the profrate.
    When run, the results come out as if you had entered 0 for both

    Code:
    /*
    	Project 3 program solution to the Project 1 problem. 
    	11/03/2005
    	by Phil Pliuskonis
    */
             
    
    #include<stdio.h>
     
             
    // Function prototypes
    void title(void);
    void initial(void); 
    void inputrate(void); 
    void inputdata(void);
    void process(void);
    void unit_price(void);
    void sales_cost_ammt(void);
    void item_prof(void);
    void output_1(void);
    void totals(void);
    void loop_out(void);
    void high_prof(void);
    void calc_ta(void); 
    void rpt_out(void);
    // Data declarations
    
    int tsales, tcost, tunits, ttax, tprof, hiprof, hicode, avgprof, itemprof, saleamt, costamt, code, qty, ucost, uprice;
    float profrate, trate;
    char more_item;
    
    
    
    main()						
    	{
    		title();
    		initial();	
    		inputrate(); 
    		while (more_item !='N') 
    			{
    				process();
    			}
    		
    		
    		calc_ta();				
    						
    		rpt_out();				
    		return 0;				
    	}
    void title(void)
    	{
    		printf("\nProject 3 by: Phil Pliuskonis\n");
    		return;
    	}
    void inputdata(void)
    	{	
    		printf("\nInput item code:  ");
    		scanf("%d", &code);
    		printf("\nInput item quantity: ");
    		scanf("%d", &qty);
    		printf("\nInput unit cost:  ");
    		scanf("%d", &ucost);
    		return;
    	}	
    
    void inputrate(void)
    	{	
    		float trate, profrate;
    		printf("\nInput tax rate (decimal number) : ");
    		scanf("%f", &trate);
    		printf("\nInput profit rate (decimal number) : ");
    		scanf("%f", &profrate);
    		return;
    	}				
    
    void initial(void)
    	{					
    
    		tsales = tcost = tunits = hiprof = 0;
    		more_item = 'Y';				 
    		return;					
    	}
    void process(void)					
    	{
    		inputdata();
    		unit_price();
    		sales_cost_ammt();
    		item_prof();
    		output_1();
    		totals();
    		high_prof();
    		loop_out();
    		return;			
    	}
    void unit_price(void)	
    	{			
    		uprice = ucost + profrate * ucost;				
    		return;	
    	}
    void sales_cost_ammt(void)
    	{
    		saleamt = qty * uprice;
    		costamt = qty * ucost;
    		return;			
    	}					
    
    void item_prof(void)
    	{
    		itemprof = saleamt - costamt;
    		return;			
    	}				
    
    void output_1(void)
    	{
    		printf("\nCode: %d\n", code);
    		printf("Quantity: %d\n", qty);
    		printf("Unit cost: %d\n", ucost);
    		printf("Unit price: %d\n", uprice);
    		printf("Sale ammount: %d\n", saleamt);
    		printf("Cost ammount: %d\n", costamt);
    		printf("Item profit: %d\n", itemprof);
    		return;			
    	}				
    
    void totals(void)
    	{
    		tsales = tsales + saleamt;
    		tcost = tcost + costamt;
    		tunits = tunits + qty;
    		return;			
    	}				
    void high_prof(void)
    {
    	if (itemprof > hiprof)
    		{
    			hiprof = itemprof;
    			hicode = code;
    		}
    	return;
    }
    void loop_out(void)					
    	{
    		printf("\n Are there any more items to process? (Y/N): ");
    		fflush(stdin);
    		scanf("%c", &more_item);
    		if (more_item == 'n')
    		{
    			more_item = 'N';
    		}	
    								
    		return;
    	}					
    void calc_ta(void)
    {
    	
    	tprof = tsales - tcost;
    	avgprof = tprof / tunits;
    	ttax = trate * tsales;
    	return;
    }
    void rpt_out(void)
    	{						
    
    		printf("Total sales: %d\n", tsales);
    		printf("Total profit: %d\n", tprof);
    		printf("Average profit: %d\n", avgprof);
    		printf("Total tax: %d\n", ttax);
    		printf("High profit: %d\n", hiprof);
    		printf("High item code: %d\n", hicode);
    		return;
    	}

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Floating point numbers must be stored in float(s). Trying to store an integer in a float will result in a 0 being stored at best, and at worst some off the wall value.

  3. #3
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    Sorry am I missing something? I thought they were stored in float(s)?
    Or am I just not seeing it?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The problem is you have
    Code:
     float profrate, trate;
    declared globally, and also declared in your inputrate() function. The local variables are the ones that actually get assigned the values, and then the function goes out of scope (and so do those variables). This means you global profrate and trate remain at zero. Delete the declaractions from the local function, and I think your program will behave more the way you intended.

  5. #5
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    Quote Originally Posted by bithub
    The problem is you have
    Code:
     float profrate, trate;
    declared globally, and also declared in your inputrate() function. The local variables are the ones that actually get assigned the values, and then the function goes out of scope (and so do those variables). This means you global profrate and trate remain at zero. Delete the declaractions from the local function, and I think your program will behave more the way you intended.
    Wow perfect. Thank you very much. I could have sworn that I had already tried that, but that must have been when I still had them declared under int, and not float.
    Thanks again

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Those kinds of problems can be tough to spot for people who are new to C

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And you shouldn't do this:
    Code:
    fflush(stdin);
    See the FAQ.
    Last edited by dwks; 11-04-2005 at 04:16 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Sorry didn't read the code close enough.

    Gimme a break it's early here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2008, 12:39 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM