从文本中读取数字并输出平均值

时间:2016-04-09 03:11:43

标签: c++ file loops if-statement while-loop

所以我正在努力从文件中读取文本(逐行)并输出IDID后平均4个等级和字母等级。因此,任何平均成绩为50或以上的字母等级为S,低于50的任何字母等级为U,而2个免除课程的成绩为字母等级I。 (如果超过或等于2,则为S或U)。

所以假设文件有数字:

42 50 51 57 52
48 90 -1 60 -1
40 46 -1 59 45
47 50 -1 49 50

输出应该如下所示:

ID=42 Avg=52.5 Grade=S
ID=48 Excused=2 Grade=I
ID=40 Avg=50.0 Grade=S
ID=47 Avg=49.7 Grade=U

类型等级数

S    U    I
2    1    1

This is the output I am receiving from my code

它正在读取所有数字,但我需要它将第一个数字读作ID并将4个数字作为等级读取。

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ifstream in;
    ofstream results;
    string filename;
    char grade;
    int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
    double allscores = 0;

    cout << "Enter the name of the file that has the scores of students: " << endl;
    cin >> filename;

    cout << "Enter the number of scores for each student: " << endl;
    cin >> ID;

    in.open(filename);
    results.open("results.txt");

    if (in)
    {
        while (in >> ID)
        {
            int first = 0

            for (i = 0; i<=4; i++)
            {
                if (first == -1)
                    excused++;
                else
                    allscores += first;
            }
            if (first > 4)
            {
                avg = allscores / (4 - excused);

                if (avg >= 50.0)
                {
                    grade = 'S';
                    S++;
                    cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;

                }
                else
                {
                    grade = 'U';
                    U++;
                    cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
            }

            else
            {
                grade = 'I';
                I++;
                cout << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
            }
        }
    }
    else
    {
        cout << "Couldn't open file\n";
    }

    cout << "Number of Grades of Type" << endl;
    cout << "S   " << "U   " << "I" << endl;
    cout << S << "   " << U << "   " << I << endl;

    in.close();
    results.close();

    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您的变量int first=0未在代码中分配0以外的任何值,因此语句if(first>4)将始终为false并导致您获得的输出。

您可以这样做:

    while (in >> ID)//this is supposed to get the ID of each student, in order for that to happen you need to read all the 4 scores inside this loop before coming here and reading the next ID,otherwise everything will read as an ID
    {
       int first = 0
       excused=0;//you need to reset excused on every iteration
       allscores=0;//you need to reset allscore on every iteration
       for (i = 0; i<4; i++)
       {//here you need to get all the 4 scores
           in>>first;//get each score in first
           if (first == -1)
             excused++;
           else
             allscores += first;
       }

       if(excused<2)//instead of if(first>4)
            ...//do your calculation for average score
       else
            ...//set the grade to 'I'...
    }

答案 1 :(得分:0)

这是我的最终解决方案:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ifstream in;
    ofstream results;
    string filename;
    char grade;
    int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
    double allscores = 0;

    cout << "Enter the name of the file that has the scores of students: " << endl;
    cin >> filename;

    cout << "Enter the number of scores for each student: " << endl;
    cin >> ID;

    in.open(filename);
    results.open("results.txt");

    if (in)
    {
        while (in >> ID)
        {
            int first = 0;
            excused = 0;
            allscores = 0;
            for (i = 0; i < 4; i++)
            {
                in >> first;
                if (first == -1)
                    excused++;
                else
                    allscores += first;
            }

            if (excused < 2)
            {
                avg = allscores / (4 - excused);

                if (avg >= 50.0)
                {
                    grade = 'S';
                    S++;
                    results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;

                }
                else
                {
                    grade = 'U';
                    U++;
                    results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
            }
            else
            {
                grade = 'I';
                I++;
                results << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
            }
        }
    }

    else
    {
        cout << "Couldn't open file\n";
    }

    results << "Number of Grades of Type" << endl;
    results << "S   " << "U   " << "I" << endl;
    results << S << "   " << U << "   " << I << endl;

    in.close();
    results.close();

    system("pause");
    return 0;
}

在代码之后,我将其输出到名为&#34的文件;结果&#34;。
谢谢您的帮助。我想我的while循环是最大的错误 特别是不添加in >> first部分。