Functions

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 #include <iostream>
using namespace std;

//Function prototype
void totamt(float, int, int, int, int);
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 nimber of pennies: ";
	cin >> pennies;


	//Call totamt passing the 4 arguments
	totamt (quarters, dimes, nickels, pennies);

	return 0;
}
	

	float total1;
	total1 += quarters * .25f;
	


void totamt (float total1, float total2, float total3, float total4)
{
	cout << (total1 + total2 + total3 + total4) << endl;

}
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.
Last edited on
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.
closed account (3qX21hU5)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>

using namespace std;

double CalcDollars(const int pennies, const int nickels, const int dimes, const int quarters);

int main()
{
    // Used so there is always 2 digits after the decimal
    cout.precision(2);
    cout << fixed << "$" << CalcDollars(5, 4, 15, 3);
}

double CalcDollars(const int pennies, const int nickels, const int dimes, const int quarters)
{
    double total;

    total += (pennies * .01);
    total += (nickels * .05);
    total += (dimes * .10);
    total += (quarters * .25);

    return total;
}
For all those that helped thank you. Here is what I ended up with:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <iomanip>
using namespace 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;

}
Topic archived. No new replies allowed.