Question: Can I know why this code didn't work? it even didn't show the error message. #include #include #include #include using namespace std; void printHeading(ofstream& oFile);
Can I know why this code didn't work? it even didn't show the error message.
#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; } 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;
} else if (option == 3) { sum=getSumAndAverage(option, balance, index); oFile << "Sum of Balance for all persons :" << endl; oFile << "$ " << sum << endl; } else if (option == 4) { avg=getSumAndAverage(option, balance, index); oFile << "Average Balance for all persons :" << endl; oFile << "$" << avg << 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++; }
return index; }
int getLagerAndSmallerBallence( int option, float balance[100], int index, string name[100], int id[100]) { float large = -9999; float small = 9999; string namee; int idd;
if (option == 1) { for (int i = 0;i < index;i++) { if (balance[i] > large) { large = balance[i]; return i;
} } } if (option == 2) { for (int i = 0;i < index;i++) { if (balance[i] < small) { small = balance[i]; return i; } } } }
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; }
}
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; } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
