使用数据文件排序进行计算时遇到问题

时间:2013-02-13 21:30:48

标签: c++ if-statement while-loop counter ifstream

我是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;
}

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

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。我们一次拿一个东西,不管吗?

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

最后一行做什么? inData尚未公开,但您正试图从中读取。

maleScore = maleScore +=score;

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

maleScore += score;

或像这样使用+

maleScore = maleScore + score;

然后你这样做:

if(school == CC)

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

再往下,你在循环中有这一行:

maleAvg = maleScore/maleCount;

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

最后,在循环中使用iostream::eof()并不是一个好主意。您可以在StackOverflow上阅读更多相关内容:Why is iostream::eof inside a loop condition considered wrong?

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

#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;
}