-5

I am writing a program for a class that computes tuition for students. I am getting an infinite loop and cant seem to find why?

Code:

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

using namespace std;

int main ()
{
    string ssn;
    char resident;
    int count = 0, hours;
    double feesOther = 30, feesTech = 18, tuition, totalFees, sumTuition = 0, subTuition;

    ofstream printFile ("StudentGrades.txt");
    if (!printFile) 
    {
        cout << " Error opening printFile" << endl;
        system ("pause");
        return 100; 
    }

    ifstream studentFile;
    studentFile.open("c:\\lab5b.dat");

    if (!studentFile)
    {
        cout << "Open error on lab5a.dat" << endl;
        system ("pause");
        return 101;
    }

    studentFile >> ssn >> resident >> hours;

    cout << "SSN          Tuition\n";
    cout << "--------------------\n";

    while (!studentFile.eof())
    {
        if (resident == 'Y' || resident == 'y')
        {
            if (hours >= 12)
                tuition = 1548;

            else

                tuition = hours * 129;
        }   
        if  (resident == 'N' || resident == 'n')
        {
            if (hours >= 12)
                tuition = 6360;

            else

                tuition = hours * 530;
        }   

        totalFees = feesOther + (hours * feesTech);

        if (totalFees > 112.50)
            totalFees = feesOther + 112.50;

        subTuition = tuition + totalFees;
        sumTuition += tuition ;
        count++;


        cout << ssn << setw(7) << showpoint << setprecision(2) << subTuition << endl;
        cout << "Total Semester Tuition: " << sumTuition << endl;

        studentFile >> ssn >> subTuition;


    }

    studentFile.close();    
    printFile.close();      

    system ("pause");
}
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
Kevin Schultz
  • 886
  • 3
  • 20
  • 42
  • 9
    Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Mar 30 '13 at 00:58
  • Infinite loop? Where's the loop? What's the condition of the loop? What's the meaning of that loop variant `!studentFile.eof()`? – CppLearner Mar 30 '13 at 00:59
  • Try to refer to this. http://stackoverflow.com/questions/1494342/end-of-file-in-c – wmfairuz Mar 30 '13 at 01:06

1 Answers1

1

Your studentfile is most likely in a fail state, as you never check whether the read operations are successful or not. Change the code as follows:

if(!(studentFile >> ssn >> resident >> hours))
{
    std::cout << "read failed";
    return 1;
}

//...    

do 
{
    // Do stuff
    // REMOVE THIS LINE: studentFile >> ssn >> subTuition;
} while (studentFile >> ssn >> subTuition); // while loop stops as soon as read fails

The key lesson to learn here is always perform error checking during read and write operations. Also, please read Why is iostream::eof inside a loop condition considered wrong? as while (!studentFile.eof()) is considered a C++ anti-pattern.

Community
  • 1
  • 1
Jesse Good
  • 50,901
  • 14
  • 124
  • 166