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. Each time NormalizeMoney is called it should print out the sum before returning, but it is not.

#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;
}

Edited 2 Years Ago by blazemadej

Running total of all money should be processed in the local static variable called sum.

A few things I noticed.

To use an object by reference instead of by value use the addrees of(&) operator.

To strip the dollar portion from the total cents use the modulo(%) operator

To keep a running total, there's no need for a loop, just keep incrementing it each time the function is called.

See if this helps:

#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 = cents % 100;
    // Accumulate a Running Total.
    sum+=dollars;
    cout << "The Sum is : " << sum << endl;
    return dollars;
}

One thing to keep in mind. The way you have it set up, only the dollar portion is added to the sum. One way to keep a running total of everything is to add the cents to the sum before you call NormalizeMoney, then call NormalizeMoney to display the sum.

Edited 2 Years Ago by tinstaafl

I believe I am supposed to accumulate a running total, so more than just one number should be entered and then all accumulated into a sum. Is there a way to not use the (&) operator? I havent gotten that far yet.

One way is to not calculate the cents in the function, just display cents % 100 to show the cents. This way you don't have to use the & operator since you are only using the value.

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?

#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;
}

Edited 2 Years Ago by blazemadej

Cancel that, I fixed it :) Thank you for all your help. You definitely steered me in the right direction!

So ... are you saying that you need to keep asking for more cents to be input in a loop ... until the user indicates that they are done ?

And each time, show the dollar and the cents totals so far?

Since you do not wish to use pass by reference ...
you could return a struct to hold dollars and cents
and update the totals outside the function each loop,
as per the following C++ struct example ...

// centsToDollars2.cpp //

#include <iostream>

using namespace std;

struct Change
{
    int dollars;
    int cents;

    // default constructor sets to zero (optional, since not needed here)
    Change() : dollars(0), cents(0) {}

} ; // don't forget this ; to indicate the struct is done!


// Function Prototypes.
Change NormalizeMoney( int );



int main()
{
    int tot_dollars= 0; // totals initialed to 0
    int tot_cents=0;    // totals ...
    int count=0;        // totals ...

    int cents = -1;
    for( ; ; ) // an example of a C/C++ forever loop ... until break
    {
        cout << "(Enter 0 when finished.)\n";
        cout << "Enter the amount of cents you want to add: ";

        if( cin >> cents && cents >= 0 &&  cin.get() == '\n' )
        {
            if( !cents ) break;

            // OK ... we have cents ... so can ...

            Change tmp = NormalizeMoney( cents );

            tot_dollars += tmp.dollars;
            tot_cents += tmp.cents;

            ++ count;

            cout << "After " << count << " entries, you have accumlated "
                 <<  "$" << tot_dollars << "." << tot_cents << endl;
        }
        else
        {
            cout << "\nError!  Only VALID positive integer values accepted.\n";
            cin.clear(); // clear all error flags that were set ..
        }

        cin.sync(); // 'flush' the cin stream ...
        cout << endl;
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.sync();
    cin.get();
}


Change NormalizeMoney (int cents)
{
    Change cng;

    // Convert cents to dollars and cents.
    cng.dollars = cents / 100; // integer division
    cng.cents = cents % 100;   // division mod 100

    cout << "The Converted Amount is $"
         << cng.dollars << "." << cng.cents << endl;

    return cng;
}

Edited 2 Years Ago by David W: fixed comments

This question has already been answered. Start a new discussion instead.