I am trying to print out the number of occurences for each alphabetic chracter in a file that our instructor provided us with. The values range from 1 to 999. I'm trying to print out "count[...] : " followed by the character. So far my counts are correct. However, at the end of each & every line, the program 'spits' out the digit '4.' Is the problem being caused by the arguments in my printf function? Thanks!

// The values of possible count[c] range from 1 through 999

// Print array of the 26 letters in the alphabet & their counts
for (c = 0 ; c < 26 ; c++)
     cout << "count['" << (char)('a' + c) << "'] : " << printf(" %3u", count[c]);
		cout <<  endl;
6 Years
Discussion Span
Last Post by Banfa

You really shouldn't mix cout and printf especially on the same line...When you have this


You'll print out the result of calling printf which is four characters in your code.

Edited by gerard4143: n/a


Mixing cout and printf to output your results is a mistake.

That 4 being output is the return value of printf, it printed 4 characters.

Get rid of the printf and instead pipe count[c] staight to cout in the normal way. If you want to format the output read up on the io manipulators found in the header <iomanip>

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.