#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# How can i add commas to certain numbers

2»

• Posts: 1Member
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;
}
· ·
• Posts: 5Member
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;
}
· ·
• Posts: 5Member
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;
}
· ·
• Posts: 5Member
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;
}
· ·
• Posts: 5Member
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;
}
· ·
• Posts: 5Member
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;
}
· ·
2»