字符输出格式问题

问题描述 投票:0回答:1

计算机科学一年级学生,正在为我当前的班级做一个项目,其中涉及格式化字符输出。我以前做过,无论我怎么看,我都无法弄清楚我在这里做错了什么。第一个输出行和外部 for 循环的迭代正确地格式化了它。此后的每次迭代都格式错误。我可能在这里遗漏了一些明显的东西,但我看不到它。 代码:

    //Loop output in given format
    //students[i].name right/width 30, students[i].studentID right/width 15, *(students.testScores + j) right/width 5
    for (int i = 0; i < numStudents; ++i)
    {
        cout << right <<
        setw(30) << students[i].name <<
        setw(15) << students[i].studentID;
        //Iterate students[i].numTests amount of times for *(students[i].testScores + j)
        for (int j = 0; j < students[i].numTests; ++j)
        {
            cout << right << setw(5) << *(students[i].testScores + j);
        }
        //End on a newline
        cout << endl;
    }
    

输出:

            Smith,John Stevens       12456214   99   98   96   92   91
                
Johnson,Chris       11058975   84   83   78   91
                    
abcd,abcd       11114444  100  100  100   98
               
Stromberg,Noah         121678   25   50   75  100
                   
Hutch,Maya         8098121   90   80   70   60   50

输出的第一行再次反映了它应该如何格式化。感谢任何帮助。

我注意到问题很可能出现在开头或结尾,因为格式不正确的行由于宽度和对齐方式错误而不正确。

这是我的标题:

#include <iostream> 
#include <cstdio>
#include <string>
#include <stdio.h>
#include <fstream>
#include <iomanip>

using namespace std;

//Initialize Global Variables
int TESTS = 5;
enum MENU{Add = 1, Remove = 2, Display = 3, Search = 4, Results = 5, Quit = 6};
struct Student{
    string name;
    int studentID;
    int numTests;
    int *testScores = new int[TESTS]; //Access with *(<variableName>.testScores + i)
    int avgScore;
};

//Initialize filestreams
ifstream inFS;
ofstream outFS;
string FILENAME = "student.dat"; //used to save time and brain power

这是整个display()函数:

void display()
{
    //Initial variable declaration
    int numStudents = getNumber();
    Student *students = new Student[numStudents];
    string firstName, lastName, numHold;

    //Open ifstream student.dat & check for errors
    inFS.open(FILENAME);
    if (!inFS.is_open())
    {
        cout << "Error opening " << FILENAME << " Aborting." << endl;
        //Delete heap space & Exit the function
        delete[] students;
        return;   
    }

    //Iterate getNumber() times
    for (int i = 0; i < numStudents; i++)
    {
        //Get student[i].firstName and lastName
        getline(inFS, lastName, ',');
        getline(inFS, firstName, ',');
        students[i].name = lastName + "," + firstName;

        //Get students[i].studentID
        getline(inFS, numHold, ',');
        students[i].studentID = stoi(numHold);

        //Get students[i].numTests
        getline(inFS, numHold, ',');
        students[i].numTests = stoi(numHold);

        //Get students[i].numTests amount of testScores
        for (int j = 0; j < students[i].numTests; ++j)
        {
            getline(inFS, numHold, ',');
            *(students[i].testScores + j) = stoi(numHold);
        }
    }

    //Close inFS
    inFS.close();

    //Loop output in given format
    //students[i].name right/width 30, students[i].studentID right/width 15, *(students.testScores + j) right/width 5
    for (int i = 0; i < numStudents; ++i)
    {
        cout << 
        right << setw(30) << students[i].name <<
        setw(15) << students[i].studentID;
        //Iterate students[i].numTests amount of times for *(students[i].testScores + j)
        for (int j = 0; j < students[i].numTests; ++j)
        {
            cout <<
            setw(5) << *(students[i].testScores + j);
        }
        //End on a newline
        cout << endl;
    }
    
    //Exit the function
    delete[] students;
    return;
}

学生.dat:

Smith,John Stevens,12456214,5,99,98,96,92,91,
Johnson,Chris,11058975,4,84,83,78,91,
abcd,abcd,11114444,4,100,100,100,98,
Stromberg,Noah,121518,4,25,50,75,100,
Hutch,Maya,815121,5,90,80,70,60,50,
c++ formatting iomanip
1个回答
0
投票

如何产生BAD输出:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

struct Student
{
   string name;
   string studentID;
   int numTests;
   vector<int> testScores;
};

int main()
{
    vector<Student> students;
    students.push_back( { "Smith, John Stevens", "12456214", 5, { 99, 98, 96, 92, 91 } } );
    students.push_back( { "\nJohnson, Chris", "11058975", 4, { 84, 83, 78, 91 } } );
    students.push_back( { "\nabcd,abcd", "11114444", 4, { 100, 100, 100, 98 } } );
    students.push_back( { "\nStromberg, Noah", "121678", 4, { 25, 50, 75, 100 } } );
    students.push_back( { "\nHutch, Maya", "8098121", 5, { 90, 80, 70, 60, 50 } } );
    int numStudents = students.size();

    for (int i = 0; i < numStudents; ++i)
    {
        cout << right << setw(30) << students[i].name 
             << setw(15) << students[i].studentID;
        for (int j = 0; j < students[i].numTests; ++j)
        {
            cout << right << setw(5) << students[i].testScores[j];
        }
        cout << endl;
    }

}

产生

           Smith, John Stevens       12456214   99   98   96   92   91
               
Johnson, Chris       11058975   84   83   78   91
                    
abcd,abcd       11114444  100  100  100   98
              
Stromberg, Noah         121678   25   50   75  100
                  
Hutch, Maya        8098121   90   80   70   60   50

所以你删除了' ' 在学生姓名的开头:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

struct Student
{
   string name;
   string studentID;
   int numTests;
   vector<int> testScores;
};

int main()
{
    vector<Student> students;
    students.push_back( { "Smith, John Stevens", "12456214", 5, { 99, 98, 96, 92, 91 } } );
    students.push_back( { "Johnson, Chris", "11058975", 4, { 84, 83, 78, 91 } } );
    students.push_back( { "abcd,abcd", "11114444", 4, { 100, 100, 100, 98 } } );
    students.push_back( { "Stromberg, Noah", "121678", 4, { 25, 50, 75, 100 } } );
    students.push_back( { "Hutch, Maya", "8098121", 5, { 90, 80, 70, 60, 50 } } );
    int numStudents = students.size();

    for (int i = 0; i < numStudents; ++i)
    {
        cout << right << setw(30) << students[i].name 
             << setw(15) << students[i].studentID;
        for (int j = 0; j < students[i].numTests; ++j)
        {
            cout << right << setw(5) << students[i].testScores[j];
        }
        cout << endl;
    }

}

你会得到漂亮的输出:

   Smith, John Stevens       12456214   99   98   96   92   91
        Johnson, Chris       11058975   84   83   78   91
             abcd,abcd       11114444  100  100  100   98
       Stromberg, Noah         121678   25   50   75  100
           Hutch, Maya        8098121   90   80   70   60   50
© www.soinside.com 2019 - 2024. All rights reserved.