Error c2660 in C programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bil M
    New Member
    • Jan 2012
    • 11

    Error c2660 in C programming

    I amm getting 3 errors for , please help..

    error C2660: 'calculate_disc ount' : function does not take 1 arguments

    the code is

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int calculate_discount(int,int,int);
    
    int main()
    {
    
    	int item1,item2,item3;
    	int price1,price2,price3;
    	
    
    
    	/* For reference
    		Item	Discount
    		Item 1 	20%
    		Item 2 	30%
    		Item 3	40% 
    
    	*/
    		printf("Enter Price for Item 1: \n");
    		scanf("%d",&item1);
    		printf("Enter Price for Item 2: \n");
    		scanf("%d",&item2);
    		printf("Enter Price for Item 3: \n");
    		scanf("%d",&item3);
    
    		
    		printf("Net price of Item 1: %d \n",calculate_discount(item1)); /*ERROR IS HERE*/	
    		printf("Net Price of Item 2: %d \n",calculate_discount(item2)); /*ERROR IS HERE*/
    		printf("Net Price of Item 3: %d \n",calculate_discount(item3)); /*ERROR IS HERE*/
    	
    		getch();
    }
    
    int calculate_discount(int item1,int item2, int item3)
    
    {
    
    	int price1,price2,price3;
    
    	if(item1)
    		price1=item1-((item1*20)/100);
    	else if(item2)
    		price2=item2-((item2*20)/100);
    	else if (item3)
    		price3=item3-((item2*20)/100);
    		
    	return item1,item2,item3;
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    int calculate_discount(int,int,int);
    says that calculate_disco unt needs htree arguments that are ints. You call it like this:

    Code:
    calculate_discount(3,4,6);
    You are only using one argument. Hence the error.

    You may have a design issue. With three items you would call calculate_disco unt using one the one item whose discount is needed.

    Code:
    calculate_discount(item1);
    so you would declare your function as:
    Code:
    int calculate_discount(int);
    and not:
    Code:
    int calculate_discount(int,int,int);

    Comment

    • Bil M
      New Member
      • Jan 2012
      • 11

      #3
      but that would only allow me to calculate 1 discount value...what about the other 3..should i make seperate items for it

      Comment

      • Bil M
        New Member
        • Jan 2012
        • 11

        #4
        could you please post the error-free solution..as I am new to Pointers

        Comment

        • Bil M
          New Member
          • Jan 2012
          • 11

          #5
          i must use
          Function prototype: void calculatediscou nt(int *, int *, int *)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            As it stands now, your code won't compile.
            In addition to your reported problem, the calculate_disco unt function is attempting to return 3 its. No dice. A function may return a type or a pointer to a type and that's it. Only one value.

            Now you say you must use:
            Code:
            void calculatediscount(int *, int *, int *)
            but you have coded this in your program:
            Code:
            int calculate_discount(int,int,int);
            Do you see any difference?
            The one in your program won't work for you but the one you must use will.
            Isuggest you code the correct function in your program and then post again if you are still stuck.

            Comment

            • Bil M
              New Member
              • Jan 2012
              • 11

              #7
              this is what i have done but the answer is not calculated but instead comes as same as the input...

              Code:
              
              #include <stdio.h>
              #include <conio.h>
              
              
              void calculate_discount(int*,int*,int*);
              
              void main()
              {
              
              	int item1,item2,item3;
              	
              	
              
              
              	/* For reference
              		Item	Discount
              		Item 1 	20%
              		Item 2 	30%
              		Item 3	40% 
              
              	*/
              		printf("Enter Price for Item 1: \n");
              		scanf("%d",&item1);
              		printf("Enter Price for Item 2: \n");
              		scanf("%d",&item2);
              		printf("Enter Price for Item 3: \n");
              		scanf("%d",&item3);
              
              
              		printf("Net price of Item 1: %d \n",item1);	
              		printf("Net Price of Item 2: %d \n",item2);
              		printf("Net Price of Item 3: %d \n",item3);
              	
              		getch();
              }
              
              void calculate_discount(int *item1,int *item2, int *item3)
              
              {
              
              	int price1,price2,price3;
              
              	if(item1)
              		price1=*item1-((*item1*20)/100);
              	else if(item2)
              		price2=*item2-((*item2*20)/100);
              	else if (item3)
              		price3=*item3-((*item3*20)/100);
              		
              	
              
              }

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Your function calculates price1, price2 and price3 but these are local variables inside the function and they are destroyed when the function completes. You have to do something to get the results passed out of the function.

                You could have three more arguments for that:
                Code:
                void calculate_discount(int *item1,int *item2, int *item3,
                 int* price1, int* price2, int* price3);
                The best solution is:
                Code:
                int calculate_discount(int item);
                where you call the function with one of the items and it returns the discount price for that item. Then you can call it using each item and get the discount price for that item.

                However, you said you had to use:
                Code:
                void calculatediscount(int *, int *, int *)
                where there is no way to get the discount price back from the function. I guess you could display it inside the function but that is a very poor design choice since you don't generally want screen displays spattered throughout the code as that leads to a very complicated user interface control.

                Comment

                Working...