Hey guys, I'm wondering if you can help me out on my problem. I wrote this code to print out consecutive numbers till it reach the maximum number entered. The code works but I want to control the output. Here's my code so far...

cout << "Enter max number: " << endl;
cin >> max;

int num = 1;
while (num <= max)
{
     cout << num++ <<'\t';
}
cout << endl;

I want to control the output from this:

Enter max number:
10
1 2 3 4 5 6 7 8 9 10

Press any key to continue . . .

To this...

Enter max number:
10
1 2 3 4
5 6 7 8
9 10

Press any key to continue . . .

Thanks in advance!

Recommended Answers

All 4 Replies

this should work

if (num%4 == 0)
 cout << endl;

Thanks, so I updated my code like you suggested to this...

cout << "Enter max number: " << endl;
			cin >> max;
			cout << endl;

			int num = 1;
			while (num <= max)
			{	
				cout << setw(4) << num++;
				if (num % 4 == 0)
					cout << endl;
			}
			cout << endl;
			system ("pause");

But how come my output is this:

Enter max number:
10

1 2 3
4 5 6 7
8 9 10
Press any key to continue . . .

Also, can you please explain why a modulus operator works? (I'm new to C++ obviously :))

Thanks!

It works now, I just changed 0 to 1. But why does the mod operator behaves like this? How come the output is still 4 output even though the mod result is not 1?

if (num % 4 == 0)

Oh now I get it, I just have to think for a minute.

Thanks kes166!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.