Hellp programmers! I'm trying to create a program that displays the impact of changing the rate of compound intrest using the historical example of Manhattan's aquisition as an example. The code is below:

``````int main()
{
double amount=0; //amount on deposit at the end of each year
double principal=24.00; //initialize principal

cout << "Legend has it that in 1626, Peter Minuit purchased"<<endl;
cout << "Manhattan Island for \$24.00 in a barter."<<endl;
cout << "\nDid he make a good investment?"<<endl;
cout << "Below is a table of his return on the investment"<<endl;
cout << "in 2013 (387 years' difference) with intrest"<<endl;
cout << "rates varying from five to ten percent per year."<<endl;
cout << "\nCheck it out for yourself.\n\n"<<endl;
cout << "Rate" << setw(40) << "Return on Investment (\$)" << endl;

//set floating point number format
cout << fixed << setprecision(2);

//calculate amount
for (float rate=.5; .5<=.10;rate+=.1)
{
//calculate new amount for specified year
amount=principal*pow(1.0+rate,387);
cout << setw(4) << rate << setw(40) << amount << endl;

}

return 0;
}
``````

However, when I run it, I get the following:

``````Legend has it that in 1626, Peter Minuit purchased
Manhattan Island for \$24.00 in a barter.

Did he make a good investment?
Below is a table of his return on the investment
in 2013 (387 years' difference) with intrest
rates varying from five to ten percent per year.

Check it out for yourself.

Rate                Return on Investment (\$)
``````

This means that the program dosn't get into the loop that varies the intrest rate from five to ten percent per year. Why isn't the loop being activated?

This line looks odd:

``````for (float rate=.5; .5<=.10;rate+=.1)
``````

Specifically, this part:

``````.5<=.10
``````

This will always be false, so the body of the loop never executes.

You probably meant something like this:

``for (float rate = 0.5; rate <= 1.0; …``

Aha! I found an example of how you can customize a locale.

A quick test:

``````#include <iostream>
#include <iomanip>

using namespace std;

struct CustomSeparator : numpunct<char>
{
char do_thousands_sep() const …``````

## All 7 Replies

This line looks odd:

``````for (float rate=.5; .5<=.10;rate+=.1)
``````

Specifically, this part:

``````.5<=.10
``````

This will always be false, so the body of the loop never executes.

You probably meant something like this:

``````for (float rate = 0.5; rate <= 1.0; rate += 0.1)
``````

It probably still won't do what you want. You probably want a start value of .05(5 percent), a limit of .10(10 percent) and an increment of .01(1 percent), `for (float rate=.05; rate<=.10;rate+=.01)`

Here's my new code- one that goes into the loop (tested):
//

include <iostream>
include <iomanip> //manipulate output stream
include <cmath> //standard math library

using namespace std;

``````int main()
{
double amount=0; //amount on deposit at the end of each year
double principal=24.00; //initialize principal

cout << "Legend has it that in 1626, Peter Minuit purchased"<<endl;
cout << "Manhattan Island for \$24.00 in a barter."<<endl;
cout << "\nDid he make a good investment?"<<endl;
cout << "Below is a table of his return on the investment"<<endl;
cout << "in 2013 (387 years' difference) with intrest"<<endl;
cout << "rates varying from five to ten percent per year."<<endl;
cout << "\nCheck it out for yourself.\n\n"<<endl;
cout << "Rate" << setw(40) << "Return on Investment (\$)" << endl;

//set floating point number format
cout << fixed << setprecision(2);

//calculate amount
for (float rate=.05; rate<=.1;rate+=.01)
{
//calculate new amount for specified year
amount=principal*pow(1.0+rate,387);
cout << setw(4) << rate << setw(40) << amount << endl;

}

return 0;
}
``````

Here's my output:

``````Legend has it that in 1626, Peter Minuit purchased
Manhattan Island for \$24.00 in a barter.

Did he make a good investment?
Below is a table of his return on the investment
in 2013 (387 years' difference) with intrest
rates varying from five to ten percent per year.

Check it out for yourself.

Rate                Return on Investment (\$)
0.05                           3806011542.49
0.06                         149135651993.65
0.07                        5645900923416.71
0.08                      206635215216007.47
0.09                     7315859348625544.00
0.10                   250714520349508480.00
``````

As you can see, the return on the investment is extremly large and is almost unreadable due to the lack of commas. Is there a way to include commas in the `Return on Investment` output?

There isnt a way to add commas to the output natively but you can wrtie a function to do it for you.

Well, your mileage may vary... I can't actually get it to format numbers like that, which appears to be because MinGW apparently doesn't do locales much at all.

Aha! I found an example of how you can customize a locale.

A quick test:

``````#include <iostream>
#include <iomanip>

using namespace std;

struct CustomSeparator : numpunct<char>
{
char do_thousands_sep() const { return ','; }
string do_grouping() const { return "\3"; }
};

#define AMOUNT 250714520349508480.0

int main()
{
cout << fixed << setprecision(2);
cout << "Normal: " << AMOUNT << endl;
cout.imbue(locale(locale(), new CustomSeparator));
cout << "Custom: " << AMOUNT << endl;
}
``````

For me, this displays the following:

``````Normal: 250714520349508480.00
Custom: 250,714,520,349,508,480.00
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.