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.

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 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.



1
2
3
// 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?

1
2
3
// 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?

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
39
40
41
42
43
44
45
46
47
#include <iostream>
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:

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
39
40
41
42
43
44
45
46
#include <iostream>
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.