繁体   English   中英

C ++计数负数和X以上的数字

[英]C++ Count Negative numbers and numbers above X

我正在一个项目中,在txt文件中有一个数字列表。 C ++程序会提取这些数字,并根据它们是否满足特定条件将它们放入适当的“存储桶”中。 我希望将小于0的数字和大于200的数字放在一起。 但是我似乎无法使负数得到认可。 思考?

//GET IT STARTED IN HERE
int main()
{
//VARIABLES ETC
int score;

//SET SCORE RANGES TO 0
int bucket[9] = {0,0,0,0,0,0,0,0,0};


ifstream scoreFile;
string file;


//OPEN UP THE SCORE FILE
cout << "Enter path to Score File: ";
cin >> file;

scoreFile.open(file.c_str());

if (file == "kill" || file == "KILL" || file == "Kill") {
    cout << "Program terminated with KILL command" << endl;
    return 0;
}
else
{
//CHECK FOR BAD PATH
while (!scoreFile)
{
    cerr << "Wrong path" << endl;
    cout << "Try path again: ";
    cin >> file;
    scoreFile.clear();
    scoreFile.open(file.c_str());
}
}
//LOOK AT ALL THE SCORES FROM LAST WEEKS TEST
int scoreRow(1);

//CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
while (scoreFile >> score)
{
    if (score <= 24)
        bucket[0]++;

    else if (score <= 49)
        bucket[1]++;

    else if (score <= 74)
        bucket[2]++;

    else if (score <= 99)
        bucket[3]++;

    else if (score <= 124)
        bucket[4]++;

    else if (score <= 149)
        bucket[5]++;

    else if (score <= 174)
        bucket[6]++;

    else if (score <= 200)
        bucket[7]++;

    //ADDED TWO EXTRA SCORES IN THE FILE TO TEST THIS AREA

    else if (score < 0 || score > 200)
        bucket[8]++;

    scoreRow++;
} 

//OUTPUT SOME RESULTS
cout << endl << "SCORE EVALUATION"<< endl;
cout << "Amount of students who scored    0 - 24: " << bucket[0] << endl;
cout << "Amount of students who scored   25 - 49: " << bucket[1] << endl;
cout << "Amount of students who scored   50 - 74: " << bucket[2] << endl;
cout << "Amount of students who scored   75 - 99: " << bucket[3] << endl;
cout << "Amount of students who scored 100 - 124: " << bucket[4] << endl;
cout << "Amount of students who scored 125 - 149: " << bucket[5] << endl;
cout << "Amount of students who scored 150 - 174: " << bucket[6] << endl;
cout << "Amount of students who scored 175 - 200: " << bucket[7] << endl;
cout << "Scores out of Range: " << bucket[8] << endl;

}

您想将最后一个条件放在顶部。

   //CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
    while (scoreFile >> score)
    {
        if (score < 0 || score > 200)
            bucket[8]++;

        else if (score <= 24)
            bucket[0]++;

        else if (score <= 49)
            bucket[1]++;

        else if (score <= 74)
            bucket[2]++;

        else if (score <= 99)
            bucket[3]++;

        else if (score <= 124)
            bucket[4]++;

        else if (score <= 149)
            bucket[5]++;

        else if (score <= 174)
            bucket[6]++;

        else if (score <= 200)
            bucket[7]++;

        scoreRow++;
    } 

此处的每个分数只能进入一个存储桶。 如果得分<0,它将由第一个if语句求值并将其放在该存储桶中。

因此,您的其他else if()将仅捕获超过200的值,并且如果您有任何负分数,它们将全部落在第一个存储桶中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM