Advertisement
ASP_Volume2 Miscellaneous #40462

Beginner Examples - how to format output

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

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
#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;
}
Původní komentáře (3)
Obnoveno z Wayback Machine