Question
Can I know how the eof work in c++ I think index have to stop at 10 because there are only ten person in InFile.txt
Can I know how the eof work in c++
I think index have to stop at 10 because there are only ten person in InFile.txt but it output until 11 so can I know how does the eof.() function work?
//InFile.txt
Jean Rousseau 1001 15.50 Steve Woolston 1002 1423.20 Michele Rousseau 1005 52.75 Pete McBride 1007 500.32 Florence Rousseau 1010 1323.33 Lisa Covi 1009 332.35 Don McBride 1003 12.32 Chris Carroll 1008 32.35 Yolanda Agredano 1004 356.00 Sally Sleeper 1006 32.36
==========================================
#include
int main() { string inFile; string outFile; string name[100] = {"0"}; int id[100] = {0}; int index; int option; int searchIndex; float sum; float avg; float balance[100] = {0}; ifstream iFile; ofstream oFile; cout << "What input file would you like to use? "; getline(cin, inFile); cout << "What output file would you like to use? "; getline(cin, outFile); iFile.open(inFile); oFile.open(outFile); index = getIndex(iFile, name, id, balance); printHeading(oFile); cout << "Enter an option (0 to exit):"; cin >> option; if (option == 1) { searchIndex=getLagerAndSmallerBallence( option, balance, index, name, id); oFile << "Larger Balance :" << endl; oFile << "ID # NAME BALANCE DUE" << endl; oFile << "---- -------------------- ---------- -" << endl; oFile << id[searchIndex] << " " << name[searchIndex] << " " << balance[searchIndex] << endl; cout << "Finding the larger balance" << endl; } else if (option == 2) { searchIndex = getLagerAndSmallerBallence( option, balance, index, name, id); oFile << "smaller Balance :" << endl; oFile << "ID # NAME BALANCE DUE" << endl; oFile << "---- -------------------- ---------- -" << endl; oFile << id[searchIndex] << " " << name[searchIndex] << " " << balance[searchIndex] << endl; cout << "Finding the smaller balance" << endl; } else if (option == 3) { sum=getSumAndAverage(option, balance, index); oFile << "Sum of Balance for all persons :" << endl; oFile << "$ " << sum << endl; cout << "Obtaining the sum of all balances" << endl; } else if (option == 4) { avg=getSumAndAverage(option, balance, index); oFile << "Average Balance for all persons :" << endl; oFile << "$" << avg << endl; cout << "Obtaining the average of all balances" << endl; } else if (option == 5) { searchIndex=searchName(name, index); oFile << "ID # NAME BALANCE DUE" << endl; oFile << "---- -------------------- ---------- -" << endl; oFile << id[searchIndex] << " " << name[searchIndex] << "$" << balance[searchIndex] << endl; } else if (option == 0) { return 0; } else { cout << "Thank you for using my program."; } iFile.close(); oFile.close(); return 0; }
void printHeading(ofstream& oFile) {
cout << "MENU OPTIONS" << endl; cout << "1 Find the larger balance" << endl; cout << "2 Find the smaller balance" << endl; cout << "3 Obtain the sum of all balances" << endl; cout << "4 Obtain the average of all balances" << endl; cout << "5 Find person" << endl; cout << "0 - Exit" << endl; oFile << "MENU OPTIONS" << endl; oFile << "1 Find the larger balance" << endl; oFile << "2 Find the smaller balance" << endl; oFile << "3 Obtain the sum of all balances" << endl; oFile << "4 Obtain the average of all balances" << endl; oFile << "5 Find person" << endl; oFile << "0 - Exit" << endl;
}
int getIndex(ifstream& iFile, string name[100], int id[100], float balance[100]) {
int index = 0;
while (!iFile.eof() && index < 100) { getline(iFile, name[index]); iFile >> id[index] >> balance[index]; iFile.ignore(1000, ' '); index++; cout << index << endl; // In here I think index have to stop at 10 because there are only ten person in InFile.txt but it output until 11 can I know how does the eof.() function //work?
return index; }
int getLagerAndSmallerBallence(int option, float balance[100], int index) { float large = -9999; float small = 9999; int number;
if (option == 1) { for (int i = 0;i < index;i++) { if (balance[i] > large) { large = balance[i]; number= i;
} } return number; } //return i i if (option == 2) { for (int i = 0;i < index;i++)// index-1 ? { if (balance[i] < small) { small = balance[i]; number= i; } } return number; } return 0; //add default return statement }
float getSumAndAverage(int option, float balance[100], int index) { float sum = 0; float avg; for (int i = 0;i < index;i++) { sum = sum + balance[i]; } avg = sum / index; if (option == 3) { return sum; } if (option == 4) { return avg; }
return avg; //add default return statement }
int searchName(string name[100], int index) { string userName; cout << "Who do you want to search for (enter done to exit):"; getline(cin, userName); for (int i = 0;i < index;i++) { if (name[i] == userName) { return i; } } return -1; //add default return statement }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started