Getting an error msg and needed some help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deenar
    New Member
    • Sep 2010
    • 13

    #1

    Getting an error msg and needed some help!

    I'm trying to obtain the amount of 50s,20s,10s & 50s to give when someone enters an amount in coins. I have done the code but I'm getting error msgs. Please help...

    Code:
    void GetAmt()
    {
    	int amt;
    	
    	printf("Please enter amount:  ");
    	scanf("%d%*c", &amt);
    	
    	return;
    }
    	
    void ChkAmt()
    {
    	int amt;
    	
    	if ((amt<5 && amt>95) || (amt%5!=0))
    		printf("Not a valid amount");
    
    	return;
    }
    
    void MakeChange(int n, int num, int &amt)
    {
    	
    	
    	num=amt/n;
    	amt=amt-(num*n);
    	
    	return(num);
    }
    	
    
    	
    int main()
    {
    	int amt;
    	
    	GetAmt();
    	ChkAmt();
    	
    	
    	printf("n50=MakeChange(amt, 50)");
    	printf("n20=MakeChange(amt, 20)");
    	printf("n10=MakeChange(amt, 10)");
    	printf("n5=MakeChange(amt, 5)");
    	
    	return(0);
    }
    The error Msg is: 'MakeChange(int ,int,int &)' cannot return a value in function MakeChange(int, int, int &)
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    this is because you have written 'void' as a return type for 'MakeChange' method. That tells the compiler that this function will not be returning anything.

    At the contrary, you are returning 'num', which is of type 'int'

    So change the return type of the function, something like this:

    Code:
    int MakeChange(int n, int num, int &amt)

    Comment

    • deenar
      New Member
      • Sep 2010
      • 13

      #3
      Hi
      Thanks Ashitpro for that,I should have looked at it more carefully. I entered that in but now I got more error msgs as I had to change a few things. Sorry i don't mean to just dump things on here but i've been trying to crack my head on this one for the past hour.

      Code:
      int MakeChange(int n, int num, int &amt)  
      { 
          num=amt/n; 
          amt=amt-(num*n); 
        
          return(num); 
      } 
        
      void PrintResults(int amt)
      {   
          int amt;
      
          printf("n50= ", MakeChange(amt, 50)); 
          printf("n20= ", MakeChange(amt, 20)); 
          printf("n10= ", MakeChange(amt, 10)); 
          printf("n5= ", MakeChange(amt, 5)); 
      	
          return;
      }  
      int main() 
      { 
          int amt; 
        
          GetAmt(); 
          ChkAmt();   
          PrintResults();
      
        
          return(0); 
      }
      the error msg i'm getting now is too few parameters in call to 'MakeChange(int ,int,int & in function PrintResults. Thanks once again

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Signature says 'MakeChange' method accepts 3 parameters..
        Code:
        int MakeChange(int n, int num, int &amt)
        But while calling 'MakeChange' from 'PrintResults', you are supplying only 2 arguments i.e. amt and some number

        You have to change your method to something like this:

        Code:
        int MakeChange(int &amt,int n)  
        { 
             int num;
             num=amt/n; 
             amt=amt-(num*n); 
          
             return(num); 
        }

        Comment

        Working...