Write a function named moneyAmount which receives four parameter values named quarters, dimes, nickels and pennies and then calculates and returns the total amount of money represented. Demonstrate the function by calling it in a program that asks the user to enter values for the number of quarters, dimes, nickles and pennies, calls the function and then displays the returned value. include comments in the program with your name and the date.

I'v been working on this for three days non stop and I cant figure out why it's returning the wrong value. can someone please help me? My name is Nick.

#include <iostream>
using namespace std;

void money()
{
int quarters,dimes,nickles,pennies;

cout << "Enter the number of quarters: ";
cin >> quarters;
cout << "Enter the number of dimes: ";
cin >> dimes ;
cout << "Enter the number of nickles: ";
cin >> nickles ;
cout << "Enter the number of pennies: ";
cin >> pennies ;
cout << "\n";

cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickles << endl;
cout << "Pennies: " << pennies << endl;
cout << "\n";
return;
}

int main()
{
int quarters,dimes,nickles,pennies;
money();
int cents;`
cents = (quarters * 0.25) + (dimes * 0.10) + (nickles * 0.05) + (pennies * 0.01);

cout << "Your total is: \$"<< cents << endl;

system ("PAUSE");
return 0;
}
Last edited on
First of all, you really should be using code tags. Second, why are you using void if you want to return a value. Void means you don't return anything. You can't have variables of the same name in another function and expect the compiler to think "Oh those are the same". You need to approach this a lot differently. Perhaps review functions a bit.
First:
Initialise your values to 0. Otherwise any data in that block of memory may effect your program, this is standard practice.

Second:
void money() should be a float function (to pass an integer with a decimal point) and should pass back a value.
I recommend doing the input for the money in the main and doing the calculations in the function like they asked you to do in the question, it says to do the input in the program and the calculations in the function.

Third:
You need to pass the values between the main function and the money function. If you don't give the function any data how do you expect it to process it? A function can't see variables in other functions unless it's told to.

Fourth:
You have integer values for everything when they should be floating point which you'll need for any calculation in decimal.

Here's what it should look like
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` `````` #include using namespace std; float moneyAmount(float quarters, float dimes, float nickles, float pennies) { float cents = 0; cents = (quarters * 0.25) + (dimes * 0.10) + (nickles * 0.05) + (pennies * 0.01); return cents; } int main() { float quarters = 0; float dimes = 0; float nickles = 0; float pennies = 0; float cents = 0; cout << "Enter the number of quarters: "; cin >> quarters; cout << "Enter the number of dimes: "; cin >> dimes ; cout << "Enter the number of nickles: "; cin >> nickles ; cout << "Enter the number of pennies: "; cin >> pennies ; cout << "\n"; cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickles << endl; cout << "Pennies: " << pennies << endl; cout << "\n"; cents = moneyAmount(quarters, dimes, nickles, pennies); cout << "Your total is: \$"<< cents << endl; system ("PAUSE"); return 0; } ``````

Last edited on
Topic archived. No new replies allowed.