Help with program-functions

This is a discussion on Help with program-functions within the C Programming forums, part of the General Programming Boards category; I hope someone can help me. I think I'm on the right track but am missing a piece of the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Question Help with program-functions

    I hope someone can help me. I think I'm on the right track but am missing a piece of the puzzle. I'm hoping, wishing (okay begging) someone to take a look at my program and provide me with some feedback. Please let me know if I'm completely off base or what.
    The program is an invoice. The user inputs the invoice cost, then inputs the discount code. Based on the code used the discount rate is determined. Using a function I am to calculate the discount amount. The program should return the initial invoice cost, discount amount and final invoice cost.
    Thanks,
    Angela


    Code:
    #include<stdio.h>
    
    /* Program will calculate customer's discount based on code entered by user*/
    /* At program's termination the initial invoice amount, discount amount and
       final invoice cost will be display.*/
       
    float Calc_Discount();
    void Display_Results();
    
    int main()
    {
    	extern float initial_invoice_amount, discount_amount, final_cost;
    	extern char discount_code;
    	
    	/*Display dicount percent with mathching code*/
    	printf("Discount code A = 5%%.\n");
    	printf("Discount code B = 8%%.\n");
    	printf("Discount code C = 12%%.\n");
    	
    	/*User Input*/
    	printf("Input the customer's invoice amount:\n");
    	scanf("%f", & initial_invoice_amount);
    	
    	printf("Input the customer's discount code:\n");
    	scanf("%c", &discount_code);
    		
    		
    	final_cost = (initial_invoice_amount - discount_amount);
    		
    	Display_Results();
    	
    	return 0;
    	}	 
    		  Calc_Discount(discount_code)
    		   {
    		     if((discount_code='A') || (discount_code='a'))
    		      {
    		        discount_amount == (0.05*initial_invoice_amount);
    		      }
    		     return discount_amount;
    		     
    		    if((discount_code='B') || (discount_code='b'))
    		     {
    		       discount_amount == (0.08*initial_invoice_amount);
    		     }
    		    return discount_amount;
    		    
    		    if((discount_code='C') || (discount_code='c'))
    		     {
    		       discount_amount == (0.12*initial_invoice_amount);
    		     }
    		      return discount_amount;
    		   }
    		      
    		  void Display_Results(float initial_invoice_amount,float discount_amount, float final_cost)
    		  {
    		    printf("\nThe invoice charges are as follows:\n");
    		    printf("\nInitial Invoice: %f", initial_invoice_amount);
    		    printf("\nDiscount Amount: %f", discount_amount);
    		    printf("\n--------------------------");
    		    printf("\nFinal Cost: %f", final_cost);
    		 }
    		   
    		      
    	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're defining your functions inside of the 'main' function. This is wrong. Do this:
    Code:
    float Calc_function( ...whatever... )
    {
        ...code goes here...
    }
    
    void Display_function( ...whatever... )
    {
        ...code goes here...
    }
    
    int main( void )
    {
        ...code goes here...
        ...use functions here...
        return 0;
    }
    Each function is a seperate entity. They need to be declared out on their own. If this was a C++ class, it'd be different, but it's C, so do it as per above.

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

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you have more { than }. fix your code so it shows what you intend for it to do.
    hello, internet!

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    In the function prototype

    Code:
    float Calc_Discount();
    has no formal parameters

    in the function definition
    Code:
    Calc_Discount(discount_code)
    has one formal parameter. This is an error.

    Also, why are you using extern with the variables, here you do not need them.
    Mr. C: Author and Instructor

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    See if this hatchet job will be of any use. It's late and I'm tired, so things were cutback from your original post. Let's just hope the cutbacks weren't part of the programs purpose

    Code:
    #include <stdio.h> 
    #include <ctype.h>
    
    void showResults(float, double, double);
    double calcDiscount(char, float);
    
    int main(void)
    {
        float initialAmount;
        double discountAmount, finalCost;
        
        char discountCode;
    	
    	printf("Discount code A = 5%%.\n");	
    	printf("Discount code B = 8%%.\n");
    	printf("Discount code C = 12%%.\n");
    		
    	printf("\nInput the customer's discount code: ");
    	scanf("%c", &discountCode);
    	
    	printf("Input the customer's invoice amount: ");
    	scanf("%f", &initialAmount);
    	
    	discountAmount = calcDiscount(discountCode, initialAmount);
    	
    	finalCost = initialAmount - discountAmount;
    	
    	showResults(initialAmount, discountAmount, finalCost);
    	
    	return 0;
    
    }	 		  
    		
    		
    double calcDiscount(char code, float amount)
    {
    	
    	double total;
    	
    	switch(toupper(code))
    	{
    		case 'A': total = 0.05 * amount;
    		          break;
    		
    		case 'B': total = 0.08 * amount;
    		          break;
    		
    		case 'C': total = 0.12 * amount;
    		          break;
    		
    		default: printf("\nThere is no such option for %c\n", code);
    		         total = 0.0;
    		         break;
    	};
    	
    	return total;
    } 		   
    		   
         
    		  
    void showResults(float amount1, double amount2, double amount3)
    {		    
    	printf("\nThe invoice charges are as follows:\n");
    	printf("\nInitial Invoice: %.2f", amount1);
    	printf("\nDiscount Amount: %.2f", amount2);
    	printf("\n--------------------------");
    	printf("\nFinal Cost: %.2f", amount3);	
    
    }
    Good luck.
    Last edited by ronin; 10-13-2002 at 12:59 AM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Smile Thanks

    Thanks everyone for the help. I really appreciate the feedback. Part of my problem is that I have a C++ book but the Instructor is teaching C. He told us that we would still be able to use the book we were told to buy. However, I'm having a hard time switching back and forth between the two.
    Again thanks for the help.

    Angela

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 03:43 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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