Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a c++ program I wrote, but I can\'t seem to get the average test score

ID: 3665406 • Letter: B

Question

Below is a c++ program I wrote, but I can't seem to get the average test score for all students combines to work. Any suggestions?


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

using namespace std;

int main()

{
   string name;
   string fileSource;

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

   ifstream inData;
   ofstream outData;

   cout << "Please input file location: ";
   cin >> fileSource;

   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)
   {
       char sex;
       int score;
       string school;

       inData >> sex >> school >> score;

       cout << name << sex << school << endl;

       if (sex == 'M')
       {
           maleScore += score;
           maleCount++;
       }

       if (sex == 'F')
       {
           femScore += score;
           femCount++;
       }

       if (school == "CC")
       {
           ccScore += score;
           ccCount++;
       }
       else if (school == "UN")
       {
           unScore += score;
           unCount++;
       }
   }
   cout << fixed << showpoint << setprecision(2) << endl;

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

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

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

   if (unCount != 0)
   {
       cout << "The average score for Universities is: "
           << static_cast<double>(unScore) / unCount << endl;
   }
   cout << "The average test score for all students combined is: "
       << (1.0 * (maleScore + femScore + ccScore + unScore)) / 12 << endl;

   return 0;
}

Explanation / Answer

Corrected Program:


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

using namespace std;

int main()

{
    string name;
    string fileSource;

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

    ifstream inData;
    ofstream outData;

    cout << "Please input file location: ";
    cin >> fileSource;

    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)
    {
        char sex;
        int score;
        string school;

        inData >> sex >> school >> score;

        cout << name << sex << school << endl;

        if (sex == 'M')
        {
            maleScore += score;
            maleCount++;
        }

        if (sex == 'F')
        {
            femScore += score;
            femCount++;
        }

        if (school == "CC")
        {
            ccScore += score;
            ccCount++;
        }
        else if (school == "UN")
        {
            unScore += score;
            unCount++;
        }
    }
    cout << fixed << showpoint << setprecision(2) << endl;

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

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

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

    if (unCount != 0)
    {
        cout << "The average score for Universities is: "
            << static_cast<double>(unScore) / unCount << endl;
    }
    cout << "The average test score for all students combined is: "

//why are you dividing by 12 instead you should divide by total count
    << (1.0 * (maleScore + femScore + ccScore + unScore)) / (maleCount+ femCount + ccCount + unCount<< endl;

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote