如何从文件中读取并存储变量?

时间:2015-03-30 18:43:35

标签: c++ file for-loop while-loop getline

我制作了一个程序来计算学生的测验平均值和班级平均值,同时使用循环来简化它并将数据存储到文件中(现在只是.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的平均成绩:平均成绩 等

班级平均分:

底部的当前While / for循环显示来自文件的正确信息,而不是正确的事情(学生ID之间没有间隙)

我真的需要它一次获得一行,平均一行,然后存储总数以便稍后平均。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

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 << " ";

答案 1 :(得分:0)

在评论“输出完成”

之后,我将以下代码添加到现有代码中
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