In: Computer Science
Hello all, I am toward the beginning of my semseter and am still pretty rusty working with code. here is my following goal: basically create a structure and populate an array from a txt file which contains
98 45.70
72 15.0
12 0.0
56 43.26
83 123.0
28 931.96
123 12.38
"Create a struct named familyFinance that contains 2 members: ▪ int acctNos ▪ float balance o (prior to the while loop) Create an array financeAry (size 10), of type familyFinance o Populate familyFinance by reading from the file lab3data.txt (each line has an acctNo and balance) o Modify function parameters (for spitThemOut and anyBelowThreshold) so they work with financeAry. Modify as well, the prototypes, and the actual calling/invocation (in while loop). NO change to function names. o (in while loop) If Option 1 chosen, cout “NOT implemented.” and comment the call to printOnesBelowThreshold"
here is what i have for code so far.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void printOnesBelowThreshold(float, int [], float [], int);
void spitThemOut(int [], float [], int);
bool anyBelowThreshold(int [], float [], int, float);
struct familyFinance {
int acctNOS;
float balance;
};
int main() {
int acctNos[] = {98, 72, 12, 56, 83, 28, 123};
float balance[] = {45.70, 15, 0, 43.26, 123, 931.96, 12.38};
int noAccounts = 7, option;
float threshold;
double financeAry [10];
int x = 0;
{
ifstream inFile;
inFile.open("lab3data.txt");
while(!File.eof())
{
inFile >> financeAry[x].acctNos;
infile >> balance[x].acctNos
x++;
}
inFile.close();
}
while (true) {
cout << "Option (1 2 3 or other# to quit): ";
cin >> option;
switch (option) {
case 1:
cout << endl << "Not Implemented: ";
break;
case 2:
spitThemOut(acctNos, balance, noAccounts);
break;
case 3:
cout << endl << "Balance threshold please: ";
cin >> threshold;
if (anyBelowThreshold (acctNos, balance, noAccounts, threshold))
cout << endl << "Yes, some a/c below balance of :" << threshold;
break;
default:
cout << endl << "All done" << endl;
exit(1);
}
}
}
//Option 1: Dipaly each account and balance, if < Threshold
void printOnesBelowThreshold(float threshold, int acctNos[], float balance[], int upperBound) {
cout << showpos;
for (int i = 0; i < upperBound; i++) {
if (balance[i] < threshold) {
cout << "Acct, Balance < Threshold: " << setw(3)
<< acctNos [i] << " " << balance[i] << endl;
}
}
}
//Option 2: Dipaly each account and balance
void spitThemOut(int acctNos[], float balance[], int upperBound) {
cout << showpos;
for (int i = 0; i < upperBound; i++) {
cout << "Acct, Balance: " << setw(3)
<< acctNos [i] << " " << balance[i] << endl;
}
}
//Option 3: Return true if any Balance < Threshold
bool anyBelowThreshold (int acctNos[], float balance[], int upperBound, float threshold) {
for (int i = 0; i < upperBound; i++)
if (balance[i] < threshold)
return true;
return false;
}
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> // for exit
using namespace std;
struct familyFinance
{
int acctNos;
float balance;
};
// function prototypes
void printOnesBelowThreshold(float, familyFinance [] , int);
void spitThemOut(familyFinance [], int);
bool anyBelowThreshold(familyFinance [], int, float);
int main() {
int size = 10;
familyFinance financeAry[size];
float threshold;
int noAccounts, option;
ifstream inFile;
inFile.open("lab3data.txt");
if(inFile.is_open()) // if file can be opened
{
noAccounts = 0;
// read till the end of file or till maximum size of array records have been read
while(!inFile.eof() && (noAccounts < size))
{
inFile >> financeAry[noAccounts].acctNos>>financeAry[noAccounts].balance;
noAccounts++;
}
inFile.close(); // close the file
while (true) {
cout << "Option (1 2 3 or other# to quit): ";
cin >> option;
switch (option) {
case 1:
cout << endl << "Not Implemented: ";
break;
case 2:
spitThemOut(financeAry, noAccounts);
break;
case 3:
cout << endl << "Balance threshold please: ";
cin >> threshold;
if (anyBelowThreshold (financeAry, noAccounts, threshold))
cout << endl << "Yes, some a/c below balance of :" << threshold<<endl;
break;
default:
cout << endl << "All done" << endl;
exit(1);
}
}
}
return 0;
}
//Option 1: Display each account and balance, if < Threshold
void printOnesBelowThreshold(float threshold, familyFinance financeAry[], int upperBound) {
cout << showpos;
for (int i = 0; i < upperBound; i++) {
if (financeAry[i].balance < threshold) {
cout << "Acct, Balance < Threshold: " << setw(3)
<< financeAry[i].acctNos << " " << financeAry [i].balance << endl;
}
}
}
//Option 2: Display each account and balance
void spitThemOut(familyFinance financeAry[], int upperBound) {
cout << showpos;
for (int i = 0; i < upperBound; i++) {
cout << "Acct, Balance: " << setw(3)
<< financeAry[i].acctNos << " " << financeAry[i].balance << endl;
}
}
//Option 3: Return true if any Balance < Threshold
bool anyBelowThreshold (familyFinance financeAry[], int upperBound, float threshold) {
for (int i = 0; i < upperBound; i++)
if (financeAry[i].balance < threshold)
return true;
return false;
}
//end of program
Output:
Input File:
Output: