Help add about code

Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right c++. Do not use arrays, for loops, functions, pointers, or while loops. This problem was designed to highlight the difference between integer and real division.
I have a problem when I input number 10000000, output 10,0,0. I want to 100,000,00 but I do not how to code. Can you help me? Thank you so much. And this is my code

#include <iostream>
using namespace std;
int main()
{
int leadingDigits, middleDigits, lastDigits;
int tempValue, original;
cout<<"Please enter 7- to 9-digit number.\n";
cin>>original;

tempValue = original / 1000;
lastDigits = original % 1000;

leadingDigits = tempValue / 1000;
middleDigits = tempValue % 1000;

cout<<"The number with commas is "<<leadingDigits;
cout<<","<<middleDigits<<","<<lastDigits<<endl;
return 0;
}
if you do it this way you will need to handle 1, 2 and 3 digit cases.
if 123456 %1000 is 456, that is a 3 digit case.
but 123056 % 1000 gives only 56: you need to do something.
and 123001 % 1000 gives 1, do something again!
a crude way is if statements, if result < 10 do something else if result < 100 do something else default ...

Can you help me with the code? Since I'm new to c++ code, I don't know much, I search a lot online, I feel confused. Thank you so much.
> I have a problem when I input number 10000000, output 10,0,0
You need to output 0 as 000.
In fact, anything <100 not in the leading position needs to be padded with zeros.

https://en.cppreference.com/w/cpp/io/manip
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>

int main()
{
    int i = 42;
    std::cout << std::setfill('0') << std::setw(3) << i << std::endl;
    std::cout << i << std::endl;
}


042
42

Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right.
I want to 100,000,00

Shouldn't it be 10,000,000 ?

@seeplus,
You need to output 0 as 000.
In fact, anything <100 not in the leading position needs to be padded with zeros.

Why ? The exercise doesn't demand it.
Last edited on
The easiest way to generate the groups is to do it from right to left - using % and / operators.
You can move the code from the function into main, since you are not allowed to use functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <exception>

void pretty_print(int num)
{
	if(num < 1'000'000 || num > 999'999'999)
		throw std::exception("Invalid in put in function pretty_print");

	int last_group = num % 1000;
	num /= 1000;
	int middle_group = num % 1000;
	num /= 1000;

	std::cout << num << ',' << middle_group << ',' << last_group << '\n';
}

int main()
{
	pretty_print(1234567);
	pretty_print(12345678);
	pretty_print(12345679);
}

Output:
1,234,567
12,345,678
12,345,679


A good book to learn problem solving is "Think Like a Programmer: An Introduction to Creative Problem Solving"
https://www.amazon.co.uk/Think-Like-Programmer-Introduction-Creative/dp/1593274246/ref=sr_1_1?dchild=1&keywords=think+like+a+programmer&qid=1629794882&s=books&sr=1-1
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   int i;
   cout << "Enter an integer (9 digits or less): ";   cin >> i;
   int millions = i / 1000000, thousands = i / 1000 % 1000, ones = i % 1000;
   #define THREE << ',' << setfill( '0' ) << setw( 3 ) << 
   if      ( millions  ) cout << millions THREE thousands THREE ones << '\n';
   else if ( thousands ) cout <<                thousands THREE ones << '\n';
   else                  cout <<                                ones << '\n';
}

Enter an integer (9 digits or less): 12045006
12,045,006
The 'C++ way':

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
#include <iostream>
#include <locale>

struct comma_facet : public std::numpunct<char> {
	explicit comma_facet(size_t group) : g(group) {}
	virtual char do_thousands_sep() const { return ','; }
	virtual std::string do_grouping() const { return std::string(1, static_cast<char>(g)); }
	size_t g {};
};

struct set000s {
public:
	explicit set000s(size_t grp = 3) : group(grp) {}

private:
	size_t group {};

	friend std::ostream& operator<<(std::ostream& os, const set000s& grp) {
		os.imbue(std::locale(os.getloc(), new comma_facet(grp.group)));
		return os;
	}
};

int main()
{
	std::cout << set000s() << 1234567 << '\n' <<
		12345678 << '\n' <<
		12345679 << '\n';
}



1,234,567
12,345,678
12,345,679

Thank you so much, everyone.
Topic archived. No new replies allowed.