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.
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.
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.
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¢s in floating point)
----calculate dollars in floating point format
----increment sum with dollar amount
----print current sum
----return dollars
#include <iostream>
usingnamespace 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;
staticfloat 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.
#include <iostream>
usingnamespace 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;
staticfloat sum=0; // Local static variable.
// Accumulate a Running Total.
sum+=dollars;
// Display Sum.
cout << "The Current Sum is: " << sum << endl;
return dollars;
}