Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9291161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:35:02+00:00 2026-06-18T20:35:02+00:00

I am new to the stackoverflow communiy and new to coding so apologies in

  • 0

I am new to the stackoverflow communiy and new to coding so apologies in advance whie I learn the ropes of posting here as well as the rules of coding. I am using C++ and am in a CS161 beginner Computer Science class.

I am currently working on an assignment which asks me to read from a data file stored on my computer and sort the data in order to do some calculations, which, in this assignment is finding the average test scores based on sex and type of school. everything compiles and the program runs but there are a few problems.

The first problem is with my echo.

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

The program does echo the data but it ends up echoing the last name on the list, twice for some reason. Also, (and I don’t know if this matters) when it does echo, it does not skip spaces inbetween name, sex, school, and score.

The second problem is that it isn’t executing the calculations and I think it is because I am missing a “count” related instruction of some sort but I am stumped as far as what I can do.

Here is my code, let me know what you think:

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

Also, my compiler keeps running the program and does not stop. I took a pic of my compiler window but don’t know how to post it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:35:03+00:00Added an answer on June 18, 2026 at 8:35 pm

    You have a few issues with your code. Let’s take things one at a time, shall we?

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

    What does the last line do? inData isn’t open and yet you’re trying to read from it.

    maleScore = maleScore +=score;
    

    Here (and in a few other places) you are using the += operator incorrectly. You should either use += like this:

    maleScore += score;
    

    or use + like this:

    maleScore = maleScore + score;
    

    You then do this:

    if(school == CC)
    

    Now, CC is an std::string in your code, which you have not initialized (which means that it is empty). So it will never match, therefore the if body will never execute. The same thing happens with UN.

    Further down, you have this line inside the loop:

    maleAvg = maleScore/maleCount;
    

    What this does is recalculate the average for males every time through the loop. That’s not necessarily wrong (you will get the right result) but if the first person in the file is a female, your program will crash with a division by zero, since maleCount will be 0. The same thing will happen if no females are in the input file, or if there are no scores from a University or a College.

    Lastly, using iostream::eof() inside a loop is not a good idea. You can read more about that right here on StackOverflow: Why is iostream::eof inside a loop condition considered wrong?

    With all that said, your mistakes aren’t really serious and are typical of new programmers, so don’t get discouraged. Think of this as an opportunity to learn and to understand how to examine your code to find these sort of bugs. So without further ado, let’s see an improved version of this program:

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new here to stackoverflow, so bear with me. I have a book that
Brand new poster here on StackOverflow. I've been reading and learning here for some
I'm new to stackoverflow. My question ia about gpuocelet. Is there anybody using it?
Hi guys I´m new at stackoverflow and also new at Jquery Well hope I
I'm new here, working with Web Apps & SEO. StackOverflow has been a great
I'm playing a little bit with the new StackOverflow API . Unfortunately, my JSON
I am new to stackoverflow as a member, although I follow this a lot
I'm new to StackOverflow and VBA. I am an Expert with all aspects of
Hi i am new to stackoverflow. I am developing an xmpp client application for
Hi everyone I am new to StackOverflow and SQL. I am not sure how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.