problem getting decimal not to = 0

This is a discussion on problem getting decimal not to = 0 within the C Programming forums, part of the General Programming Boards category; Ok here is my program Code: /* Project 5 solution 11/14/2005 by Phil Pliuskonis */ #include<stdio.h> // Function prototypes void ...

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

    problem getting decimal not to = 0

    Ok here is my program
    Code:
    /*
    	Project 5 solution 
    	11/14/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);
    void item_number(void);
    // Data declarations
    
    int code[20];
    int qty[20];
    int ucost[20];
    int uprice[20];
    int saleamt[20];
    int costamt[20];
    int itemprof[20];
    
    int tsales, tcost, tunits, ttax, tprof, hiprof, hicode, avgprof, j, x, i, p, q, r, w, t;
    float profrate, trate;
    char more_item;
    
    
    
    main()						
    	{
    		title();
    		initial();	
    		inputrate(); 
    		item_number();
    		while (more_item !='N') 
    			{
    				process();
    			}
    		
    		
    		calc_ta();				
    						
    		rpt_out();				
    		return 0;				
    	}
    void title(void)
    	{
    		printf("\nProject 5 by: Phil Pliuskonis\n");
    		return;
    	}
    void inputdata(void)
    	{	
    		
    		
    		for (x=0; x<j; ++x)
    		{
    			printf("\nInput item code:  "); 
    			scanf("%d", &code[x]);
    		}	
    		
    		for (x=0; x<j; ++x)
    		{
    			printf("\nInput item quantity: "); 
    			scanf("%d", &qty[x]);
    		}	
    		
    		for (x=0; x<j; ++x)
    		{
    			printf("\nInput unit cost:  ");
    			scanf("%d", &ucost[x]);
    		}	
    		return;
    	}	
    
    void inputrate(void)
    	{	
    		
    		printf("\nInput tax rate (decimal number) : ");
    		scanf("%f", &trate);
    		printf("\nInput profit rate (decimal number) : ");
    		scanf("%f", &profrate);
    		return;
    	}			
    void item_number(void)
    {
    	printf("\n How many items do you have to input today? :");
    		scanf("%d", &j);
    		return;
    }
    
    void initial(void)
    	{					
    
    		tsales = tcost = tunits = hiprof = j = p = q = r = w = x = i = t = 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)	
    	{	
    		for (t=0; t<j; ++t)
    		{
    			uprice[t]= ucost[t] * profrate + ucost[t];
    		}	
    						
    		return;	
    	}
    void sales_cost_ammt(void)
    	{
    		for (p=0; p<j; ++p)
    		{
    		saleamt[p] = qty[p] * uprice[p];
    		costamt[p] = qty[p] * ucost[p];
    		}	
    		
    		return;			
    	}					
    
    void item_prof(void)
    	{
    		for (q=0; q<j; ++q)
    		{
    		itemprof[q] = saleamt[q] - costamt[q];
    		}	
    		
    		return;			
    	}				
    
    void output_1(void)
    	{
    		printf("%4s  %13s  %9s %10s %13s %12s %11s\n", "Item", "Quantity Sold", "Unit Cost", "Unit Price", "Sales Ammount", "Cost Ammount", "Item Profit");
       for ( i = 0; i < j; ++i )
       {
          printf("%4d  %13d  %9d %10d %13d %12d %11d\n", code[i], qty[i], ucost[i], uprice[i], saleamt[i], costamt[i], itemprof[i]);
       }
    		return;
    	}				
    
    void totals(void)
    	{
    		for (w=0; w<j; ++w)
    		{
    		tsales = tsales + saleamt[w];
    		tcost = tcost + costamt[w];
    		tunits = tunits + qty[w];
    		}	
    		
    		return;			
    	}				
    void high_prof(void)
    {
    	for (r=0; r<j; ++r)
    		{
    			if (itemprof[r] > hiprof)
    				{
    					hiprof = itemprof[r];
    					hicode = code[r];
    				}
    		}	
    	
    	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 Cost: %d\n", tcost);
    		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;
    	}

    Here is the output
    Code:
    Item  Quantity Sold  Unit Cost Unit Price Sales Ammount Cost Ammount Item Profit
     121             75         50         50          3750         3750           0
     145             50        100        100          5000         5000           0
     201             40        150        150          6000         6000           0
     225             10        300        300          3000         3000           0
     275             22        450        450          9900         9900           0
     331             20        600        600         12000        12000           0
     335             21        700        700         14700        14700           0
     360             10       1000       1000         10000        10000           0
     444             15        500        500          7500         7500           0
     445             20        500        500         10000        10000           0
    
     Are there any more items to process? (Y/N): n
    Total sales: 81850
    Total Cost: 81850
    Total profit: 0
    Average profit: 0
    Total tax: 0
    High profit: 0
    High item code: 0
    Now as you can see, I am having trouble getting the correct output.
    I narrowed it down and determined that it is all happening because profrate is pretty much = 0.

    I input it correctly when I run the program (is supposed to be .2) but I can't figure out why it doesn't take. In the output it keeps acting like 0.

    We are re visiting this program (for school), and the first time I did it I couldn't get it to work either. I ended up hardcoding in profrate =.2. That won't work this time however because the assignment requires that it be from input.

    Please help, why won't profrate take the .2 value?

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >Now as you can see
    Can you show us what output you're expecting? Don't assume that we're as familiar with your project requirements as you are.

    >In the output it keeps acting like 0.
    As wild speculation, I imagine the actual value is less than 0, but since you output the values as integers, the precision is cut off and all you have left is 0.

  3. #3
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    All of the correct outputs are integers except for average profit. So I will need to change that, but that shouldn't affect the other outputs

    This is what the output is supposed to be
    http://clem.mscd.edu/~morrellj/cms21...cts/proj3s.htm
    (under Test Run and pwd Command near the bottom of the page)
    Except don't pay attention to the formatting, all the info for this one will need to be in a table like format.

    Really the only problem is profrate is = 0 and not the input value

  4. #4
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    Do you have to float things that will be using a decimal number even if the output isn't a decimal?

  5. #5
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    Never mind!
    I got it. I don't even know what I did, but it works now.
    I did set the avg profit to float. Maybe that was it.
    thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 08:51 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. decimal being stored as 0
    By scoketboy in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 01:31 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21