How can i add commas to certain numbers

2»

Comments

  • 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