谢谢大家,该程序的输出是固定的。除了studentNumber。我读到的评论是我从来没有给它定值,这让我感到困惑。

void process_file(ifstream& input)
    {
     int thisStudent = 0;

     StudentRecord student = StudentRecord();
        while (thisStudent++ < CLASS_SIZE)
        {
         student.input(input, thisStudent);
            student.computeGrade();
                student.output();
            }
        }


会不会将studentNumber设置为等于0,然后在每次循环中添加+1。

 // Author:
// Assignment 8
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

ofstream     outputfile("output.txt");

const int    MAX_FILE_NAME = 35;
const int    CLASS_SIZE = 5;

class StudentRecord
{
public:
void input( ifstream& input,int studentid);

void computeGrade();

void output();

private:
  int studentNumber;
  double exam1;
  double exam2;
  double exam3;
  double exam4;
  double average;
  char grade;
};


void open_input(ifstream& input, char name[]);
void process_file(ifstream& input);

int main()
{  char again;
   char file_name[MAX_FILE_NAME + 1];
   ifstream input_numbers;

   cout << "This program can calculate the exam average and grade for\n"
        << "each student.\n" << endl;
   system("pause");

   do
   {
      system("cls");
      open_input(input_numbers, file_name);
      process_file(input_numbers);
      input_numbers.close();

      cout << "\nDo you want to process another file (Y/N)? ";
      cin >> again;
      cin.ignore(256, '\n');

   } while ( again == 'y' || again == 'Y');

   cout << "\nEnd of Program!" << endl;
   outputfile << "\n\nThanks for using GradeCalc!\f";
   outputfile.close();

   return 0;
}

void process_file(ifstream& input)
{
 int thisStudent = 0;

 StudentRecord student = StudentRecord();
    while (thisStudent++ < CLASS_SIZE)
    {
     student.input(input, thisStudent);
        student.computeGrade();
        student.output();
    }
}

void open_input(ifstream& input, char name[])
{  int count = 0;
   do
   {  count++;
      if (count != 1)
      {  cout << "\n\aInvalid file name or file does not exist. Please try again."
              << endl;
      }
      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(256, '\n');
      input.clear();
      input.open(name,ios_base::in);
    } while (input.fail() );
}

void StudentRecord::input(ifstream& input, int studentid)
{
      input >> exam1 >> exam2 >> exam3 >> exam4;

}

void StudentRecord::computeGrade()
{
    average = (exam1 + exam2 + exam3 + exam4) / 4 ;
      if (average >= 90)
        grade = 'A';
else if (average >=  80)
  grade = 'B';
else if (average >= 70)
  grade = 'C';
else if (average >= 60)
  grade = 'D';
else if (average < 60)
  grade = 'F';
}

void StudentRecord::output()
{
   cout << "\n\nThe record for student number:" << setw(8) << studentNumber << endl;
   cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
   cout << "The numeric average is:" << setw(8) << average << endl;
   cout << "and the letter grade assigned is:" << setw(8) << grade << endl;
}

最佳答案

好吧,studentNumber是垃圾,因为您从未在其中输入任何值。因此,该位置上已经发生了任何事情。

考试成绩打印错了,因为C ++中的逗号没有按照您的想象做,这也是为什么在其中加上endl;会导致错误的原因。

我将让您自己进行格式化。您应该考虑阅读输出或至少进行一些反复试验。

10-07 19:25
查看更多