### Cents into Dollars using a Function prototype

My program is almost done, but when i try to convert a number into cents, it does not calculate the cents properly. So if i enter 325, it says i have 3 dollars and 325 cents and my running total is not working. Running total of all money should be processed in the local static variable called sum. Each time NormalizeMoney is called it should print out the sum before returning, but it is not.

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` #include using namespace std; // Function Prototypes. int NormalizeMoney(int); int main() { int cents, dollars; cout << "How many cents do you have: "; cin >> cents; dollars = NormalizeMoney(cents); cout << "You have " << dollars << " dollars and " << cents << " cents!\n"; return 0; } int NormalizeMoney (int cents) { int dollars; static int sum=0; // Local static variable. // Convert cents to dollars and cents. dollars = cents/100; cents = dollars - cents; // Accumulate a Running Total. for (int i = 1;i <= sum; i++) { cout << "The Sum is : " << sum << endl; sum+=dollars; } return dollars; }``````
Last edited on
`static int sum=0; // Local static variable. `

sum is initialized to 0. The counter variable i in the for loop starts at 1, so it will never be less than or equal to sum to perform the code in the for loop.

`for (int i = 1;i <= sum; i++)`

edit:

`cout << "You have " << dollars << " dollars and " << cents << " cents!\n";`

You're outputting the original cents figure that the person entered here, not a converted amount after determining the # of whole dollars.

 ``123`` ``````// Convert cents to dollars and cents. dollars = cents/100; //if 325 entered, dollars = 325/100 = 3 cents = dollars - cents; //if 325 entered, cents = 3 - 325 ``````

I think you're trying to convert 325 cents into 3 dollars and 25 cents?

 ``123`` ``````// Convert cents to dollars and cents. dollars = cents/100; //if 325 entered, dollars = 325/100 = 3 cents = cents - (dollars*100) // if 325 entered, cents = 325 - (3*100) = 25 ``````

What is the sum supposed to do?
Last edited on
Write a program that takes cents as an integer and converts it to dollars and cents.
The conversion should be done in a function called NormalizeMoney. This function is given a value in cents. It will convert cents to dollars and cents, which should be stored in a local variable called dollars which is returned to the calling function. NormalizeMoney will keep a running total of all the money processed in a local static variable called sum. Each time NormalizeMoney is called it should print out the sum before returning.
Main function should prompt the user for cents and keep prompting till the user is done. For each cent value received, main function should print out the dollar equivalent.
Last edited on
OK - so if dollars needs to hold a figure like 3.25 it needs to be a float or double type instead of integer. Do you really need to split out the dollars and cents separately or can you just print \$3.25? They're just asking you to return a single value.

while user wants to enter cents amount
--enter cents
--call conversion function
--print converted amount
--enter more cents?

conversion function (takes cents, returns dollar&cents in floating point)
----calculate dollars in floating point format
----increment sum with dollar amount
----print current sum
----return dollars
I am almost done with my code but I am getting an error saying: on line 11 Variable 'dollars' is set but not used. How do i fix it?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include using namespace std; // Function Prototypes. int NormalizeMoney(int); int main() { int dollars; // Number of dollars in change int cents=0; // Amount of change int amount=1; // Amount counter cout << "Enter the amount of cents you wish to calculate.\n"; cout << "Enter 0 when finished.\n"; cout << "Enter amount of cents would you like to calculate: "; cin >> cents; dollars = NormalizeMoney(cents); while (cents!=0) { amount+=cents; amount++; cout << "Enter amount of cents would you like to calculate: "; cin >> cents; dollars = NormalizeMoney(cents); } return 0; } int NormalizeMoney (int cents) { double dollars=0; // Convert cents to dollars and cents. dollars = ((float)cents/100); cents = cents - (dollars*100); cout << "The Converted Amount is \$" << dollars << endl; static float sum=0; // Local static variable. // Accumulate a Running Total. sum+=dollars; // Display Sum. cout << "The Current Sum is: " << sum << endl; return dollars; }``````
I literally copied and pasted your code and it ran fine for me. Do you receive the error after you input a value? I might suggest restarting your compiler or maybe copying the code and pasting it in a fresh project.
I fixed it, I took out dollars in main and put this instead:
`amount = NormalizeMoney(cents);`

Works wonderfully with no errors now :)
You shouldn't get that error on line 11 considering that doesn't make sense.

` int amount=1; // Amount counter ` How can this be your error??

Anyways it runs fine. (Also wasn't there another thread with this post a few seconds ago but then deleted as I tried to post? :P)

http://ideone.com/rJsJno
Yeah lol, but i solved my own problem. My sense of satisfaction is unmeasurable right now :)

My completed beautiful program:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include using namespace std; // Function Prototypes. int NormalizeMoney(int); int main() { int cents=0; // Amount of change int amount=1; // Amount counter cout << "Enter the amount of cents you wish to calculate.\n"; cout << "Enter 0 when finished.\n"; cout << "Enter amount of cents would you like to calculate: "; cin >> cents; amount = NormalizeMoney(cents); while (cents!=0) { amount+=cents; amount++; cout << "Enter amount of cents would you like to calculate: "; cin >> cents; amount = NormalizeMoney(cents); } return 0; } int NormalizeMoney (int cents) { double dollars=0; // Convert cents to dollars and cents. dollars = ((float)cents/100); cents = cents - (dollars*100); cout << "The Converted Amount is \$" << dollars << endl; static float sum=0; // Local static variable. // Accumulate a Running Total. sum+=dollars; // Display Sum. cout << "The Current Sum is: " << sum << endl; return dollars; }``````
Last edited on
Congrats!

 main function should print out the dollar equivalent

You're actually printing in the normalizemoney function - just an FYI in case you think the prof will mind.
Topic archived. No new replies allowed.