It looks like you're new here. If you want to get involved, click one of these buttons!

- 140.8K All Categories
- 103.6K Programming Languages
- 6.4K Assembler Developer
- 401 Assembly Code Share
- 239 Getting started in assembly
- 4.6K x86 Assembly
- 1.9K Basic
- 97 Qbasic
- 39.9K C and C++
- 5.6K Beginner C/C++
- 330 C/C++ on Linux/Unix
- 450 C/C++ Windows API
- 522 C++ Builder
- 253 C++ Game Development
- 3.3K C++ MFC
- 103 C++.NET
- 404 Visual C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 334 Advanced Delphi
- 360 Delphi beginners
- 4 Haskell
- 9.7K Java
- 56 Enterprise JavaBeans
- 1.3K Java Beginners
- 304 Java Server Pages
- 4.1K Pascal
- 1.3K Perl
- 11 Perl 6
- 2K PHP
- 546 Python
- 37 Ruby
- 4.4K VB.NET
- 258 Advanced VB.Net
- 1.6K VBA
- 20.9K Visual Basic
- 767 Access databases and VB
- 831 Advance Visual Basic
- 1.2K Beginner VB
- 2.6K Game programming
- 315 Console programming
- 90 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 279 3D Graphics
- 129 DirectX
- 125 OpenGL
- 740 Computer Hardware
- 9 Cooling & Overclocking
- 3.4K Database & SQL
- 1.1K Access
- 91 ADO Programming
- 288 MySQL
- 358 Oracle
- 440 SQL-Server
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 25 DirectSound
- 257 XML Development
- 3.3K Classifieds
- 200 Co-operative Projects
- 198 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 603 Jobs Wanted
- 209 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 22 .NET WEB-Services
- 129 .NET WinForms
- 14 .NET XML
- 50 ADO.NET
- 143 C# & VB.NET School Support
- 3.4K Miscellaneous
- 4 Join the Team
- 354 Comments on this site
- 69 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 621 Off topic board
- 200 Mobile & Wireless
- 72 Android
- 126 Palm Pilot
- 338 Multimedia
- 154 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 27 Cloud Computing
- 1 Witsbits Go Cloud
- 53 FreeBSD
- 1.7K LINUX programming
- 1 Awk scripting
- 332 Linux Support
- 0 Sed scripting
- 370 MS-DOS
- 0 Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 177 COM/DCOM
- 61 Networking And Security
- 17 Windows 2003 Server
- 6 Windows Vista
- 176 Windows XP
- 941 Software Development
- 417 Algorithms
- 68 Object Orientation
- 24 RUP & UML
- 92 Project Management
- 95 Quality & Testing
- 268 Security
- 63 Evil Scripting
- 81 Hacking
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 4 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 131 Mobile Internet & Messaging
- 211 Wireless development
- 2.2K JavaScript
- 37 JQuery
- 304 WEB Servers
- 153 Apache
- 79 IIS
- 150 WEB-Services / SOAP

## Comments

using namespace std;

int main()

{ cout << "Please enter an integer >= 1000 : ";

double numbers;

cin >> numbers;

cout << numbers << endl;

return 0;

}

:

: Please enter an integer >= 1000 : 23456

:

: 23,456

:

: Can anyone figure this out?

Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.

Cheers,

Eric

#include

using namespace std;

int main()

{

cout << "Please enter an integer >= 1000 (ex:'23 456'): ";

string number1;

string comma;

string number2;

cin >> number1 >> comma >> number2;

comma = ',';

string final = number1.substr(0, 3) + comma.substr(0, 1) + number2.substr(3, 6);

cout << "The interger you entered is: " << final << endl;

return 0;

}

this is what i have so far but i dont know how to produce the last numbers? This is for my programming class and been working on this for days can someone help?

Think about this, find out how many digits would be at the very left of your number, e.g.:

1,234 has one

12,345 has two

123,456 has three

Okay, now you have that. Now, say for example you have two digits on the end.

Write out the first two digits of your number and then add a comma. Something like,

