Reputation: 11
I was able to align my columns but I am having trouble with my coding where my "%" will not connect to the percent variable.
This is my coding for the output/alignment:
//HEADER
outFile << left << setfill(' ') << setw(15) << "Member Name"
<< setfill(' ') << setw(20) << "Portion Achieved"
<< setfill(' ') << setw(12) << "Grade (%)"
<< setfill(' ') << setw(15) << "Letter Grade"
<< setfill(' ') << setw(15) << "Comment" << endl;
while (count <= 6)
{
cout << "Enter Name: ";
cin >> student.name;
cout << "Points Achieved: ";
cin >> student.points;
cout << "\n";
decimal = (student.points/60);
percent = decimal*100;
outFile << left << setfill(' ') << setw(15) << student.name
<< setfill(' ') << setw(20) << decimal
<< setfill(' ') << setw(12) << percent << " %"
<< setfill (' ') << setw(15);
This is how the output looks:
Member Name Portion Achieved Grade (%) Letter Grade Comment
Min 0.166667 16 %F Sorry, you did not pass :(
Carmela 0.333333 33 %F Sorry, you did not pass :(
Jayson 0.5 50 %F Sorry, you did not pass :(
Kristin 0.666667 66 %D Needs Improvement!
Mae 0.833333 83 %B Well Done!
JT 1 100 %A Excellent!
VS how I want it to look:
Member Name Portion Achieved Grade (%) Letter Grade Comment
Min 0.166667 16% F Sorry, you did not pass :(
Carmela 0.333333 33% F Sorry, you did not pass :(
Jayson 0.5 50% F Sorry, you did not pass :(
Kristin 0.666667 66% D Needs Improvement!
Mae 0.833333 83% B Well Done!
JT 1 100% A Excellent!
Upvotes: 0
Views: 86
Reputation: 385104
It's doing what you told it to do. The integer percent
value is given a "width" of 12 characters. Anything that comes after, comes after. There is no code here to "connect" anything together, nor does any such feature exist in C++ stream speak.
You're probably going to have to pre-construct percent + " %"
(pseudocode) into a string, then use that single string as the column value. Perhaps:
<< setfill(' ') << setw(12) << (std::to_string(percent) + " %")
Otherwise, drop setw
and write n spaces yourself. To do this, you'll need to work out how "long" your stringised percent
text is, add 2 (for " %") then subtract the lot from 12. Write std::string(n, ' ')
to the stream. Yuck!
Upvotes: 1