Archived

This topic is now archived and is closed to further replies.

Eriond

Problems with formatting output in consolewindow

Recommended Posts

I''m trying to format the output in my simple program:
int main(int argc, char *argv[])
{
	using namespace std;
	srand(time(0));
	vector<int> results;
	int nr;
	cout << "How many times do you want 2 dices to be rolled? ";
	cin >> nr;
	results.reserve(nr);
	for (int i = 0; i < nr; ++i)
	{
		results.push_back((rand() % 6 + 1) + (rand() % 6 + 1));
	}

	vector<int> freq(11);
	for (int i = 0; i < 11; ++i)
	{
		freq[i] = count(results.begin(), results.end(), i + 2);
	}
	for (int i = 0; i < freq.size(); ++i)
	{
		cout << setw(2) << setfill('' '') << i + 2; 
		cout << setw(4) << setfill('' '') << " ";
		cout << freq[i];
		cout << fixed << setprecision(2);
		cout << setw(8) << setfill('' '') << " ";
		cout << (static_cast<float> (freq[i]) / results.size()) * 100 << "%" << endl;
	}
	return 0;
}
If you run the program you see that the last column is not nicely formatted, but it only happens if the different values in the middle column is different in length. I have tried with different formatting functions but I can''t make it look nice. If you want to see what I mean, enter 100 000 as the number of times to throw 2 dice. How do I fix that?

Share this post


Link to post
Share on other sites
If I remember correcty those sad stream formating command only apply to one output and are then discarded. At this point I like to point out that not everything has to be streamed and that sprintf is not inherently evil. Back to business. I fear

cout << setw(4) << setfill(' ') << " ";
cout << freq[ i ];

leads to " " being formated as desired and freq[ i ] being dumped as usual, without formating. Either try the following:

cout << setw(2) << setfill(' ') << i + 2;
cout << setw(5) << setfill(' ') << freq[ i ];
cout << fixed << setprecision(2);
cout << setw(9) << setfill(' ') << (static_cast (freq[ i ]) / results.size()) * 100.0f << "%" << endl; }

Note that I increased the setw() commands to account for the removed blanks. Also multiplying with 100.0f instead of 100 is jst more correct and yields more precise results.

If the use of older functions doesn't offend you, I offer another alternative:

char buffer[100];
sprintf( buffer, "%2d%5d%9g%%\n", i+2, freq[ i ], (static_cast (freq[ i ]) / results.size()) * 100.0f );
cout << buffer;

Yes, streaming is great, but I like code that does something once in while, other than look bloated.

And since I am in a know-it-all mood at the moment a small remark about your dice throwing: Since you apply % on a value that is not a multiple of the base you get an uneven distribution of 1, 2, .. 6. Doesn't matter here really but your results might be a little biased. Somewhat better would be

(int)((float)rand()*6.0f/(float)RAND_MAX) + 1

for each dice. And slowly we get to the point where a small inline function would increase readablity.

And now I should shut up
Edit: how on earth does one use [ i ] without ending up in italics mode? it's not like it's an uncommon sequence in code



[edited by - Dreamforger on January 15, 2004 5:01:41 PM]

Share this post


Link to post
Share on other sites