C++ 如何读取文件并存储变量?
我正在制作一个程序来计算学生的测验平均数和班级平均数,同时使用一个循环来简化它,并将数据存储到一个文件中(现在只需一个.txt文件) <我是C++的新手,这可能是我至今为止做过的最先进的事情……/P> 我目前的代码是:C++ 如何读取文件并存储变量?,c++,file,for-loop,while-loop,getline,C++,File,For Loop,While Loop,Getline,我正在制作一个程序来计算学生的测验平均数和班级平均数,同时使用一个循环来简化它,并将数据存储到一个文件中(现在只需一个.txt文件) 我目前的代码是: // Grade Average calculator #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main(){ ofstre
// Grade Average calculator
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
ofstream outputFile;
ifstream inputFile;
int continueYes;
double qz, score;
string studentId;
//Open File
outputFile.open("quizav.txt");
//Continue to start...
cout << "Press 1 to add a student, Press 0 to exit.\n";
cin >> continueYes;
while (continueYes == 1){
cout << "Please Enter student ID: ";
cin >> studentId;
outputFile << studentId << " ";
for (qz = 1; qz < 5; qz++){
cout << "Enter quiz score " << qz << " ";
cin >> score;
+-score;
outputFile << " " << score << " ";
}
cout << "Press 1 to add a student, press 0 if no more.\n";
cin >> continueYes;
outputFile << " " << endl;
}
outputFile.close();
//OUT PUT FINISHED
double average;
double student, studentInfo;
inputFile.open("quizav.txt");
while (inputFile >> studentInfo){
cout << studentInfo << endl;
}
system("pause");
return 0;
}
我不确定如何在底部生成while和for循环,以大致提供所需的输出:
学生1的平均分:平均分
学生2的平均分:平均分
等
班级平均成绩:
底部的当前While/for循环显示文件中正确的信息,只是不正确(学生ID之间没有间隙)
我真的需要它一次得到一行,求平均值,然后将总数存储到以后的平均值
谢谢您的帮助。在注释“输出完成”之后,我在您现有的代码中添加了以下代码
int i = 0;
while (inputFile >> studentInfo)
{
cout << "Student " << i << "'s info is " << studentInfo << endl;
++i;
}
double studentInfo;
open(“quizav.txt”);
int count=0,numstudents=0,numquizzes=4;
双倍学生分数=0,学生平均分数=0,总平均分数=0;
你是否已经用一行一行地调试了阅读部分?您可能更希望使用std::getline()
首先读取整个记录,然后分割其中感兴趣的值(例如,使用std::istringstream
)。我尝试使用getline,但它与我使用它的方式不兼容,您可以使用其中一个示例吗?它不需要非常具体。你有没有试过看太多类似的问题?试试这个搜索:当然,我手头有一些问答,这可能会让你掌握如何处理这条线。请看。这两个示例都使用std::getline()
和std::istringstream
。更接近!是的,这是我所需要的,但是我需要弄清楚如何让它显示平均值,而不仅仅是分数。其中一个示例输出是学生1的信息:(学生id)学生2的信息(第一次测验分数)等@AceEbert“如何让它显示平均值”您需要逐个解析值以构建平均值,直到行尾,然后计算所有值的平均值。然后显示结果。@WillBriggs不,这不完全是OP要求的,从文件格式中读取的分数不止一个,平均值应该根据每个学生计算。没错,分数不止一个,我以前做过类似的事情,但从来没有这样做过。我已经做了很多研究,但似乎没有任何效果…请参见上面的答案,以获得关于平均值问题的答案。
int i = 0;
while (inputFile >> studentInfo)
{
cout << "Student " << i << "'s info is " << studentInfo << endl;
++i;
}
double totalScore = 0.0;
enum {NUM_QUIZZES = 5};
//read in 5 quizzes and get the total score
for (qz = 1; qz < 5; qz++)
{
cout << "Enter quiz score " << qz << " ";
cin >> score;
totalScore += score;
}
//print the average score to a file
outputFile << " " << totalScore/NUM_QUIZZES << " ";
double studentInfo;
inputFile.open("quizav.txt");
int count = 0, numstudents = 0, numquizzes = 4;
double studentScores = 0, studentAvg = 0, totalAvg = 0;
cout << endl << "Class Statistics" << endl << endl;
while (inputFile >> studentInfo){
if(count == 0){ //if count == 0, then studentInfo contains the ID
++numstudents; //increment number of students
cout << "Current Student ID: " << studentInfo << endl; //output student ID
}
else{ //else, studentInfo contains a quiz score
studentScores += studentInfo; //total up cur student quiz scores
}
++count; //increment counter to keep track of which data your're looking at
if(count == numquizzes + 1){ //if counter = number of quizzes + 1 for student ID, current student is now complete
studentAvg = studentScores / numquizzes; //compute the average for student
totalAvg += studentAvg; //add average to total for later class avg calculation
studentScores = 0; //reset scores for next student
cout << " Student Average: " << studentAvg << endl; //output current student average
count = 0; //reset counter
}
}
totalAvg = totalAvg/numstudents; //when all students are evaluated, calc total average
cout << endl << "Class Average: " << totalAvg << endl;
inputFile.close();
return 0;
Class Statistics
Current Student ID: 123
Student Average: 5
Current Student ID: 234
Student Average: 4.25
Current Student ID: 568
Student Average: 4
Class Average: 4.41667