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:
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:
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.
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?
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.
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.
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.