cout << Number.substr (0,2);

Then output a comma,

cout << ",";

and then output three more digits and a comma, and continue doing that until you reach the end of the string. You don't even need to know how many digits are actually in your string to begin with, only how many are on the very left.

-Xotor-

i did what you said but it wont show the rest of the #'s.

ex: user inputs '1000'

then the screen shows '10,'

im really lost here.i need for the program to place the comma where is needed. 1,000 or 100,000

ill really appreciate it if you can teach me this one thing...im stuck!

-Michelle

: :

: : Please enter an integer >= 1000 : 23456

: :

: : 23,456

: :

: : Can anyone figure this out?

:

: Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.

:

: Cheers,

: Eric

:

:

I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

: : :

: : : Please enter an integer >= 1000 : 23456

: : :

: : : 23,456

: : :

: : : Can anyone figure this out?

: :

: : Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.

: :

: : Cheers,

: : Eric

: :

: :

: I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

Don't get too stressed, it's not as easy as it might first appear. The easiest way to do it is to output each character one at a time, inserting commas as needed. Since you're using std::string, to access individual characters you can use operator[], or using string::iterator. As in:

[code]for (int = 0; i < s.length(); ++i)

putchar (s[i]);[/code]or[code]for (string::iterator it = s.begin(); it != s.end(); ++it)

putchar (*it);[/code]or you could use pointers, but basic_string::iterator really is just char* anyway, and the iterator is cleaner.

Anyway, the real trick is knowing when to insert a comma. We need to know when we're at the end of a "three digit group". Well we know we need to insert commas after the digits 3,6,9,12,etc. Multiples of three, right? How do we find out in code if a number is a multiple of three? Just divide by 3; if there's no remainder, it's an even multiple. Of course, anytime we need the remainder of an integer division, we know we're going to be using the mod operator (%).

Let's say we have the number:

[code]84903928[/code]Let's number the digits, right to left:

[code]the number : 84903928

digits index: 87654321[/code]Digit 1: 1%3==1

Digit 2: 2%3==2

Digit 3: 3%3==0 .. end of a group of 3 digits, need a comma here

Digit 4: 4%3==1

Digit 5: 5%3==2

Digit 6: 6%3==0 .. end of a group of 3 digits, need a comma here

Digit 7: 7%3==1

Digit 8: 8%3==2

So now we have mathematical way of determining if we're at a digit that needs a comma.

Small problem. In our 'legend' above we number the digits [N..1] (with the 'first' digit on the right). However, in C++ you index digits in a string [0..N-1] (with the 'first' digit on the left). So you'll have to reconcile that in your program. The easiest way is to loop from N to 1, as in "for (int i = length; i >= 1; --i)", or something like that. If (i % 3 == 0), we know we need to insert a comma.

Hopeful that helps and doesn't just confuse the crap out of you.

Good luck,

Eric

: ex: user inputs '1000'

: then the screen shows '10,'

:

: im really lost here.i need for the program to place the comma where is needed. 1,000 or 100,000

:

: ill really appreciate it if you can teach me this one thing...im stuck!

Have you determined how many characters on the left you need?

You've need to have your program count the number of digits there are in the number, and then perform a modulo (%) of three on that number to get how many you need to print before simply printing three at a time.

Pseudocode:

NumDigits = Number of digits in number (10000 would have five).

EndDigits = NumDigits % 3;

If EndDigits = 0, EndDigits = 3;

Output -EndDigit- amount of characters from the Numberstring.

While there are still digits left to be printed:

print a comma

Read three more digits and print those out.

Repeat

If you're wondering how to read more digits, use your substr function to grab digits, and then move to the end of those digits:

Value = Number.substr (i, 3);

i += 3;

-Xotor-

#include

#include

#include

using namespace std;

int main()

{

cout << "Please enter an integer >= 1000: ";

int comma;

int Digit1;

int Digit2;

int Digit3;

int Digit4;

int Digit5;

int Digit6;

cin >> Digit1 >> Digit2 >> Digit3 >> Digit4 >> Digit5 >> Digit6;

Digit1 = 1%3==1;

Digit2 = 2%3==2;

Digit3 = 3%3==0;

comma = ',';

Digit4 = 4%3==1;

Digit5 = 5%3==2;

Digit6 = 6%3==0;

for (int i = length; i >= 1; --i)

return 0;

}

Well, Xotor gave you a better "road map" than I did. My code was probably a bit more theoretical I guess, trying to get you to understand how the mod operator would work for this situation, it was not really meant as example code. You don't want to address each digit individually like you're doing, you need to do this in a loop so that your program can handle number that are arbitrarily long.

Xotor has already given you pseudocode. He's some psuedocode for another approach:

[code]digit = length of string - 1

c = first char of string

while digit is greater than 0

output c

c = next char of string

if (digit != 0 and digit mod 3 == 0)

output ','[/code]

Cheers,

Eric

The big commented block was a bunch of code I abandoned since it was getting out of hand but left it in this post so you can see where I was trying to go with it.

#include

#include

using namespace std;

int main(){

string num;

int numlength;

int numpos = 0; //number position

string newnum = "";

cout << "Enter a number greater or equal to 1,000: ";

cin >> num;

numlength = num.length();

while (numlength > 0) {

if ((((num.length() - numpos) % 3) == 1) && numlength > 3) {

newnum = newnum + num.substr(numpos,1) + ",";

}

else{

newnum = newnum+num.substr(numpos,1);

}

numpos++;

--numlength;

}

cout << endl << newnum << endl;

/*

THIS DOES NOT WORK

string number;

int numlength;

int numposition = 0;

cout << "Enter a number greater or equal to 1,000: ";

cin >> number;

numlength = number.length();

while (numlength >= 4) {

if ((number.length() % 3) == 1) { // comma after 1 spot

if ((numlength == number.length()) && (numlength > 4)){

cout << "mod one";

cout << number.substr(numposition, 1) << "," << number.substr(numposition+1,number.length()-(numlength-2));

}

else{

cout << number.substr(numposition, 1) << "," << number.substr(numposition+1,number.length()-(numlength-3));

}

}

if ((number.length() % 3) == 2) { // comma after 2 spots

if ((numlength == number.length()) && (numlength > 4)){

cout << "mod 2";

cout << number.substr(numposition, 2) << "," << number.substr(numposition+2,number.length()-(numlength-3));

}

else{

cout << number.substr(numposition, 2) << "," << number.substr(numposition+1,number.length()-(numlength-3));

}

}

if ((number.length() % 3) == 0) { // comma after 3 spots

if ((numlength == number.length()) && (numlength > 4)){

cout << "mod 0";

cout << number.substr(numposition, 1) << "," << number.substr(numposition+1,number.length()-(numlength-2));

}

else{

cout << number.substr(numposition, 1) << "," << number.substr(numposition+1,number.length()-(numlength-3));

}

}

numlength = numlength-3;

numposition = numposition +3;

}

cout << endl;*/

return 0;

}

#include

#include

#include

#include //Need this for comma fixer loop.

using namespace std;

int main ()

{

double investment;

double intrestRATE;

int months;

double monthlyinterest;

double interestEarned;

double investBalence= investment;

int count = 0;

string investmentf;

string investBal;

cout << "Enter the amount of the investment."; //Ask the user for the amount of the investment.

cout << "The minimum investment is $10,000.00.

"; //Get a minimum investment amount of $10,000 from the user.

do

{

cin >> investment;

ostringstream stm ; // Note to self: thins is so useful!

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

stm << fixed << setprecision(2) << showpoint << investment;

string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investmentf = str;

}

if (investment < 10000.00)

{

cout << "

Error, the minimum investment is $10,000.00.

";

cout << "Enter the amount of the investment: ";

}

}while(investment < 10000);

cout << "Enter the annual interest rate: "; //Ask the user for the annual interest rate.

do

