如何从文件中读取并存储变量?
How to read from a file and store variables?
我正在制作一个程序来计算学生的测验平均分和 class 平均分,同时使用循环来简化它并将数据存储到一个文件中(目前只是一个 .txt)。
我是 c++ 的新手,这可能是我迄今为止所做的最先进的事情...
我当前的代码是:
// 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;
}
虽然上半部分工作正常,并且像这样将信息存储在文本文件中
student id score, score, score
student id score, score, score
etc.
我不确定如何在底部进行 while 和 for 循环以大致提供我想要的输出:
学生 1 的平均分:平均分
学生 2 的平均水平:平均水平
等等
class平均:
底部的当前 While/for 循环显示文件中的正确信息,只是不正确(学生 ID 之间没有间隙)
我真的需要它一次获取一行,取平均数,然后存储总数以便以后取平均数。
感谢您的帮助。
int i = 0;
while (inputFile >> studentInfo)
{
cout << "Student " << i << "'s info is " << studentInfo << endl;
++i;
}
这是你要问的吗?
针对 OP 的评论进行了编辑。好的:所以您希望显示的信息是平均值。所以存储这个平均值是有意义的(如果这是你想要的)。您可以用以下代码替换 qz
上的循环,以实现此目的:
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 << " ";
我在您现有的代码中添加了以下代码,在评论后"Output Finished"
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
我正在制作一个程序来计算学生的测验平均分和 class 平均分,同时使用循环来简化它并将数据存储到一个文件中(目前只是一个 .txt)。
我是 c++ 的新手,这可能是我迄今为止所做的最先进的事情...
我当前的代码是:
// 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;
}
虽然上半部分工作正常,并且像这样将信息存储在文本文件中
student id score, score, score
student id score, score, score
etc.
我不确定如何在底部进行 while 和 for 循环以大致提供我想要的输出:
学生 1 的平均分:平均分 学生 2 的平均水平:平均水平 等等
class平均:
底部的当前 While/for 循环显示文件中的正确信息,只是不正确(学生 ID 之间没有间隙)
我真的需要它一次获取一行,取平均数,然后存储总数以便以后取平均数。
感谢您的帮助。
int i = 0;
while (inputFile >> studentInfo)
{
cout << "Student " << i << "'s info is " << studentInfo << endl;
++i;
}
这是你要问的吗?
针对 OP 的评论进行了编辑。好的:所以您希望显示的信息是平均值。所以存储这个平均值是有意义的(如果这是你想要的)。您可以用以下代码替换 qz
上的循环,以实现此目的:
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 << " ";
我在您现有的代码中添加了以下代码,在评论后"Output Finished"
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