简体   繁体   中英

C++ Count Negative numbers and numbers above X

I am working on a project where in a txt file there is a list of numbers. The C++ program is pulling these numbers and putting them into appropriate "buckets" based on if they meet certain criteria. I want numbers less than 0 and numbers more than 200 to be put into a bucket together. But I can not seem to get the negative numbers to be recognized. Thoughts?

//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;

}

You want to place the last condition at the top.

   //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++;
    } 

Each score here can only go into one bucket. If score < 0, it will be evaluated by the first if statement and placed in that bucket.

So your last else if() will only catch values over 200, and if you have have any negative scores, they'll all land in the first bucket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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