使用结构和ifstream创建成绩簿

时间:2014-03-02 21:34:46

标签: c++

好的,所以我必须编写一个程序,当用户输入"load filename"时,其中"filename"是您要打开的文件的象征(例如"input.txt")。这将读入所有数据并存储学生的姓名,成绩和计算平均值。然后,用户可以输入"display filename",其中将显示学生的姓氏,名字以及他们在出勤,家庭作业,项目,期中考试和期末考试中的所有分数。输入文件将始终具有相同的前5行,它们看起来像这样:

    Attendance: 5
    Midterm: 20
    Final: 20
    Homework: 15
    Projects: 40
    Henry, Patrick
    Attendance: 12 15
    Midterm: 80 100
    Homework: 50 100
    Homework: 60 100
    Homework: 80 100
    Project: 90 100
    Project: 80 100
    Project: 75 100
    Final: 80 100

第一个数字是学生收到的,第二个是他们可能得分的数字。家庭作业和项目总是超过100分,但是你不知道每个人有多少。但是,您将拥有其他类别之一。最后一个类别总是最终的,可能会或可能不会被另一个学生跟随。学生人数不会超过100人。我写了这段代码:

int main()
{
    int numStudents = 0;
    int projCount = 0;
    int hwCount = 0;
    student students[100];
    string command, filename;
    cout << "What do you want to do?" << endl;
    cin >> command;
    ifstream in ;
    cin >> filename;
    string Attendance, Midterm, Final, Homework, Projects;
    int A, M, F, H, P;
    while (!in.fail())
    {
        if (command == "Load"
         || command == "load"
         || command == "l")
        {
            in .open(filename);

            in >> Attendance >> A
               >> Midterm >> M 
               >> Final >> F
               >> Homework >> H
               >> Projects >> P;

            string lname, fname;
            in.ignore(200, '\n');
            getline(in, lname, ',');
            getline(in, fname, '\n');

            while (!in.fail())
            {
                string cat;
                int score, possible;
                in >> cat;
                in >> score >> possible;
                while (cat != "Final:")
                {
                    students[numStudents].last = lname;
                    students[numStudents].first = fname;
                    if (cat == "Attendance:")
                    {
                        students[numStudents].attendpos = possible;
                        students[numStudents].attendgot = score;
                        students[numStudents].attgrade = (score * A) / possible;
                    }
                    else if (cat == "Midterm:")
                    {
                        students[numStudents].midpos = possible;
                        students[numStudents].midgot = score;
                        students[numStudents].midgrade = (score * M) / possible;
                    }
                    else if (cat == "Homework:")
                    {
                        students[numStudents].hw[hwCount] = score;
                        hwCount++;
                        students[numStudents].hwgrade = (score * H) / 100;
                    }
                    else if (cat == "Project:")
                    {
                        students[numStudents].proj[projCount] = score;
                        projCount++;
                        students[numStudents].projgrade = (score * P) / 100;
                    }
                    in >> cat; in >> score >> possible;
                }
                in >> score >> possible;
                students[numStudents].finpos = possible;
                students[numStudents].fingot = score;
                students[numStudents].fingrade = (score * F) / possible;
                students[numStudents].average = students[numStudents].fingrade + students[numStudents].hwgrade + students[numStudents].projgrade + students[numStudents].attgrade + students[numStudents].midgrade;
                numStudents++; in .ignore(200, '\n');
                getline(in, lname, ',');
                getline(in, fname, '\n');
            }
            for (int i = 0; i < numStudents; i++)
            {
                cout << students[i].average << endl;
            }
        }
        else if (command == "Display" || command == "display" || command == "d")
        {
            cout << "Displayed:  " << filename << endl;
            cout << "#Students:" << numStudents << endl;
            cout << "Last Name" << "   First Name" << "   Attendance";
            for (int i = 0; i < hwCount; i++)
            {
                cout << "HW" << i << "   ";
            }
            for (int i = 0; i < projCount; i++)
            {
                cout << "Proj" << i << "   ";
            }
            cout << "   Midterm";
            cout << "   Final" << endl;
            for (int i = 0; i < numStudents; i++)
            {
                cout << students[i].last << " " << students[i].first << " " << students[i].attendgot;
                int j = 0;
                while (j < numStudents)
                {
                    for (int i = 0; i < hwCount; i++)
                    {
                        cout << students[j].hw[hwCount] << "   ";
                    }
                    for (int i = 0; i < projCount; i++)
                    {
                        cout << students[i].proj[projCount] << "   ";
                    }
                    j++;
                }
                cout << students[i].midgot << "   " << students[i].fingot << "   " << students[i].average << endl;

            }
        }
        else if (command == "Sort" || command == "sort" || command == "s")
        {
            cout << "A" << endl;
        }
        else
        {
            cout << "I didn't recognize that" << endl; in >> command;
        }
    }
}

这是我的头文件:

