c++输出错误

C++ Output Errors

本文关键字:错误 输出 c++      更新时间:2023-10-16

由于你们的缘故,这个程序的输出是固定的。除了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 forn"
        << "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 << "nnThanks 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 << "naInvalid 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 << "nnThe 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;给你一个错误。

格式我将让你自己计算。您应该考虑仔细阅读输出,或者至少做一些试验和错误。

其中一个错误是:

cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;

我想你是这个意思:

cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;

CLASS_SIZE被定义为5,所以这个循环:

while (thisStudent++ < CLASS_SIZE)

将迭代6次

cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;

这将输出exam1,然后对其余变量求值且不做任何操作。

70 80 90 95 95 85 90 80 75 85 70 80 55 85 50 70 45 50 40 35

有空格吗?如果是,请忽略它们。input >> exam1 >> exam2 >> exam3 >> exam4;将把空间加载到一个考试变量中。

—edit for MooingDuck—

#include <iostream>
#include <sstream>
using namespace std;
int main() {
    cout << "main() ENTRY" << endl;
    stringstream s1(ios_base::in | ios_base::out),
                 s2(ios_base::in | ios_base::out);
    int i = -1;
    s1 << "111 222";
    s1 >> i; cout << i << endl;
    s1 >> i; cout << i << endl;
    s2 << "111 222";
    s2 >> noskipws;
    s2 >> i; cout << i << endl;
    s2 >> i; cout << i << endl;
    return 0;
}
输出:

main() ENTRY
111
222
111
0