Speed up development with full-stack environments for every branch.

Learn More

Format Output in C++ using iomanip

1012 Runs 5555 Views 845 Copies
Saved

Saved

tianxiasoft 13

tianxiasoft
published 4 years ago

#include <iostream>
#include <iomanip>

using namespace std;
/******************************************************************************************/
/*  This is an example that input the scores of 5 students and print it with some format. */
/******************************************************************************************/

#define MAX_WIDTH_NAME 10
#define MAX_WIDTH_SCORE 5

// main function
int main()
{
  int i = 0;
	char names[5][11];
	float scores[5];

	// input the name and score of 5 students.
	for (; i < 5; i ++)
	{
		cout << "Input the name of no." << i + 1 << endl;
		cin >> names[i];
		cout << "Input the score of no." << i + 1 << endl;
		cin >> scores[i];
	}

	// ----------------------------------------------
	// print name and score in some format
	// ----------------------------------------------

	cout << setfill('*') << setw(30) << "*" << endl ;
	// print "No          Name         Score"
	cout << setfill(' ') << "No" << setw(15) << "Name" << setw(15) << "Score" << endl;
	for (i = 0; i < 5; i ++)
	{
		// print "i        name         Score"
		cout << setw(2) << i + 1 << setw(15) << names[i] << setprecision(3) << setw(15) << scores[i] << endl;
	}
	cout << setfill('*') << setw(30) << "*" << endl ;

	// wait for any key pressing
	cin.get();
	return 0;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).