C ++输出错误

时间:2011-11-15 22:31:29

标签: c++

感谢你们,这个程序的输出是固定的。除了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;
}

4 个答案:

答案 0 :(得分:2)

好吧,studentNumber是垃圾,因为你从来没有把价值放进去。所以它只是已经发生在那个位置的内存中。

考试成绩打印错误,因为C ++中的逗号不会按照您的想法执行,这也是为什么添加endl;会给您带来错误的原因。

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

答案 1 :(得分:2)

其中一个错误是代替:

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

我认为你的意思是:

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

答案 2 :(得分:0)

CLASS_SIZE定义为5,因此这个循环:

while (thisStudent++ < CLASS_SIZE)

将迭代 6 次。

另外

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

这会输出exam1,然后对其余变量进行评估并不执行任何操作。

答案 3 :(得分:-1)

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;会将空间加载到其中一个考试变量中。

- 编辑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