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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:38:21+00:00 2026-05-31T01:38:21+00:00

I’m in a CS101 class at my school using C++. I have a program

  • 0

I’m in a CS101 class at my school using C++. I have a program that has to read a file in this format:

Ramirez, Manny
1572838992 a 4 b 5 x 4 a 7 c 3 c 4 *
Kemp, Matt
3337474858 a 4 b 4 b 4 a 4 *

It is supposed to read the file, calculate the GPA of each student and output the student’s name, ID, units, and GPA. I have been trying different things for a while now, and I’ve been going back and forth with infinite loops. I got help from a friend, who had me include some stuff.

I have tried changing the condition of the loop inside the while (!eof) loop so many times I can’t count them all. Every time I look up what to do online, the advice I get is just to not use while !eof because it has a propensity to cause infinite loops. Well, I know that now, but my professor wants us to do it that way.

Here is my code:

 #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <sstream>
    using namespace std;
    int main() {

    char grade;
    int units=0, studentsGrade=0, studentUnits=0;
    int unitTotal=0;
    float gradeTotal=0, gpa = 0;
    string inputName, outputName, fullName;
    long ID;
    ifstream inputFile;
    ofstream outputFile;

    cout << "Please enter the input filename: ";
    cin >> inputName;
    cout << "Please enter the output filename: ";
    cin >> outputName;
    inputFile.open(inputName.c_str());
    if (inputFile.fail()) 
        cout << "Bad input file name." << endl;
    else {
    outputFile.open(outputName.c_str());
    outputFile << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
    << "Units" << setw(15) << "GPA" << endl;
    outputFile << "--------------------------------------------------------------------------------" << endl;
    cout << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
    << "Units" << setw(15) << "GPA" << endl;
    cout << "--------------------------------------------------------------------------------" << endl;





    getline(inputFile, fullName);
    while (!inputFile.eof()) {
        gpa = 0;
        unitTotal = 0;
        gradeTotal = 0;
        inputFile >> ID;
        outputFile << setw(25) << fullName;
        outputFile << setw(15) << ID;
        cout << setw(25) << fullName << setw(15) << ID;
        string line;
        getline(inputFile,line);
        istringstream iss(line);
        while (!iss.eof()) {
            units = 0;
            iss >> grade >> units;
            if (grade == '*')
                break;
            if (units > 0 && units <=5 && (grade == 'a' || grade == 'A')) {
                gradeTotal += 4 * units;
                studentsGrade += 4 * units;
                unitTotal += units; 
                studentUnits += units;}
            else if (units > 0 && units <=5 && (grade == 'b' || grade == 'B')) {
                gradeTotal += 3 * units;
                studentsGrade += 3 * units;
                unitTotal += units;
                studentUnits += units; }
            else if (units > 0 && units <=5 && (grade == 'c' || grade == 'C')) {
                gradeTotal += 2 * units;
                studentsGrade += 2 * units;
                unitTotal += units;
                studentUnits += units; }
            else if (units > 0 && units <=5 && (grade == 'd' || grade == 'D')) {
                gradeTotal += 1 * units;
                studentsGrade += 1 * units;
                unitTotal += units;
                studentUnits += units; }
            else if (units > 0 && units <=5 && (grade == 'f' || grade == 'F')) {
                unitTotal += units;
                studentUnits += units; }
            else if (grade == '*') {
                unitTotal += 0;}
            else {
                unitTotal += 0; }
        } 
        gpa = (float)gradeTotal / unitTotal;
        outputFile << fixed << showpoint;
        outputFile << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
        cout << fixed << showpoint;
        cout << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
        getline(inputFile,fullName);
        }
    outputFile << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits;
    cout << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits << endl;

}
    inputFile.close();
    outputFile.close();
    cout << endl;
    return 0;
    }

If anyone can explain to me why I keep getting an infinite loop, I would really appreciate 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-05-31T01:38:22+00:00Added an answer on May 31, 2026 at 1:38 am

    Though it needs to be verified by running the code, it seems like you are getting either failbit or badbit set but not the eof. This may happen, for example, after you do other reads and not getline() – for example inputFile >> ID;.

    Since you are only checking for the eof, the loop runs endlessly and I/O errors are getting ignored. I’d say try using the result of getline() and use operator void* of the std::istream instead of checking eof().

    If your professor wants you to use eof() only, then try to workaround error checking. The solution could be to use special values and assume that if read operation didn’t set the value to something else then it has failed, and break out of the loop. For example, set ID to LONG_MAX before read, and make sure it is not LONG_MAX after.

    And after all – just run the code under debugger on a small working data set, step through and see what happens. Get yourself familiar with a debugger, and practice troubleshooting skills. That will for sure save you a lot of time throughout your career.

    See also – http://www.cplusplus.com/reference/iostream/ifstream/

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

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.