0

我得到这个程序来计算其对应字母等级的等级,并且我让它循环用户想要输出的次数。但是,由于某种原因,它只将最后一个已知输出写入其文本文件。谁能告诉我我在这里做错了什么?

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

int weighted1 = 0;
int weighted2 = 0;
int weighted3 = 0;
int weighted4 = 0;
int weighted_average = 0;

const int MAX = 20;

int flag = 0;
int choice;
double sum = 0;
double average = 0;
char name [10];
char letter;
char file [MAX];

int num = 0;
int count = 0;

int main( )
{

//Initiate input/output stream
std::ifstream in_stream;
std::ofstream out_stream;

in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");

double first, second, third, fourth;
in_stream >> first >> second >> third >> fourth >> name;
//std::cout >> " 1: " >> first >> " 2: " >> second >> 
double grade = 0.0;
grade = (first + second + third + fourth)/4;

//Gives user the choice of reading student records from keyboard or file
bool menu = true;
while (menu != false)
{
      std::cout << "Would you like to open as keyboard or file?" << '\n';
std::cout << "1. keyboard" << '\n';
std::cout << "2. file" << '\n';

std::cin >> choice;

switch (choice)
{
//Enter the number students the grades will enter
case 1:
std::cout << "How many students? ";
std::cin >> num;
for(count =0; count < num; count++) 
{
    {
    std::cout << "Student's Name: ";
    std::cin >> name;
    }
do
{
flag = 0;
std::cout << "Please input your first exam grade and press enter: \n";
    std::cin >> first;
    if ((first < 0) || (first > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your second exam grade and press enter: \n";
    std::cin >> second;
    if ((second < 0) || (second > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your third exam grade and press enter: \n";
    std::cin >> third;
    if ((third < 0) || (third > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }
}while (flag == 1);

do
{
flag = 0;
std::cout << "Please input your final exam grade and press enter: \n";
    std::cin >> fourth;
    if ((fourth < 0) || (fourth > 100))

    {
        std::cout << "You've entered invalid data!" << '\n';
        flag = 1;
    }

}while (flag == 1);

//Formulas that calculate student average
grade = (first + second + third + fourth)/4;
sum = first + second + third + fourth;
average = sum/4;

//Letter grade and it's weighted averages
 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(grade >= 90)
 { 
 letter = ('A');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 80)
 {
 letter = ('B');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 70)
 {
 letter = ('C');
 std::cout<<letter<<'\n';
 }
 else if(grade >= 60)
 {
 letter = ('D');
 std::cout<<letter<<'\n';
 }
 else if (grade < 60)
 {
 letter = ('F');
 std::cout<<letter<<'\n';
 }
 weighted1 = (first  * .20);
 weighted2 = (second * .20);
 weighted3 = (third  * .20);
 weighted4 = (fourth * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

 //Output
 std::cout << "Exam Grades: " <<  first << "," << second << "," << third << "," << fourth << '\n';
 std::cout << "This is the average for " << name << ": " << weighted_average << '\n';
 std::cout << "This is the letter grade: " << letter << '\n';
  }
  {
//Writing the grade into grades.txt
  for(count =0; count < num; count++)   
  {
    std::ofstream myfile;
    myfile.open ("grades.txt");
    myfile << "Writing this to a file: ";
    myfile << name << ' ';
    myfile << weighted_average << ' ';
    myfile << letter << '\n';
    myfile << "****";
    myfile.close();

  }
  break;
  }

//Here we open "grade.txt" to output grade to screen
case 2:

in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");

 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(grade >= 90)
 letter = ('A');

 else if(grade >= 80)
 letter = ('B');

 else if(grade >= 70)
 letter = ('C');

 else if(grade >= 60)
 letter = ('D');

 else if (grade < 60)
 letter = ('F');

 weighted1 = (first  * .20);
 weighted2 = (second * .20);
 weighted3 = (third  * .20);
 weighted4 = (fourth * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

std::cout << "Enter file name: ";
     std::cin >> file;
if(file != "grade.txt")
{
     std::cout << std::fixed << "The average grade for: " << name << '\n';
     std::cout << "average in grade.txt is: "<< weighted_average << std::setprecision(2) << '\n';
     std::cout << "and the letter grade is: " << letter << '\n'; 
}
else
{
    return 0;
}

in_stream.close();
out_stream.close();
}

return 0;
}
}
4

2 回答 2

1

编辑:这里更严重的问题是您只存储最后一个输入。您应该创建一个对象来存储每个学生的所有数据(例如一个学生对象),创建一个学生数组,并在获得所有输入后循环遍历该数组以打印信息。我已将下面的代码更新为对象数组的样子。

如果您不了解任何面向对象的编程概念,您还可以将每条数据(姓名、字母等级、平均数等)放入一个数组中,其中每个元素中的第 0 个元素都代表一个学生,第一个将代表另一个,等等。这不是好的做法;创建一个对象来存储有关学生的信息是一个更好的主意。

原文:您正在覆盖您的文件,而不是通过在循环的每次迭代中打开和关闭它来附加到它。

相反,在循环之前打开文件并在之后关闭它,如下所示:

{
  //Writing the grade into grades.txt
  std::ofstream myfile;
  myfile.open ("grades.txt");
  for(count =0; count < num; count++)   
  {
    myfile << "Writing this to a file: ";
    myfile << students[count].name << ' ';
    myfile << students[count].weighted_average << ' ';
    myfile << students[count].letter << '\n';
    myfile << "****";
  }
  myfile.close();
}
于 2013-08-02T17:17:37.843 回答
0

如果你试图输出很多东西,我建议你

在循环完成输出该变量示例后,遍历循环将您的预期输出添加到变量中

var output

 while(true) {
     add to output
 }

 print output
于 2013-08-02T16:51:02.403 回答