{

cin >> intrestRATE;

if (intrestRATE < 0)

{

cout << "

Error, the interest rate should be positive."; //Get a non-negative annual interest rate from the user.

cout << "

Enter the annual interest rate: ";

}

}while(intrestRATE < 0);

cout << "Enter the number of months of the investment: "; //Ask the user for the months of the investment.

do

{

cin >> months;

if (months < 0)

{

cout << "

Error, the number of months should be positive."; //Get a non-negative number of months from the user.

cout << "

Enter the number of months of the investment: ";

}

}while(months < 0);

monthlyinterest = intrestRATE / 12 / 100; //Calculate the monthly interest rate-

//by dividing the annual interest rate-

//by 12 and then dividing the resulting value-

//by 100.

for( int count = 1; count <= months; count++) //Repeat for the number of months of the investment.

{

interestEarned = monthlyinterest * investment; //Calculate the interest earned by multiplying the-

//investment balance by the monthly interest rate.

investment += interestEarned; //Increase the value in the investment balance by the interest earned.

investBalence = investment; //Save the initial investment amount in-

//investment balance.

}

ostringstream stm ; // Note to self: thins is so useful!

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

stm << fixed << setprecision(2) << showpoint << investBalence;

string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investBal = str;

}

cout << "After " << months << " months, your investment of $" << //Display a message indicating the future value-

investmentf << " will be worth $" << investBal << "." << // of the investment stored in investment balance.

endl;

return 0;

}

/* Problem/programs Description.

An investor has the opportunity to invest a minimum of $10,000 in an investment.

We want to write a program to help him/her determine the future value of the investment.

The program will ask the user to enter the investment amount, the annual interest rate, and

the number of months of the investment to be used as inputs in calculating the future value of the investment.

Write the source code for a program that implements the pseudocode algorithm given below.

Save the source code in a file named Lab6_Ex3.cpp.

Ask the user for the amount of the investment

Get a minimum investment amount of $10,000 from the user

Ask the user for the annual interest rate

Get a non-negative annual interest rate from the user

Ask the user for the months of the investment

Get a non-negative number of months from the user

Calculate the monthly interest rate by dividing the annual interest rate by 12 and then dividing the resulting value by 100

Save the initial investment amount in investment balance

Repeat for the number of months of the investment

Calculate the interest earned by multiplying the investment balance by the monthly interest rate

Increase the value in the investment balance by the interest earned

Display a message indicating the future value of the investment stored in investment balance

Notice that input validation is required on the investment amount, the annual interest rate, and the number of months.

*/

#include

#include

#include

#include //Need this for comma fixer loop.

using namespace std;

int main ()

{

double investment;

double intrestRATE;

int months;

double monthlyinterest;

double interestEarned;

double investBalence= investment;

int count = 0;

string investmentf;

string investBal;

cout << "Enter the amount of the investment.";

//Ask the user for the amount of the investment.

cout << "The minimum investment is $10,000.00.

";

//Get a minimum investment amount of $10,000 from the user.

do{

cin >> investment;

ostringstream stm; // For comma problem.

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//below code fixes comma problem.

stm << fixed << setprecision(2) << showpoint << investment;

string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investmentf = str;

}

if (investment < 10000.00)

{

cout << "

Error, the minimum investment is $10,000.00.

";

cout << "Enter the amount of the investment: ";

}

}while(investment < 10000);

cout << "Enter the annual interest rate:";

//Ask the user for the annual interest rate.

do{

cin >> intrestRATE;

if (intrestRATE < 0)

{

cout << "

Error, the interest rate should be positive."; //Get a non-negative annual interest rate from the user.

cout << "

Enter the annual interest rate: ";

}

}while(intrestRATE < 0);

cout << "Enter the number of months of the investment: ";

//Ask the user for the months of the investment.

do{

cin >> months;

if (months < 0)

{

cout << "

Error, the number of months should be positive.";

//Get a non-negative number of months from the user.

cout << "

Enter the number of months of the investment: ";

}

}while(months < 0);

monthlyinterest = intrestRATE / 12 / 100; //Calculate the monthly interest rate-

