Need help with commas in calc program

While perfecting and adding things to my first calculator program i ever wrote, i have all the basic things and now, I'm left to wonder how to make numbers like 34293 into 34,293 and add commas every 3rd number.

This is just a section of my program that raises a given number to a given power.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
                                       unsigned int x, power;
                                       unsigned long int product;

					cout << "Pick a number you wish to raise to the power of another number: ";
					cin >> x;
					
					cout << endl << endl;

					cout << "Pick a number you wish to be the \n";
					cout << "amount times the first number is multipled by: ";
					cin >> power;

					cout << endl << endl;

					product = pow(x, power);
					
					cout << x << " rasied to the power of " << power << " equals " << product << "." << endl << endl;

					cout << "Press Y if you want to return to the ";
					cout << "main menu, Press N if u want to close the program: ";
					cin >> repeatMenu;
					break;
Last edited on
The answer is to ignore the comma. You can take the number in a string format with all the comas. Then when you are reading the string from the user you can skip input whenever a coma is an input. Then once you have the whole number in string format you can use the string to number conversion functions like stod and stoi.
I finally found some source code that actually works and put commas in but after the program stops it pops up, unhandled exception, the string binding is invalid. heres the code

1
2
3
4
5
6
7
8
9
10
11
template<typename CharT>
struct Sep : public std::numpunct<CharT>
{
	virtual std::string do_grouping()      const   { return "\003"; }
};

int main()
{
	std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>()));

	std::cout << 123456789 << "\n";


and i use it with #include <locale> ofc.

How about this?
https://ideone.com/3N8PvS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string toString(const int num)
{
	stringstream ss;
	ss << num;
	return ss.str();
}

void formatCommas(string& str)
{
	/*
	we add a comma every 3rd digit
	*/
	int num_of_commas = str.length() / 3;
	/*
	we don't want a comma at the end of temp
	eg. 133 333 337
	num_of_commas = 3
	but there only should be 2 commas.
	*/
	if(str.length() % 3 == 0) --num_of_commas;
	/*
	reverse the string to make it easier to work with
	this is because we start adding commas from the end
	*/
	string temp(str.rbegin(), str.rend());
	/*
	add the commas in the necessary position, i.e. every 3rd digit
	note the i-1. this is because the length of the string changed
	due to us adding a comma so we need to compensate for that.
	*/
	for(int i{1}; i <= num_of_commas; i++) {
		temp.insert(i * 3 + i-1, ",");
	}
	/*
	reverse the reversed string to put the string in order
	*/
	str.assign(temp.rbegin(), temp.rend());
}

int main()
{
	int num{ 133333337 };
	cout << "Original number: " << num << "\n";
	string strNum{ toString(num) };
	formatCommas(strNum);
	cout << "Formatted number: " + strNum + "\n";
}



Original number: 133333337
Formatted number: 133,333,337
Last edited on
Topic archived. No new replies allowed.