简体   繁体   English

在C ++中排列文本输出

[英]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. 对于右对齐,来自<iomanip> std::setw效果很好。 Unlike other stream manipulators, std::setw is not sticky, so it will only modify your next immediate output. 与其他流操纵器不同, std::setw不粘,所以它只会修改你的下一个立即输出。

#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. 根据您的输出格式计算298 ,您可以根据需要进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM