How to Align Output in C++?

How to Align Output in C++

Do you want to know how to align output in C++ (CPP)? In this article, we’ll discuss the alignment of a C++ Output. 😃

The text placement on the screen is referred to as “alignment” or “aligned.” For instance, text that is left-aligned produces a line of text on the left side of the page (like this paragraph). Text can be aligned to follow any visible or invisible line, including the edge of a page, cell, div, table, or another object. Here are a few samples of aligned text from various sources.

In this article, we’ll discuss alignment operators in CPP, how to align the output of a C++, and different types of alignment. so that you can align your output according to the desired alignment. So without further ado, let’s dive deep into the topic and see some real examples!

 

 

What is an Aligned Output in C++?

An aligned output in C++ means that we have changed the positioning of the lines and characters in our output. An optimal output is generated by the C++ compiler, but if we want some change in output, we can make changes with the help of “alignof” operators in C++. The alignment is returned in bytes of the supplied type using the “alignof” operator.

 

Following is the syntax for the “allignof” operator in C++.

alignof( data type )

 

Code

#include <iostream>

using namespace std;

struct Allign {

    float a;

    char b;

int i;

};

int main()

{

cout << " The Alignment of char datatype: " << alignof(char) << endl;

cout << " The Alignment of int datatype: " << alignof(int*) << endl;

cout << "The Alignment of float datatype: " << alignof(float) << endl;




return 0;

}

 

Output

The Alignment of char datatype: 1

 The Alignment of int datatype: 8

The Alignment of float datatype: 4

 

In the above example, we have used the “alignof” operator, which basically returns the alignment in bytes for the data types we have provided: int, float, and char. The alignment is returned in bytes of the supplied type using the align function of the operator.

 

 

How to Align Output in C++?

There are different approaches that can be used to align output in C++. You can leave justify an output. You can also right justify an output as well. You can also change the alignment of the output by using the “customize width” option. Outputs can be aligned in the following ways:

  1. Left justified output
  2. Right justified output
  3. Custom justified output

 

How to Left Justify Align an Output in C++?

Only the subsequent value to be printed is impacted by the setw() manipulator. The minimum field width for the value to be printed is an integer that is sent as a parameter to the setw() manipulator. You can left-align an output by printing it to the left of the specified width. Let’s see an example.

 

Code

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  int col_width = 30;

  cout << left; 

  cout << setw(col_width) <<250.59 << endl;

  return 0;

}

 

Output

250.59       

 

We can observe in the above example that integer 250.59 is justified to the left most of the Output as it is printed according to the width of the output which is 30

 

 

How to Right Justify Align an Output in C++ ?

The setw() manipulator only affects the next value to be printed. The number passed as a parameter to the setw() manipulator specifies the minimal field width at which the value will be written. Output can be left aligned by printing it to the right of the designated width. Let’s see an example.

 

Code

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  int col_width = 30;

  cout << left; 

  cout << setw(col_width) <<250.59 << endl;

  cout << right;

  cout << setw(col_width) <<  9.5 << endl;

  

 

  return 0;

}

 

Output

250.59                        

                           9.5

 

It is clear from this example we can see that the integer 2

 

 

How to Use Custom Justified Output in C++?

For padding with a specific character, we may alternatively use the std::setfill manipulator. Another helpful manipulator, this one uses a single input to set the fill character for the output.

 

Code

#include <iostream>

#include <iomanip>

#include <string>




const char* text = "The value at index: ";

const size_t max_width= 10;




void print(const std::string& var_text, int num)

{

    std::cout << text

              // align output to left, fill goes to right

              << std::left << std::setw(max_width) << std::setfill('.')

              << var_text << ": " << num << '\n';

}




int main()

{

    print("13232", 0);

    print("12343331115", 1);

}

 

Output

The value at index: 13232.....: 0

The value at index  : 12343331115: 1

 

We can see in the above example that by using std::setfill, we can align the text “The value at index: ” to the integer we are printing in the output; the width of the text is 10; it should not exceed 10.

 

 

Conclusion

To summarize the article, on how to align the output of a C++ program in the desired format (left justified or right justified), we have discussed three different ways we can align output. You may use these methods to provide the base for more complex C++ output alignment methods. 

There are three ways discussed above to set the alignment of the output of a C++ program.

Let’s have a quick recap of the topics discussed in this article.

  1. What is an aligned output of C++?
  2. How to align output in C++?
  3. Left justified output in C++.
  4. Right justified output in C++.
  5. Custom-justified output in C++.

If you’ve found this article helpful, don’t forget to share and comment below. 👇: Which solutions have helped you solve the problem?

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share