//by dividing the annual interest rate-

//by 12 and then dividing the resulting value-

//by 100.

for( int count = 1; count <= months; count++) //Repeat for the number of months of the investment.

{

interestEarned = monthlyinterest * investment; //Calculate the interest earned by multiplying the-

//investment balance by the monthly interest rate.

investment += interestEarned; //Increase the value in the investment balance by the interest earned.

investBalence = investment;

//Save the initial investment amount in- //investmentbalance.

}

ostringstream stm ; // Note to self: this is so useful!

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//solves comma problem.

stm << fixed << setprecision(2) << showpoint << investBalence; string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investBal = str;

}

cout << "After " << months << " months, your investment of $" <<

investmentf << " will be worth $" << investBal << "." << endl;

//Display a message indicating the future value-

// of the investment stored in investment balance.

return 0;

}

/* Problem/programs Description.

An investor has the opportunity to invest a minimum of $10,000 in an investment.

We want to write a program to help him/her determine the future value of the investment.

The program will ask the user to enter the investment amount, the annual interest rate, and

the number of months of the investment to be used as inputs in calculating the future value of the investment.

Write the source code for a program that implements the pseudocode algorithm given below.

Save the source code in a file named Lab6_Ex3.cpp.

Ask the user for the amount of the investment

Get a minimum investment amount of $10,000 from the user

Ask the user for the annual interest rate

Get a non-negative annual interest rate from the user

Ask the user for the months of the investment

Get a non-negative number of months from the user

Calculate the monthly interest rate by dividing the annual interest rate by 12 and then dividing the resulting value by 100

Save the initial investment amount in investment balance

Repeat for the number of months of the investment

Calculate the interest earned by multiplying the investment balance by the monthly interest rate

Increase the value in the investment balance by the interest earned

Display a message indicating the future value of the investment stored in investment balance

Notice that input validation is required on the investment amount, the annual interest rate, and the number of months.

*/

#include

#include

#include

#include //Need this for comma fixer loop.

using namespace std;

int main ()

{

double investment;

double intrestRATE;

int months;

double monthlyinterest;

double interestEarned;

double investBalence= investment;

int count = 0;

string investmentf;

string investBal;

cout << "Enter the amount of the investment.";

//Ask the user for the amount of the investment.

cout << "The minimum investment is $10,000.00.

";

//Get a minimum investment amount of $10,000 from the user.

do{

cin >> investment;

ostringstream stm; // For comma problem.

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//below code fixes comma problem.

stm << fixed << setprecision(2) << showpoint << investment;

string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investmentf = str;

}

if (investment < 10000.00)

{

cout << "

Error, the minimum investment is $10,000.00.

";

cout << "Enter the amount of the investment: ";

}

}while(investment < 10000);

cout << "Enter the annual interest rate:";

//Ask the user for the annual interest rate.

do{

cin >> intrestRATE;

if (intrestRATE < 0)

{

cout << "

Error, the interest rate should be positive."; //Get a non-negative annual interest rate from the user.

cout << "

Enter the annual interest rate: ";

}

}while(intrestRATE < 0);

cout << "Enter the number of months of the investment: ";

//Ask the user for the months of the investment.

do{

cin >> months;

if (months < 0)

{

cout << "

Error, the number of months should be positive.";

//Get a non-negative number of months from the user.

cout << "

Enter the number of months of the investment: ";

}

}while(months < 0);

monthlyinterest = intrestRATE / 12 / 100; //Calculate the monthly interest rate-

//by dividing the annual interest rate-

//by 12 and then dividing the resulting value-

//by 100.

for( int count = 1; count <= months; count++) //Repeat for the number of months of the investment.

{

interestEarned = monthlyinterest * investment; //Calculate the interest earned by multiplying the-

//investment balance by the monthly interest rate.

investment += interestEarned; //Increase the value in the investment balance by the interest earned.

investBalence = investment;

//Save the initial investment amount in- //investmentbalance.

}

