1

I'm learning C++ and I'm trying to convert dollars into cents with a function that has a static variable that accumulates the total with each call. Unfortunately it looks like I created an overflow or underflow situation with my function. Any pointers for this function would be a great help. Here is the code.

#include <iostream>
#include <iomanip>
using namespace std;

void normalizeMoney(float& dollars, int cents = 150);
// This function takes cents as an integer and converts it to dollars
// and cents. The default value for cents is 150 which is converted
// to 1.50 and stored in dollars

int main()
{
   int cents;
   float dollars;

   cout << setprecision(2) << fixed << showpoint;

   cents = 95;
   cout << "\n We will now add 95 cents to our dollar total\n";

   normalizeMoney(dollars, cents);//    Fill in the code to call normalizeMoney to add 95 cents

   cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";



   cout << "\n We will now add 193 cents to our dollar total\n";

   normalizeMoney(dollars, 193);// Fill in the code to call normalizeMoney to add 193 cents

   cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";

   cout << "\n We will now add the default value to our dollar total\n";

   normalizeMoney(dollars);// Fill in the code to call normalizeMoney to add the default value of cents

   cout << "Converting cents to dollars resulted in " << dollars << " dollars\n";

   return 0;
}

//*******************************************************************************
//  normalizeMoney
//
//  task:     This function is given a value in cents. It will convert cents
//            to dollars and cents which is stored in a local variable called
//            total which is sent back to the calling function through the
//            parameter dollars. It will keep a running total of all the money
//            processed in a local static variable called sum.
//
//  data in:  cents which is an integer
//  data out: dollars (which alters the corresponding actual parameter)
//
//*********************************************************************************

void normalizeMoney(float& dollars, int cents)
{
    float total = 0;

    // Fill in the definition of sum as a static local variable
    static float sum = 0.0;

    // Fill in the code to convert cents to dollars
    if (cents >= 100) {
        cents -= 100;
        dollars += 1;
        total = total + dollars;
        sum = static_cast <float> (sum + dollars + (cents / 100));

    }
    else {
        total += (cents / 100);
        static_cast <float> (sum += (cents / 100));
    }
    cout << "We have added another $" << dollars << "   to our total" << endl;
    cout << "Our total so far is    $" << sum << endl;

    cout << "The value of our local variable total is $" << total << endl;
}

The output I'm getting is:

We will now add 95 cents to our dollar total
We have added another $-107374176.00    to our total
Our total so far is     $0.00
The value of our local variable total is $0.00
Converting cents to dollars resulted in -107374176.00 dollars

 We will now add 193 cents to our dollar total
We have added another $-107374176.00    to our total
Our total so far is     $-107374176.00
The value of our local variable total is $-107374176.00
Converting cents to dollars resulted in -107374176.00 dollars

 We will now add the default value to our dollar total
We have added another $-107374176.00    to our total
Our total so far is     $-214748352.00
The value of our local variable total is $-107374176.00
Converting cents to dollars resulted in -107374176.00 dollars
Press any key to continue . . .

If someone can tell me where I messed up I would greatly appreciate it.

Aleon
  • 311
  • 2
  • 10
  • 2
    In main, try to initialize dollars variable to 0 before the first call to normalizeMoney. – coolparadox May 06 '18 at 02:49
  • 2
    A good trick is to avoid floating point fuzziness and the rounding problems it can bring is store and all the math in integers with pennies as the base unit. You have to make the output routine a bit smarter to add in the decimal point, but the rest gets a lot easier. – user4581301 May 06 '18 at 02:52
  • Okay I set dollars to 0 and I got the proper start. I can't get the cents to add into dollars, any suggestions? –  May 06 '18 at 03:14

2 Answers2

2

As far as your original question goes, dollars is never initialized. So whatever value happens to be in memory at the time will be your starting dollar value that your function adds to. But instead of fixing your main, this problem stems from the fact that you do not assign your newly calculated sum to dollars in your function, which would be the expected behaviour according to the function's description.

To answer your additional question in the comments, to convert your cents into dollars all you would do is calculate cents / 100.0f, noting the fact that you divide by a floating point number 100.0f instead of an integer 100, so that your result itself becomes a float instead of an int.

Notes:

While by the looks of it, this is a school assignment of sorts, it's still worth to mention a couple of things:

  1. Don't store money amounts in floating point values.
  2. The function signature/behaviour is borderline insane.

Currently what you are trying to achieve is more along the lines of "normalize these cents AND add them to this value". If you wanted to write a single function that converts your cents into dollars, it would make more sense to write it as

float normalizeMoney(const int cents = 150);

and then used as

dollars += normalizeMoney(95);

forgetting about the completely unwarranted static variable.

Community
  • 1
  • 1
Aleon
  • 311
  • 2
  • 10
  • Thank you. This is a school assignment and it's way too rigid for something so simple I cannot agree any more. I'm glad it's done in addition to learing a new trick. Thanks. –  May 06 '18 at 03:33
0

I believe other comments have answered the question. Here’s some suggestion to other bugs: instead of if (cents >= 100), etc, do just the else part: 193 cents / 100 == 1.93 Besides, this fill fix your problem when more than 199 cents are added (it will only deal with the first dollar).

gunta
  • 71
  • 6
  • Yes the comments were a great help. Do you have any tips as to how I can add the cents into the dollars? I am mixing data types I know. –  May 06 '18 at 03:15