我得到了这个程序来计算相应字母等级的等级,然后我按照用户想要输出的次数循环。但是,由于某种原因,它只将最后已知的输出写入其文本文件。谁能告诉我这里做错了什么?
#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;
}
}
答案 0 :(得分:1)
编辑:这里更严重的问题是你只存储最后一个输入。您应该创建一个对象来存储每个学生的所有数据(例如Student对象),创建一个学生数组,并在完成所有输入后循环遍历该数组以打印信息。我已经将下面的代码更新为对象数组的样子。
如果您不了解任何面向对象的编程概念,您还可以将每个数据(名称,字母等级,平均值等)放在一个数组中,其中每个数据中的第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();
}
答案 1 :(得分:0)
如果你试图输出我建议的很多东西
在循环完成输出变量后,迭代循环将预期输出添加到变量 实施例
var output
while(true) {
add to output
}
print output