How can i add commas to certain numbers

Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:

Please enter an integer >= 1000 : 23456

23,456

Can anyone figure this out?






«1

Comments

  • #include
    using namespace std;
    int main()
    { cout << "Please enter an integer >= 1000 : ";
    double numbers;
    cin >> numbers;
    cout << numbers << endl;
    return 0;
    }


  • : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    :
    : 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
    #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?

  • Sometimes it is just easier to use a little figuring out beforehand and then bruteforce the result.

    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-

  • 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

  • : : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    : :
    : : 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?

  • : : : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    : : :
    : : : 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







  • : 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!

    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-

  • This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

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

  • : This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

    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

  • Sorry for reopening an old post but I was just assigned this exact problem out of my programming class's C++ book. Took me awhile to figure out the logic, but got a solution that works flawlessly with any number size.

    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;
    }
  • Here is a program that I wrote that does what your asking without the solution being more lines of code then your main program.

    #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;
    }
  • Here is a program that I wrote that does what your asking without the solution being more lines of code then your main program. I like examples with explanation and I'm lazy so deal with the long program example. Note that the programs format was changed when pasting into the website's replay box. I did go through a fix it as best I could but it still ain't the best.
    /* 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;
    }
  • Here is a program that I wrote that does what your asking without the solution being more lines of code then your main program. I like examples with explanation and I'm lazy so deal with the long program example. Note that the programs format was changed when pasting into the website's replay box. I did go through a fix it as best I could but it still ain't the best.
    /* 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;
    }
  • Here is a program that I wrote that does what your asking without the solution being more lines of code then your main program. I like examples with explanation and I'm lazy so deal with the long program example. Note that the programs format was changed when pasting into the website's replay box. I did go through a fix it as best I could but it still ain't the best.
    /* 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;
    }
Sign In or Register to comment.

Howdy, Stranger!

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

Categories