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