0

我是 stackoverflow 社区的新手,也是编码的新手,所以在我了解在这里发帖的技巧以及编码规则时提前道歉。我正在使用 C++,并且正在参加 CS161 初学者计算机科学课程。

我目前正在做一项作业,该作业要求我从存储在计算机上的数据文件中读取数据并对数据进行排序以进行一些计算,在此作业中,根据性别和学校类型查找平均考试成绩。一切都编译并且程序运行但是有一些问题。

第一个问题是我的回声。

    // echo the data file
    while (inData)
    {
    inData >> name >> sex >> school >> score;
    cout << name << sex << school << score << endl;

该程序确实回显了数据,但由于某种原因,它最终回显了列表中的姓氏两次。此外,(我不知道这是否重要)当它回显时,它不会跳过姓名、性别、学校和分数之间的空格。

第二个问题是它没有执行计算,我认为这是因为我错过了某种与“计数”相关的指令,但我无法做到。

这是我的代码,让我知道你的想法:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
    //Declare variables to manipulate data
    char sex;
    string name;
    string school;
    string fileSource;
    string CC;
    string UN;
    int maleScore = 0;
    int femScore = 0;
    int unScore = 0;
    int ccScore = 0;
    double maleAvg;
    double femAvg;
    double unAvg;
    double ccAvg;
    double sumAvg = 0;
    int femCount = 0;
    int maleCount = 0;
    int ccCount = 0;
    int unCount = 0;
    int score;
    int sum;
    //Declare stream variables
    ifstream inData;
    ofstream outData;
    inData >> name >> sex >> school >> score;
    // Promt user for file location
    cout << "Please input file location: ";
    cin >> fileSource;

    // open output file and run program exit failsafe command
    inData.open(fileSource);
    if (!inData)
    {
        cout << "Cannot open input file. "
            << "Program will now terminate." << endl;
        return 1;
    }
    outData << fixed << showpoint << setprecision(2);

    // echo the data file
    while (inData)
    {
        inData >> name >> sex >> school >> score;
        cout << name << sex << school << score << endl;

        // while reading incoming data from file, execute the conditions

        // Male and female calculations
        if(sex=='M')
        {
            maleScore = maleScore +=score;
            ++maleCount;
        }
        else if(sex =='F')
        {
            femScore = femScore +=score;
            ++femCount;
        }

        // Community college and University calculations
        if(school == CC)
        {
            ccScore = ccScore +=score;
            ++ccCount;
        }
        else if(school == UN)
        {
            unScore = unScore +=score;
            ++unCount;
        }
        maleAvg = maleScore/maleCount;
    }

    // Male average output
    cout << maleAvg;

    femAvg = femScore/femCount;

    // Female average output
    cout << femAvg;

    ccAvg = ccScore/ccCount;

    // Community College average output
    cout << ccAvg;

    unAvg = unScore/unCount;

    // University average output
    cout << unAvg;
    sum = maleScore + femScore + ccScore + unScore;
    sumAvg = sum/12;
    cout << sumAvg;
    return 0;
}

此外,我的编译器继续运行程序并且不会停止。我拍了一张编译器窗口的照片,但不知道如何发布。

4

1 回答 1

1

您的代码有一些问题。让我们一次一件地处理,好吗?

ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;

最后一行是做什么的?inData尚未打开,但您正在尝试从中读取。

maleScore = maleScore +=score;

在这里(以及其他一些地方)您+=错误地使用了运算符。您应该+=像这样使用:

maleScore += score;

+像这样使用:

maleScore = maleScore + score;

然后你这样做:

if(school == CC)

现在,CCstd::string您的代码中的一个,您尚未初始化(这意味着它是空的)。所以它永远不会匹配,因此if身体永远不会执行。同样的事情发生在UN.

再往下,你在循环中有这条线:

maleAvg = maleScore/maleCount;

这样做是在每次循环中重新计算男性的平均值。这不一定是错误的(你会得到正确的结果),但如果文件中的第一个人是女性,你的程序将因除以零而崩溃,因为maleCount它将是 0。如果没有女性在,也会发生同样的事情输入文件,或者如果没有大学或学院的分数。

最后,iostream::eof()在循环内使用不是一个好主意。您可以在 StackOverflow 上阅读更多相关信息:为什么循环条件内的 iostream::eof 被认为是错误的?

尽管如此,你的错误并不严重,而且是新程序员的典型错误,所以不要气馁。将此视为学习和了解如何检查代码以查找此类错误的机会。所以事不宜迟,让我们看看这个程序的改进版本:

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

using namespace std; // it's considered bad to do this - but since
                     // this is homework, we'll let it slide.

int main()
{
    //Declare variables to manipulate data    
    string name;
    string fileSource;

    int maleScore = 0;
    int femScore = 0;
    int unScore = 0;
    int ccScore = 0;
    int femCount = 0;
    int maleCount = 0;
    int ccCount = 0;
    int unCount = 0;

    //Declare stream variables
    ifstream inData;
    ofstream outData;

    // Promt user for file location
    cout << "Please input file location: ";
    cin >> fileSource;

    // open output file and run program exit failsafe command
    inData.open(fileSource);

    if(!inData)
    {
        cout << "Cannot open input file. "
             << "Program will now terminate." << endl;
        return 1;
    }    

    cout << "Reading data from '" << fileSource << "'" << endl;

    while(inData >> name)
    { // If we read a name, we can continue. Otherwise, we're done.     
        char sex;       
        int score;
        string school;

        inData >> sex >> school >> score;

        // Write the data out
        cout << "Processing " << name << " (" << sex << ") attending ";

        if(school == "UN")
            cout << "University";
        else if(school == "CC")
            cout << "Community College";

        cout << ". Score = " << score << endl;

        // Male and female calculations
        if(sex=='M')
        {
            maleScore +=score;
            maleCount++;
        }
        else if(sex =='F')
        {
            femScore +=score;
            femCount++;
        }

        // Community college and University calculations
        if(school == "CC")
        {
            ccScore +=score;
            ccCount++;
        }
        else if(school == "UN")
        {
            unScore +=score;
            unCount++;
        }       
    }

    // We do static_cast<double>(maleScore) / maleCount; to ensure that
    // the division is done using floating point and not integer 
    // arithmetic. We could have multiplied the numerator by 1.0 instead.

    if(maleCount != 0)
    {
        cout << "The average scores for males is: " << setprecision(2)
             << static_cast<double>(maleScore) / maleCount << endl;
    }

    if(femCount != 0)
    {
        cout << "The average score for females is: " << setprecision(2)
             << static_cast<double>(femScore) / femCount << endl;
    }

    if(ccCount != 0)
    {
        cout << "The average score for Community Colleges is: " << setprecision(2)
             << static_cast<double>(ccScore) / ccCount << endl;
    }

    if(unCount != 0)
    {
        cout << unScore << "/" << unCount << endl;

        cout << "The average score for Universities is: "  << setprecision(2)
             << static_cast<double>(unScore) / unCount << endl;
    }

    // In this case we will use the multiplication technique instead:   
    cout << "The 'sum' average is: " << setprecision(2)
         << (1.0 * (maleScore + femScore + ccScore + unScore)) / 12 << endl;

    return 0;
}
于 2013-02-13T22:21:11.073 回答