当前位置: 动力学知识库 > 问答 > 编程问答 >

arrays - Line up text output in C++

问题描述:

So i have a program that reads from a text file and outputs to another text file. Here is the file text (format included) that it reads:

Duckey Donald 85

Goof Goofy 89

Brave Balto 93

Snow Smitn 93

Alice Wonderful 89

Samina Akthar 85

Simba Green 95

Donald Egger 90

Brown Deer 86

Johny Jackson 95

Greg Gupta 75

Samuel Happy 80

Danny Arora 80

Sleepy June 70

Amy Cheng 83

Shelly Malik 95

Chelsea Tomek 95

Angela Clodfelter 95

Allison Nields 95

Lance Norman 88

In the program it adds each of these to a struct within an array; it then calculates the grade and prints.

cout << "Student Name Test Score Grade" << endl;

for (int studentNumber = 0; studentNumber < STUDENT_NUMBER; studentNumber++){

cout << students[studentNumber].studentLName << ", "

<< students[studentNumber].studentFName << " "

<< students[studentNumber].testScore << " "

<< students[studentNumber].grade << endl;

}

The desired output looks like this:

Student Name Test Score Grade

Donald, Duckey 85 B

Goofy, Goof 89 B

Balto, Brave 93 A

Smitn, Snow 93 A

Wonderful, Alice 89 B

Akthar, Samina 85 B

Green, Simba 95 A

Egger, Donald 90 A

Deer, Brown 86 B

Jackson, Johny 95 A

Gupta, Greg 75 C

Happy, Samuel 80 B

Arora, Danny 80 B

June, Sleepy 70 C

Cheng, Amy 83 B

Malik, Shelly 95 A

Tomek, Chelsea 95 A

Clodfelter, Angela 95 A

Nields, Allison 95 A

Norman, Lance 88 B

Highest Test Score: 95

Students having the highest test score:

Green, Simba

Jackson, Johny

Malik, Shelly

Tomek, Chelsea

Clodfelter, Angela

Nields, Allison

So basically my question is this... is there any way for the test score column integers to line up like they do in my above desired output?

SOLUTION

int colWidth = 20;

for (int studentNumber = 0; studentNumber < STUDENT_NUMBER; studentNumber++){

string s = students[studentNumber].studentFName + ", " + students[studentNumber].studentLName;

int size = s.size();

cout << s << std::string(colWidth - size, ' ')

<< students[studentNumber].testScore << " "

<<

网友答案:

You can pad the string manually with spaces like so:

#include <string>
#include <iostream>

int main()
{
    std::string first("John");
    std::string last("Smith");
    std::string full = last + ", " + first;
    int colWidth = 20;

    std::cout << "12345678901234567890\n"
              << full << std::string(colWidth - full.size(), ' ')
              << "aligned after 20 characters\n";

    return 0;
}

Outputs:

12345678901234567890
Smith, John         aligned after 20 characters

This will work for left justifying your text, given you calculate the number of characters you print.

For right-justifying, std::setw from <iomanip> works well. Unlike other stream manipulators, std::setw is not sticky, so it will only modify your next immediate output.

#include <string>
#include <iostream>
#include <iomanip>

int main()
{
    std::string s("ABCDE");
    int colWidth = 20;

    std::cout << "12345678901234567890\n";
    std::cout << std::setw(colWidth) << s << "\n";

    return 0;
}

Prints

12345678901234567890
               ABCDE

Putting it all together, I would left justify the first column, and right justify the second and third columns.

#include <string>
#include <iostream>
#include <iomanip>

int main()
{
    std::string first("John");
    std::string last("Smith");
    std::string full = last + ", " + first;
    int score = 100;
    std::string grade("A");

    std::cout << "123456789012345678901234512345\n"
              << full << std::string(20 - full.size(), ' ')
              << std::setw(5) << score
              << std::setw(5) << grade << "\n";

    return 0;
}

Prints:

123456789012345678901234512345
Smith, John           100    A
网友答案:

You can change your code like this:

#include <iomanip>
cout << "Student Name           Test Score  Grade" << endl;
for (int studentNumber = 0; studentNumber < STUDENT_NUMBER; studentNumber++){
    cout << setiosflags(ios::left) << setw(29) << setfill(' ') 
        << students[studentNumber].studentLName + ", " + students[studentNumber].studentFName 
        << setw(8) << students[studentNumber].testScore 
        << students[studentNumber].grade << endl;
}

where 29 and 8 are calculated based on your disired output format, you can change it as you needs.

分享给朋友:
您可能感兴趣的文章:
随机阅读: