C++ 如何从循环中写入多个输出?
我让这个程序计算等级到它对应的字母等级,我让它循环用户想要输出的次数。但是,由于某些原因,它只将最后一次已知的输出写入其文本文件。有人能告诉我我做错了什么吗C++ 如何从循环中写入多个输出?,c++,file,loops,text,C++,File,Loops,Text,我让这个程序计算等级到它对应的字母等级,我让它循环用户想要输出的次数。但是,由于某些原因,它只将最后一次已知的输出写入其文本文件。有人能告诉我我做错了什么吗 #include <fstream> #include <iostream> #include <iomanip> #include <string> #include <cstdlib> int weighted1 = 0; int weighted2 = 0; int wei
#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;
}
}
#包括
#包括
#包括
#包括
#包括
int-weighted1=0;
int-weighted2=0;
int-weighted3=0;
int加权4=0;
整数加权平均=0;
常数int MAX=20;
int标志=0;
智力选择;
双和=0;
双平均=0;
字符名[10];
字符字母;
字符文件[MAX];
int num=0;
整数计数=0;
int main()
{
//启动输入/输出流
std::ifstream在_流中;
std::流出流的流量;
in_stream.open(“grade.txt”);
外流开放(“最终交易日”);
双第一,二,三,四;
在\u流中>>第一个>>第二个>>第三个>>第四个>>名称;
//标准::cout>>“1:”>>第一个>>“2:”>>第二个>>
双级=0.0;
年级=(一年级+二年级+三年级+四年级)/4;
//为用户提供从键盘或文件读取学生记录的选择
bool菜单=真;
while(菜单!=false)
{
标准:cout 100)
{
标准:cout 100)
{
标准:cout 100)
{
标准:cout 100)
{
如果你想输出很多东西,我建议你
循环完成该变量的输出后,通过循环将预期输出添加到变量中进行迭代
范例
编辑:这里更严重的问题是您只存储最后的输入。您应该创建一个对象来存储每个学生的所有数据(例如,一个学生对象),创建一个学生数组,并在完成所有输入后循环该数组以打印信息。我已将下面的代码更新为对象数组的外观
如果您不知道任何面向对象的编程概念,还可以将每个数据段(名称、字母等级、平均值等)放入在一个数组中,每个数组中的第0个元素都代表一个学生,第1个元素代表另一个学生,等等。这不是一个好做法;最好创建一个对象来存储关于学生的信息
原始:在循环的每个迭代中打开和关闭文件,从而覆盖文件,而不是附加到文件
相反,在循环之前打开文件,然后关闭,如下所示:
{
//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();
}
{
//将成绩写入grades.txt
std::流myfile;
myfile.open(“grades.txt”);
对于(计数=0;计数 myfile尝试在for循环之外打开和关闭该文件。我怀疑在每次这样的迭代中重新打开该文件时,该文件会被覆盖而不是追加。@Michelle为什么不将其作为实际答案发布,以便我可以向上投票?这有助于多次写入该行,但现在它重复了最后已知的输入。我将尝试看看原因是。哦,我刚刚意识到您根本没有使用count
的值。@您好,注意while循环中也有return语句,这意味着您的循环只执行一次。您可能想将其下移一行。
{
//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();
}