By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Getting an error msg and needed some help!

P: 13
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2. void GetAmt()
  3. {
  4.     int amt;
  5.  
  6.     printf("Please enter amount:  ");
  7.     scanf("%d%*c", &amt);
  8.  
  9.     return;
  10. }
  11.  
  12. void ChkAmt()
  13. {
  14.     int amt;
  15.  
  16.     if ((amt<5 && amt>95) || (amt%5!=0))
  17.         printf("Not a valid amount");
  18.  
  19.     return;
  20. }
  21.  
  22. void MakeChange(int n, int num, int &amt)
  23. {
  24.  
  25.  
  26.     num=amt/n;
  27.     amt=amt-(num*n);
  28.  
  29.     return(num);
  30. }
  31.  
  32.  
  33.  
  34. int main()
  35. {
  36.     int amt;
  37.  
  38.     GetAmt();
  39.     ChkAmt();
  40.  
  41.  
  42.     printf("n50=MakeChange(amt, 50)");
  43.     printf("n20=MakeChange(amt, 20)");
  44.     printf("n10=MakeChange(amt, 10)");
  45.     printf("n5=MakeChange(amt, 5)");
  46.  
  47.     return(0);
  48. }    
  49.  
The error Msg is: 'MakeChange(int,int,int &)' cannot return a value in function MakeChange(int,int, int &)
Sep 16 '10 #1

✓ answered by ashitpro

Signature says 'MakeChange' method accepts 3 parameters..
Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int n, int num, int &amt)
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int &amt,int n)  
  2.      int num;
  3.      num=amt/n; 
  4.      amt=amt-(num*n); 
  5.  
  6.      return(num); 
  7.  

Share this Question
Share on Google+
3 Replies


ashitpro
Expert 100+
P: 542
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:

Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int n, int num, int &amt)
  2.  
Sep 16 '10 #2

P: 13
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.

Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int n, int num, int &amt)  
  2.     num=amt/n; 
  3.     amt=amt-(num*n); 
  4.  
  5.     return(num); 
  6.  
  7. void PrintResults(int amt)
  8. {   
  9.     int amt;
  10.  
  11.     printf("n50= ", MakeChange(amt, 50)); 
  12.     printf("n20= ", MakeChange(amt, 20)); 
  13.     printf("n10= ", MakeChange(amt, 10)); 
  14.     printf("n5= ", MakeChange(amt, 5)); 
  15.  
  16.     return;
  17. }  
  18. int main() 
  19.     int amt; 
  20.  
  21.     GetAmt(); 
  22.     ChkAmt();   
  23.     PrintResults();
  24.  
  25.  
  26.     return(0); 
  27. }     
  28.  
the error msg i'm getting now is too few parameters in call to 'MakeChange(int,int,int & in function PrintResults. Thanks once again
Sep 16 '10 #3

ashitpro
Expert 100+
P: 542
Signature says 'MakeChange' method accepts 3 parameters..
Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int n, int num, int &amt)
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. int MakeChange(int &amt,int n)  
  2.      int num;
  3.      num=amt/n; 
  4.      amt=amt-(num*n); 
  5.  
  6.      return(num); 
  7.  
Sep 16 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.