如何从文件中读取学生信息,因为它无法显示

时间:2015-10-21 03:51:03

标签: c++ string file

我已经编写了这个程序,在另一个函数中读取id,函数名称和课程成绩信用,然后计算尝试的总学分,总学分,总成绩和总学分。 在我写完之后没有错误,但它显示了事情

#include<iostream>
#include <string> 
#include <fstream>
#include <iomanip>
#include <cmath>
#include<cstring>

using namespace std;

//functions prototype

int main() {
    //decleration
    ifstream inf;
    ofstream outf;
    int id;
    string name;
    string courseid, grade;
    int cridit;
    int tcattempted, tcearned, tcredits;
    double tgpoints;

    //opening file
    inf.open("students.txt");
    outf.open("report.txt");

    //check
    if (inf) {
        outf << "error, can not open file " << endl;
        return 1;
    }

    //header function
    header(outf);
    //read name and id function
    ReadIDAndName(inf, id, name);
    outf << id << "  " << name;

    outf << endl;

    //read course data function
    ReadCourseData(inf, courseid, grade, cridit);
    outf << "course" << "   " << "grade" << "   " << "credit" << endl;
    while (!inf.eof()) {
        outf << courseid << "   " << grade << "   " << cridit << endl;

    }

    // calculate function
    Calculate(grade, cridit, tcattempted, tcearned, tgpoints, tcredits);
    outf << "total credit attemted =" << tcattempted;
    outf << "total credit earned =" << tcearned;
    outf << "total grade point earned =" << tgpoints;
    outf << " grade point average =" << tcredits;

    //close file
    inf.close();
    outf.close();

    return 0;
}
//end of program


//function header

void header(ofstream& outf) {

    outf << setw(8) << "sultan gaboos university" << endl;
    outf << setw(8) << "student information sestem" << endl;
    outf << setw(8) << "contact show your id: show your name" << endl;
    outf << "****************************************************" << endl;



    return;
}

//function read name

void ReadIDAndName(ifstream& inf, int& id, string& name) {



    getline(inf, name, '/');
    inf >> id>>name;

    return;
}

//function read course data

bool ReadCourseData(ifstream& inf, string& courseid, string& grade, int& cridit) {
    bool exist = false;
    do {

        getline(inf, courseid, '/');
        inf >> courseid >> grade>>cridit;

    } while (exist);

    return true;
}

//function get grade point

double GetGradePoints(string grade) {
    double cradepoint;

    if (grade == "A")
        cradepoint = 4.0;

    else if (grade == "A-")
        cradepoint = 3.7;

    else if (grade == "B+")
        cradepoint = 3.3;

    else if (grade == "B")
        cradepoint = 3.0;

    else if (grade == "B-")
        cradepoint = 2.7;

    else if (grade == "C+")
        cradepoint = 2.3;

    else if (grade == "C")
        cradepoint = 2.0;

    else if (grade == "C-")
        cradepoint = 1.7;

    else if (grade == "D+")
        cradepoint = 1.3;

    else if (grade == "D")
        cradepoint = 1.0;

    else if (grade == "F")
        cradepoint = 0.0;

    else if (grade == "I")
        cradepoint = 0.0;

    else if (grade == "W")
        cradepoint = 0.0;

    else if (grade == "P")
        cradepoint = 0.0;
    else
        cout << "wrong " << endl;



    return cradepoint;
}

//function for calculations

void Calculate(string grade, int cridit, int& tcattempted, int& tcearned, double& tgpoints, int& tcredits) {


    ifstream inf;
    string courseid;
    double cradepoint;

    ReadCourseData(inf, courseid, grade, cridit);
    GetGradePoints(grade);

    int count = 0;

    do {
        for (int i = 0; i < count; i++) {
            tcattempted = cridit + count;
        }
    } while (cridit != 'I' && cridit != 'W');


    do {
        for (int i = 0; i < count; i++) {
            tcearned = cridit + count;
        }
    } while (cridit != 'I' && cridit != 'P' && cridit != 'F');



    do {
        for (int i = 0; i < count; i++) {
            tgpoints = (cridit * cradepoint) + count;
        }
    } while (cridit != 'P');


    if (cridit != 'p' && cridit != 'I' && cridit != 'W') {

        tcredits = tcearned / tcattempted;
    }
    return;
}

0 个答案:

没有答案