ostringstream stm ; // Note to self: this is so useful!

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//solves comma problem.

stm << fixed << setprecision(2) << showpoint << investBalence; string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investBal = str;

}

cout << "After " << months << " months, your investment of $" <<

investmentf << " will be worth $" << investBal << "." << endl;

//Display a message indicating the future value-

// of the investment stored in investment balance.

return 0;

}

/* Problem/programs Description.

An investor has the opportunity to invest a minimum of $10,000 in an investment.

We want to write a program to help him/her determine the future value of the investment.

The program will ask the user to enter the investment amount, the annual interest rate, and

the number of months of the investment to be used as inputs in calculating the future value of the investment.

Write the source code for a program that implements the pseudocode algorithm given below.

Save the source code in a file named Lab6_Ex3.cpp.

Ask the user for the amount of the investment

Get a minimum investment amount of $10,000 from the user

Ask the user for the annual interest rate

Get a non-negative annual interest rate from the user

Ask the user for the months of the investment

Get a non-negative number of months from the user

Calculate the monthly interest rate by dividing the annual interest rate by 12 and then dividing the resulting value by 100

Save the initial investment amount in investment balance

Repeat for the number of months of the investment

Calculate the interest earned by multiplying the investment balance by the monthly interest rate

Increase the value in the investment balance by the interest earned

Display a message indicating the future value of the investment stored in investment balance

Notice that input validation is required on the investment amount, the annual interest rate, and the number of months.

*/

#include

#include

#include

#include //Need this for comma fixer loop.

using namespace std;

int main ()

{

double investment;

double intrestRATE;

int months;

double monthlyinterest;

double interestEarned;

double investBalence= investment;

int count = 0;

string investmentf;

string investBal;

cout << "Enter the amount of the investment.";

//Ask the user for the amount of the investment.

cout << "The minimum investment is $10,000.00.

";

//Get a minimum investment amount of $10,000 from the user.

do{

cin >> investment;

ostringstream stm; // For comma problem.

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//below code fixes comma problem.

stm << fixed << setprecision(2) << showpoint << investment;

string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investmentf = str;

}

if (investment < 10000.00)

{

cout << "

Error, the minimum investment is $10,000.00.

";

cout << "Enter the amount of the investment: ";

}

}while(investment < 10000);

cout << "Enter the annual interest rate:";

//Ask the user for the annual interest rate.

do{

cin >> intrestRATE;

if (intrestRATE < 0)

{

cout << "

cout << "

Enter the annual interest rate: ";

}

}while(intrestRATE < 0);

cout << "Enter the number of months of the investment: ";

//Ask the user for the months of the investment.

do{

cin >> months;

if (months < 0)

{

cout << "

Error, the number of months should be positive.";

//Get a non-negative number of months from the user.

cout << "

Enter the number of months of the investment: ";

}

}while(months < 0);

monthlyinterest = intrestRATE / 12 / 100; //Calculate the monthly interest rate-

//by dividing the annual interest rate-

//by 12 and then dividing the resulting value-

//by 100.

for( int count = 1; count <= months; count++) //Repeat for the number of months of the investment.

{

interestEarned = monthlyinterest * investment; //Calculate the interest earned by multiplying the-

//investment balance by the monthly interest rate.

investment += interestEarned; //Increase the value in the investment balance by the interest earned.

investBalence = investment;

//Save the initial investment amount in- //investmentbalance.

}

ostringstream stm ; // Note to self: this is so useful!

//definition: stringstream provides an interface to

//manipulate strings as if they were input/output streams.

//solves comma problem.

stm << fixed << setprecision(2) << showpoint << investBalence; string str = stm.str() ;

int n = str.size() ;

for( int i = n-6 ; i>0 ; i -= 3 )

{

str.insert( i, 1, ',' ) ;

investBal = str;

}

cout << "After " << months << " months, your investment of $" <<

investmentf << " will be worth $" << investBal << "." << endl;

//Display a message indicating the future value-

// of the investment stored in investment balance.

return 0;

}