struct student
{
    string last;
    string first;
    double attgrade;
    double midgrade;
    double hwgrade;
    double projgrade;
    double fingrade;
    int attendpos;
    int attendgot;
    int midpos;
    int midgot;
    int finpos;
    int fingot;
    int hw[20];
    int proj[20];
    double average;
};

然而,我遇到的问题是,在您输入"load filename"之后代码停止运行而我不知道原因。另外,我还没有完成排序部分所以不要担心其他if语句。我包含了所有必要的库。但是,为什么在输入第一个命令后程序会停止?

编辑: 除此之外,它还仅出于某种原因存储一个学生的信息。请帮帮我!

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经尝试过你的程序了。 我怀疑它只是没有打开所需的输入文件。 尝试在in.open之后添加以下内容(...

if(in.good())
{
cout << "file opened"<<endl;
}
else
{
cout << "file not opened"<<endl;
}

我使用msvc2008编译了以下内容...并使用调试器确认了您的预期程序行为。

// test1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct student
{
    string last;
    string first;
    double attgrade;
    double midgrade;
    double hwgrade;
    double projgrade;
    double fingrade;
    int attendpos;
    int attendgot;
    int midpos;
    int midgot;
    int finpos;
    int fingot;
    int hw[20];
    int proj[20];
    double average;
};

int main(int argc, char* argv[])
{

    int numStudents = 0;
    int projCount = 0;
    int hwCount = 0;
    student students[100];
    string command, filename;
    cout << "What do you want to do?" << endl;
    cin >> command;
    ifstream in ;
    cin >> filename;
    string Attendance, Midterm, Final, Homework, Projects;
    int A, M, F, H, P;
    while (! in .fail())
    {
        if (command == "Load" || command == "load" || command == "l")
        { in.open(filename.c_str()); 
        if(in.good())
        {
                cout << "file opened"<<endl;
        }
                    else 
                    {
                    cout << "file opened"<<endl;
                    }
          in >> Attendance ;
          in >> A ;
          in >> Midterm >> M >> Final >> F >> Homework >> H >> Projects >> P;
            string lname, fname; in .ignore(200, '\n');
            getline( in , lname, ',');
            getline( in , fname, '\n');
            while (! in .fail())
            {
                string cat;
                int score, possible; in >> cat; in >> score >> possible;
                while (cat != "Final:")
                {
                    students[numStudents].last = lname;
                    students[numStudents].first = fname;
                    if (cat == "Attendance:")
                    {
                        students[numStudents].attendpos = possible;
                        students[numStudents].attendgot = score;
                        students[numStudents].attgrade = (score * A) / possible;
                    }
                    else if (cat == "Midterm:")
                    {
                        students[numStudents].midpos = possible;
                        students[numStudents].midgot = score;
                        students[numStudents].midgrade = (score * M) / possible;
                    }
                    else if (cat == "Homework:")
                    {
                        students[numStudents].hw[hwCount] = score;
                        hwCount++;
                        students[numStudents].hwgrade = (score * H) / 100;
                    }
                    else if (cat == "Project:")
                    {
                        students[numStudents].proj[projCount] = score;
                        projCount++;
                        students[numStudents].projgrade = (score * P) / 100;
                    } in >> cat; in >> score >> possible;
                } in >> score >> possible;
                students[numStudents].finpos = possible;
                students[numStudents].fingot = score;
                students[numStudents].fingrade = (score * F) / possible;
                students[numStudents].average = students[numStudents].fingrade + students[numStudents].hwgrade + students[numStudents].projgrade + students[numStudents].attgrade + students[numStudents].midgrade;
                numStudents++; in .ignore(200, '\n');
                getline( in , lname, ',');
                getline( in , fname, '\n');
            }
            for (int i = 0; i < numStudents; i++)
            {
                cout << students[i].average << endl;
            }
        }
        else if (command == "Display" || command == "display" || command == "d")
        {
            cout << "Displayed:  " << filename << endl;
            cout << "#Students:" << numStudents << endl;
            cout << "Last Name" << "   First Name" << "   Attendance";
            for (int i = 0; i < hwCount; i++)
            {
                cout << "HW" << i << "   ";
            }
            for (int i = 0; i < projCount; i++)
            {
                cout << "Proj" << i << "   ";
            }
            cout << "   Midterm";
            cout << "   Final" << endl;
            for (int i = 0; i < numStudents; i++)
            {
                cout << students[i].last << " " << students[i].first << " " << students[i].attendgot;
                int j = 0;
                while (j < numStudents)
                {
                    for (int i = 0; i < hwCount; i++)
                    {
                        cout << students[j].hw[hwCount] << "   ";
                    }
                    for (int i = 0; i < projCount; i++)
                    {
                        cout << students[i].proj[projCount] << "   ";
                    }
                    j++;
                }
                cout << students[i].midgot << "   " << students[i].fingot << "   " << students[i].average << endl;

            }
        }
        else if (command == "Sort" || command == "sort" || command == "s")
        {
            cout << "A" << endl;
        }
        else
        {
            cout << "I didn't recognize that" << endl; in >> command;
        }
    }
}