README.md

December 5, 2022 ยท View on GitHub

Beginner Examples - how to format output

Description

A couple of simple examples of ways to format output using iomanip; ie: set width, right align, left align, change fill character

More Info

Submitted On
By*LuckY*
LevelBeginner
User Rating4.0 (16 globes from 4 users)
CompatibilityC++ (general)
CategoryMiscellaneous
WorldC / C++
Archive File

Source Code

#include <iostream>
#include <iomanip>
using namespace std;
//
int main() {
	/*notes: newer implementations use ios_base
	and not ios. Also they allow using the following
	syntax:
		cout << left;
	to set the alignment to the left/right
	*/
	//
	cout.setf(ios::left,ios::adjustfield);
	cout << setw(20) << "Name:";
	cout.setf(ios::right,ios::adjustfield);
	cout << setw(60) << "Occupation:\n";
	//
	/* change fill character from ' ' to '.' */
	//
	cout.fill('.');
	//
	cout.setf(ios::left,ios::adjustfield);
	cout << setw(20) << "*LuckY*";
	cout.setf(ios::right,ios::adjustfield);
	cout << setw(60) << "Student -- Computer Science\n";
	//
	return 0;
}