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 8957641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:02:32+00:00 2026-06-15T15:02:32+00:00

I am working on a homework assignment that calculates GPA’s and course averages using

  • 0

I am working on a homework assignment that calculates GPA’s and course averages using an external file. The code is not completely fleshed out, but I’ve built it out enough that it should be able to at least show me that it can pull the data from the external file (a plain text file created in TextWrangler), and calculate the figures.

However, when I run it, it only seems to be pulling every other line of the output file. Any ideas of where I could be going wrong?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{

    int studentID, studentCounter=0;
    double grade1, grade2, grade3, grade4, course1Sum=0, course2Sum=0, course3Sum=0, course4Sum=0, course1Average, course2Average, course3Average, course4Average, GPA;
    ifstream inputFile;
    string filename;

    for (int x=1; x<=3; x++) {

        // Get the filename from the user.
        // /Users/etc/Desktop/HW5test.txt - valid set
        // /Users/etc/Desktop/HW5testset.txt - test set


        cout<<"Enter the filename: ";
        cin>>filename;

        // Open the file.
        inputFile.open(filename.c_str());
        // If the file successfully opened, process it.
        if (inputFile) {
            // Read the numbers from the file and display them.
            while (inputFile>>studentID>>grade1>>grade2>>grade3>>grade4)
            {
                if (studentCounter==0) // Prints header if at the start of the program.
                    cout<<"Header placeholder"<<endl;

                inputFile>>studentID>>grade1>>grade2>>grade3>>grade4;
                GPA=(grade1+grade2+grade3+grade4)/4;
                cout<<studentID<<" "<<GPA<<" "<<"Special message (if needed)."<<endl;

                // Course Average and Student Accumulators
                course1Sum+=grade1;
                course2Sum+=grade2;
                course3Sum+=grade3;
                course4Sum+=grade4;
                studentCounter++;

            } // Ends while() loop.

            if (studentCounter==0)
                cout<<"File contains no data."<<endl;

            else {

            // Perform course averages here, after all data has been read.
            course1Average=course1Sum/studentCounter;
            cout<<"Course 1 average: "<<course1Average<<endl;
            course2Average=course2Sum/studentCounter;
            cout<<"Course 2 average: "<<course2Average<<endl;
            course3Average=course3Sum/studentCounter;
            cout<<"Course 3 average: "<<course3Average<<endl;
            course4Average=course4Sum/studentCounter;
            cout<<"Course 4 average: "<<course4Average<<endl<<endl;

            } // Ends "else" of file empty error check.

            // Close the input file.
            inputFile.close();
        } // Ends "if" of if/else().
        else {
            cout<<"Error opening the file.\n";
        } // Ends "else" of if/else().


    } // Ends for() loop.

    return 0;
} // Ends main().

The input file data:

1001 3.0 2.7 2.0 2.8 
1002 3.7 3.5 4.0 4.0 
1003 0.0 1.0 2.2 1.0 
1004 4.0 3.0 3.5 3.5 

My output looks like this (keep in mind, still working on pieces, but see that it’s only pulling every other entry:

Enter the filename: /Users/etc/Desktop/HW5testset.txt
Header placeholder
1002 3.8 Special message (if needed).
1004 3.5 Special message (if needed).
Course 1 average: 3.85
Course 2 average: 3.25
Course 3 average: 3.75
Course 4 average: 3.75

What could be going on?

  • 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-15T15:02:33+00:00Added an answer on June 15, 2026 at 3:02 pm

    inputFile>>studentID>>grade1>>grade2>>grade3>>grade4 appears in your while statement and instide the while statement. Delete the instance that is not in the while condition (the one before GPA=.

    You read the line in the while statement, then read the next line and therefore never see the first of the two lines. Just because the code is in the while() doesn’t mean it doesn’t have side-effects

    EDIT

    Also make your

    if (studentCounter==0) // Prints header if at the start of the program.
        cout<<"Header placeholder"<<endl;
    

    into:

    if (studentCounter==0) { // Prints header if at the start of the program.
        studentCounter++;
        cout<<"Header placeholder"<<endl;
        continue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a homework assignment to modify code given by my professor using
I am working on a homework assignment that deals with binary search trees and
I'm working on a homework assignment (a project), for which one criterion is that
Good day, Stack Overflow. I have a homework assignment that I'm working on this
I'm working on a homework assignment that has four text fields and one text
I'm working on a homework assignment in Perl CGI using the CGI.pm module. In
Ok, so I am working on a homework assignment, and I am using SWING
I'm a novice working on a homework assignment introducing us to using files in
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
I'm working on a homework assignment that asks me to create a calculator that

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.