Write a function named totamt() that uses four parameters named quarters, dimes, nickels, and pennies, which represent the number of each of these coins in a piggybank. The function should determine the dollar value of the coins and return the calculated value. Include the function in a program. Make sure the function is called from main() and returns a value correctly. Have main() use a cout statement to display the returned value. Test your function by entering different amounts of each coin.
This is what I have so far. Any help would be appreciated as I continue to try and figure this out.
Well, you already have almost all of it down. Just multiply the number of quarters by .25, the number of dimes by .10, the number of nickels by .05, and the number of pennies by .01. You can do that right in your totamt function, with some clever use of parenthesis.
pretty much what ispil said but what is the point of line 27, line 28, line 5 the first float, and line 32 you are declaring them all floats when in the prototype you declared 4 of them as ints and you don't need that first float once again.
oh and by the way floats aren't really good to use if you want something with a decimal I would suggest using a double they twice as percise.
1 2
void totamt(double, double, double, double);
void totamt(double a, double b, double c, double d) { a *= .25; /*ect...*/ }
I would suggest using them as doubles though over ints since it is after all money which will not always be an exact dollar.
Actually... you don't even need to use double here. The values for pennies, nickels, dimes and quarters are all integers (can't have fractional amounts of coins, now can you?). Therefore, everything you are passing to the function totamt is an integer as well. If you multiply an integer by a decimal, you don't need it to be double or float for it to work- it'll work just fine with it all being int values. Especially since the totamt function simply displays the value, versus returning it. So... yeah. Just make totamt take 4 int values. No need for float or double at all here.
You are almost there just need to do a few things.
1) Multiply each parameter by their respective values. By this I mean for pennies you would multiply it by .01, nickels .05, ect.
2) Redo your naming conventions. As of right now it is impossible to tell which parameter is which and will cause you major problems later.
3) Make sure when you are printing the total value to use fixed precision so that there will always be 2 digits after the decimal point (IE Money Notation).
Otherwise looks good. Here is a example also to help you work through it.
#include <iostream>
#include <iomanip>
usingnamespace std;
void totamt(int,int,int,int); //Prototype
int main ()
{
int quarters;
int dimes;
int nickels;
int pennies;
//Get the number of coins
cout << "Enter the number of quarters: ";
cin >> quarters ;
cout << "Enter the number of dimes: ";
cin >> dimes ;
cout << "Enter the number of nickels: ";
cin >> nickels ;
cout << "Enter the number of pennies: ";
cin >> pennies;
// Used so there is always 2 digits after the decimal
cout.precision(2);
cout << "The total in the piggybank is : " << fixed << "$";
totamt ( quarters, dimes, nickels, pennies );
system ("pause");
return 0;
}
void totamt (int quarters1, int dimes1, int nickels1 , int pennies1) // calling the Function
{
cout << ( quarters1 * .25f + dimes1 * .10f + nickels1 * .05f + pennies1 * .01f) << endl;
}