开发者

How to line things up when outputting to a file in C++

开发者 https://www.devze.com 2023-01-19 05:43 出处:网络
New code.... #include <cstdlib> #include <iostream> #include <fstream> #include <iomanip>

New code....

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void gradeg (double & average, string & grade)
{
    if (average >= 90)
        {
            grade = "A";
        }
        else if ((average < 90) & (average >= 80))
        {
            grade = "B";
        }
        else if ((average < 80) & (average >= 70))
        {
            grade = "C";
        }
        else if ((average < 70) & (average >= 60))
        {
            grade = "D";
        }
        else
        {
            grade = "F";
        }
}

void totalg (int & total, int & s1, int & s2, int & s3)
{
    total=(s1+s2+s3);
}

void averageg (double & average, int & total)
{
    average=total/3.0;
}

int main()
{
    ifstream fin;
    ofstream fout;

    fout.setf (ios::fixed);
    fout.setf (ios::showpoint);
    fout.precision (2);

    char filename[15];

    fin.open("input.txt");

    cout << "Please enter the name of your output file: ";
    cin >> filename;

    fout.open(filename);

    if (fin.fail())
    {
        cout << "Failed to open INPUT file." << endl;
        exit(1);
    }
    if (fout.fail())
    {
        cout << "Failed to open OUTPUT file." << endl;
        exit(1);
    }

    string grade, name;
    int s1, s2, s3, total=0;
    double average=0.0;


    fout << left << "Names" << setw(24) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n";

    cout << "Reading from input files.....";

    while (! fin.eof())
    {
        fin >> name >> s1 >> s2 >> s3;

        totalg (total, s1, s2, s3);

        averageg (average, total);

        gradeg (average, grade);


        fout << left << name << setw(24) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";

    }

    cout << "Your output file has been created and the computation results have been stored.";

    return 0;
}

New output...

NamesScore 1                 Score 2   Score 3   Total     Average   Grade 
   DAN100                     70        85        255       85.00     B         
JANE78                      82        90        250       83.33     B         
PETER82                      84        91        257       85.67     B         
MINIE98                      100       75        273       91.00     A         
JOSEPH71                      62        100       233       77.67     C        开发者_JS百科 
CHRISTOPHER91                      75        82        248       82.67     B         
BEN54                      84        77        215       71.67     C         


You're making a very simple mistake, which is that a formatting manipulator (such as setw) has to come before the data that it formats, like so:

fout << left << setw(24) << "Names" << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n";
fout << left << setw(24) << name << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";
//              ^^^^^^^^^^^ NOTE THIS PART

The word Names will take up 5 characters, and it will be followed by 19 spaces. Following that, you'll see Score 1 (7 characters) followed by 3 spaces, and so forth.

Names                   Score 1   Score 2   
Ken Bloom               75        100       
Alec                    100       75        


Use the "left" manipulator for the name, then the "right" manipulator for the numbers:

fout << left << setw(19) << "Names" << right << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade" << '\n';

fout << left << setw(19) << name << right << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";

This is the output I got:

Names                 Score 1   Score 2   Score 3     Total   Average     Grade
DAN                       100        70        85       255     85.00         B
JANE                       78        82        90       250     83.33         B
PETER                      82        84        91       257     85.67         B
MINIE                      98       100        75       273     91.00         A
JOSEPH                     71        62       100       233     77.67         C
CHRISTOPHER                91        75        82       248     82.67         B
BEN                        54        84        77       215     71.67         C
BEN                        54        84        77       215     71.67         C
0

精彩评论

暂无评论...
验证码 换一张
取 消