ifstream read-in error infinite entries

This is a discussion on ifstream read-in error infinite entries within the C++ Programming forums, part of the General Programming Boards category; After I press enter the code stops and lets me press enter forever; it won't terminate or proceed with the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    ifstream read-in error infinite entries

    After I press enter the code stops and lets me press enter forever; it won't terminate or proceed with the program. What do I do??? This is due in 1 hour help pls... T_T
    Output:

    Greetings!
    Enter
    number of banks would you like to analyze: 2
    Please enter file name:
    alpha_bank.txt



    //Then after that it lets me press enter forever
    and doesn't do anything
    // also alpha_bank is a text file in the same folder
    as this file

    my code:

    Code:
    #include <iostream>
    Code:
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    bool openAndVerify(ifstream &, string &);
    double averageCalc(ifstream &, string &);
    double stndrdDevCalc(ifstream &, string &, double);
    void print(double, double, string);
    string getBankName(ifstream &, string &);
    
    int main() {
    
    	int counter, numDesired;
    	double mean, stndrdDev;
    	string bankName, fileName;
    	ifstream inputFile;
    	double min = 0;
    	string betterBank;
    	cout << "Greetings!\nEnter number of banks would you like to analyze: ";
    	cin >> numDesired;
    	counter = 0;
    
    	while(counter < numDesired) { 
    
    		if(openAndVerify(inputFile,fileName) == false) {
    			cout << "Error: bad file name. Program terminated!" << endl;
    			return 0;
    		} else {
    		
    			bankName = getBankName(inputFile, fileName);
    
    			cout << bankName;
    
    			mean = averageCalc(inputFile, fileName);
    	
    			stndrdDev = stndrdDevCalc(inputFile, fileName, mean);
    
    			if (stndrdDev < min){
    
    				betterBank = bankName;		
    
    			}		
    		
    			print(stndrdDev, mean, bankName);
    		
    			counter++;
    		}
    
    	}
    	
    	cout << betterBank << " is the preferred bank because it has the lowest standard deviation." << endl;
    
    	return 0;
    
    }
    
    
    // PART A
    
    bool openAndVerify(ifstream &inputFile, string &fileName) {
    
    	cout << "Please enter file name: ";
    	cin >> fileName;
    
    	inputFile.open(fileName.c_str());
    	
    	if(inputFile.fail()) {
    
    		inputFile.close();
    		return false; 
    
    	} else {
    
    		inputFile.close();
    		return true; 
    
    	}
    
    
    }
    
    
    // PART B
    
    double averageCalc(ifstream &inputFile, string &fileName) {
    
    	int numEntries = 0;
    	double time, sum;
    	sum = 0;
    	inputFile.open(fileName.c_str());
    	inputFile.ignore(500, '\n');
    	//getline(inputFile, fileName)
    
    	while(!inputFile.eof()) {
    
    		inputFile >> time;
    		sum += time;
    		numEntries++;
    
    	}
    		
    	inputFile.close();
    
    	return sum / numEntries;
    
    }
    
    
    // PART C
    
    double stndrdDevCalc(ifstream &inputFile, string &fileName, double mean) {
    
    	int numEntries = 0;
    	double time, stndrdDevCalcNumerator = 0;
    
    	inputFile.open(fileName.c_str());
    	inputFile.ignore(500, '\n');
    	
    	while(!inputFile.eof()) {
    
    		inputFile >> time;
    		stndrdDevCalcNumerator += pow((time - mean), 2);
    		numEntries++;
    	
    	}
    
    	inputFile.close();
    
    	return sqrt(stndrdDevCalcNumerator / (numEntries - 1));
    	
    
    }
    
    // Part D
    
    void print(double stndrdDev, double mean, string bankName) {
    
    	cout << "===============================================" << endl;
    	cout << "Bank Name: " << bankName << endl;
    	cout << setprecision(1) << showpoint << fixed;
    	cout << "Average wait time: " << mean << endl;
    	cout << "Standard Deviation of the wait time list: " << stndrdDev << endl;
    	cout << "===============================================" << endl << endl;
    
    }
    
    string getBankName(ifstream &inputFile, string &fileName) {
    
    	string bankName;
    	inputFile.open(fileName.c_str());
    	inputFile >> bankName;
    	return bankName;
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Open AND verify?

    inputFile.close();
    return true;

    So why do you close it when returning success?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    I just tried deleting the [code] inputFile.close() [\close]
    I think you're right. I shouldn't close it; however it doesn't fix the problem. It's still letting me enter forever...
    Not sure what to do...

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    why does no one help me...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > inputFile.ignore(500, '\n');
    Why do you burn a line at the start?

    > while(!inputFile.eof())
    See this
    SourceForge.net: Feof - cpwiki

    > inputFile >> time;
    If this does NOT convert a time, then this loop will go on forever.
    Ideally, the whole loop should be
    Code:
    while (  inputFile >> time ) {
            sum += time;
            numEntries++;
    }
    > inputFile.close();
    Again with the closing of the file before you're done reading it.
    See part C, which also reads the file.

    Also consider that when you've read the file once, you need to seek back to the start to read it again (and possibly clear the end of file condition).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2011, 05:30 AM
  2. CPP: cannot read with ifstream?
    By nacho4d in forum Linux Programming
    Replies: 2
    Last Post: 02-24-2010, 02:52 PM
  3. [question] ifstream read file, thanks!
    By userpingz in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2009, 07:38 PM
  4. Getting size read with ifstream and read()
    By nickname_changed in forum C++ Programming
    Replies: 13
    Last Post: 08-03-2003, 07:05 AM
  5. Help with ifstream read plz
    By dp_goose in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 07:35 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21