Problem with setw/setfill (c++)

Hello,

I'm quite new to the programming world, and am having some trouble getting my program (for an assignment, but I'm only looking for assistance with this minor bug) to output correctly. Basically, the program needs to read in grade information for a number of students, and then output them numerically and in a horizontal bar chart with asterisks.

I won't post the entire code, as I'm fairly certain I've isolated the problem to the following output section:

1
2
3
4
5
6
7
8
9
10
11
//Outputs the type of grade, numerical value, and appropriate number of asterisks for bar chart
	resultsfile << "Hw/Q"  << "\t" << hwquiztot/numstud << setw(hwquiztot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Test-1"<< "\t" << test1tot/numstud << setw(test1tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Test-2"<< "\t" << test2tot/numstud << setw(test2tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Final" << "\t" << finaltot/numstud << setw(finaltot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog1" << "\t" << p1tot/numstud	<< setw(p1tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog2" << "\t" << p2tot/numstud	<< setw(p2tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog3" << "\t" << p3tot/numstud	<< setw(p3tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog4" << "\t" << p4tot/numstud	<< setw(p4tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog5" << "\t" << p5tot/numstud	<< setw(p5tot/numstud/2) << setfill('*') << '\n';
	resultsfile << "Prog6" << "\t" << p6tot/numstud	<< setw(p6tot/numstud/2) << setfill('*') << '\n';


I know for a fact that each calculation inside the setw() is equal to the correct number of asterisks I need, as I tested this in my output by outputting the number numerically and as asterisks:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Thomas Duke
Student Score/Curve
_________________________________________________
Name		Score		Curve
Brennan,Walter	75		77
Boone,Richard	20		27
Marvin,Lee	59		62
Fonda,Henry	73		75
Cooper,Gary	80		82
Stewart,Jimmy	87		88
Rogers,Roy	87		89
Wayne,JohnDuke	73		76
Eastwood,Clint	38		44
Scott,Randolph	83		85
_________________________________________________

CS1044 Class Averages
Assign Avg........20........40........60........80.......100
Hw/Q	71**********************************
Test-1	67********************************
Test-2	64*******************************
Final	70*********************************
Prog1	70*********************************
Prog2	68*********************************
Prog3	73***********************************
Prog4	69*********************************
Prog5	65*******************************
Prog6	63******************************
35
33
32
35
35
34
36
35
33
32


Clearly, the number of asterisks are not equal to the number inside of the setw(). I do not know why this is, and any help would be appreciated.

This is the correct output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Dwight Barnette
Student Score/Curve
__________________________________________________
Name               Score      Curve
Brennan,Walter       75         77
Boone,Richard        20         27
Marvin,Lee           59         62
Fonda,Henry          73         75
Cooper,Gary          80         82
Stewart,Jimmy        87         88
Rogers,Roy           87         89
Wayne,JohnDuke       73         76
Eastwood,Clint       38         44
Scott,Randolph       83         85
__________________________________________________

CS1044 Class Averages:
Assign Avg........20........40........60........80.......100
Hw/Q    71***********************************
Test-1  67*********************************
Test-2  64********************************
Final   70***********************************
Prog1   70***********************************
Prog2   68**********************************
Prog3   73************************************
Prog4   69***********************************
Prog5   65*********************************
Prog6   63********************************


As you can see, the number of asterisks in each row are equal to the numbers printed in my output. I realize it's very likely I'm simply doing something fundamentally wrong. Am I using setfill/setw incorrectly? I really appreciate any help given.
Last edited on
Well, inside setw() you are dividing by two. However, this is integer division, so the result is not rounded up - rather, it is truncated - which I would guess is why the number of asterisks is off in the "correct" example.
Initially I assumed it was a rounding issue like that, but each expression inside of the setw() is equal to the number of asterisks I need, as shown in the output with the numbers below the "chart." (the numbers in the first output are equal to the asterisks in the 2nd) If it was a rounding issue, the number inside the setw() would not be equal to the correct number of asterisks. However, I am completely stumped as to what else could be causing it.

On an unrelated note, wouldn't it be double division? It's a double divided by a double divided by an integer, which equals a double divided by an integer. Or am I wrong?
Last edited on
Ah, if your variables were doubles it would be floating point division, you're right. Though whatever number you end up with will be truncated...though that seems to be what you want.

Are you telling the stream to output doubles without any digits after the decimal place? If so, then perhaps setw() is truncating the double you pass it while the stream is rounding it?
Everything is being restricted to 0 decimal places (resultsfile << fixed << setprecision(0))

But that's not even the problem, really. With the Hw/Q value, for example, the printed value for hwqtot/numstud/2 is 35. 35 is the correct number of asterisks in the correct output. However, in my output, resultsfile << setw(hwqtot/numstud/2 = 35) << setfill('*') is printing out a line of 34 asterisks. I just can't figure out why that would be.
Perhaps the '\n' counts as a character so it prints out 34 then the '\n' takes up the last character?
Doesn't appear to be the case when I remove the newline. Plus, in some cases, the number of asterisk printed are off by 2 (while the number inside the setw() is still correct).
I still tempted to go with a combination of the previous two answers - a combination of rounding
errors and the '\n' character taking up one '*' place.
I would just use something like this to output n asterisks:
cout << string(n, '*');
Don't try to invent some new method just use what you already have.
sim... he isn't inventing any methods here. He is using the setw and setfill manipulators from the standard <iomanip> header.

PS: Do you really need to call setfill after each cout? I thought you only had to do that with setw.
guest - like I said, the number inside of the setw() is equal to the number of asterisks in the correct output, which, unless I'm missing something, should pretty much rule out a rounding issue. And when I pulled out the newline character for the first line, it did not change the number of asterisks printed.

sim - I wasn't trying to invent anything new. I only know the few things that have been brought up in my class (like I said, 1 month), and setw()/setfill() was the only method I knew of to do this.

I switched all the setw()/setfill()s to what you suggested. I thought it had fixed it as the first line was now correct, but several of the lines were still incorrect. Which is very confusing to me....

hanst - It's very possible that you don't need it after each cout, I really don't know. I can't imagine that would change the number of output, though.
Topic archived. No new replies allowed.