How to Justify a Block of Text for Better Readability: Tips and Tricks

Also, you might want to use a default case, just in case the logic doesn't match up with any of the cases.
  • #1
magnifik
360
0
i am wondering what is the best way to justify a block of text. for example, right now i have a code that prints: 1 2 3 4 5 6 7 8 9 10 11 and so on, but i want it to print

1 2 3 4 5 6 7
10 11 12 13 14 15 16​

in other words, there is a new line at the end of the seventh number. this is the code:

Code:
int i = 0;
while (i < 31){
cout << setw(4) << i;
i++;
}

thanks
 
Physics news on Phys.org
  • #2
magnifik said:
i am wondering what is the best way to justify a block of text. for example, right now i have a code that prints: 1 2 3 4 5 6 7 8 9 10 11 and so on, but i want it to print

1 2 3 4 5 6 7
10 11 12 13 14 15 16​
You skipped some numbers in the above.
magnifik said:
in other words, there is a new line at the end of the seventh number. this is the code:

Code:
int i = 0;
while (i < 31){
cout << setw(4) << i;
i++;
}

thanks

You need some logic in your loop to print a newline character every seventh number. Also, the logic in your loop is off by one: it starts printing at 0 and ends at 30. The constant 31 makes me think that this is related to printing calendar months.

Code:
int i = 1; 
while (i <= 31) 
{
   cout << setw(4) << i;
   if (i % 7 == 0)
   {
      cout << endline;
   }
   i++;
}

This should give output like this:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
...
22 23 24 25 26 27 28
29 30 31
 
  • #3
ok, thanks for the help. but what if the numbers start at a different column? going with the idea of a calendar, if you started on say, wednesday it should look more like

1 2 3 4
5 6 7 8 9 10 11
and so on...

how do i modify the code to reflect something like that?
 
  • #4
Your program logic needs to figure out what day of the week to start on. Most calendars that I'm familiar with start on Sunday, although one Italian calendar I had started on Monday.

If the first of the month falls on Sunday, then my preceding code works fine for months with 31 days. (Months with fewer days is another story, and the fix for that entails using a variable to control the while loop instead of hard-coding 31 in it.)

If the first of the month falls on some other day, your first row of numbers needs to start some number of spaces to the right, and needs to quit after the last day of the week. Using your example of the first falling on Wednesday, the program logic needs to start printing in the fourth column (the one for Wednesday), and needs to issue an endline after the 4th, 11th, 18th, and 25th. If i represents the day of the month, for these dates the expression i % 7 == 4.

Can you write the code that takes these ideas into account?
 
  • #5
Code:
int day1;
switch (day1){
  case 0: if (i % 7 == 0) cout << endl;
  .
  .
  .
  case 5: if (i % 7 == 2) cout << endl;
}

skipped a bunch b/c i think i get the idea
 
  • #6
Don't forget that you need a break statement in each case.
 

What is the purpose of justifying a block of text?

The purpose of justifying a block of text is to create a clean and professional appearance by aligning the text evenly on both the left and right sides.

How do you justify a block of text?

To justify a block of text, you can use the text-align: justify; CSS property, or you can use the text-align: justify; HTML attribute in a

tag.

What are the benefits of justifying a block of text?

Justifying a block of text can make the text easier to read, improve the overall appearance of a document, and create a more polished and professional look.

What types of documents typically require justified text?

Justified text is commonly used in formal documents such as reports, research papers, and business letters. It is also often used in newspapers, magazines, and books.

Are there any downsides to justifying a block of text?

One potential downside of justifying text is that it can sometimes create large gaps between words, which may affect the readability of the text. It can also be more challenging to align images and other elements with justified text.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
985
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Back
Top