gpt4 book ai didi

C++ 输出错误

转载 作者:行者123 更新时间:2023-11-28 01:02:11 24 4
gpt4 key购买 nike

感谢你们,这个程序的输出是固定的。除了学号。我读到评论说我从未为其设定值(value),这让我感到困惑。

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; 会给您带来错误的原因。

我将让您自己解决格式问题。您应该考虑阅读输出或至少进行一些试验和错误。

关于C++ 输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com