The attached account info file consists of a triple of information. A valid acco
ID: 3865166 • Letter: T
Question
The attached account info file consists of a triple of information.
A valid account number
The current account balance
The maximum account balance
Using the account info file write a program using appropriate functions that lets a user:
Enter a given account number
Enter a charge amount (positive values only)
This program then validates the account number and the amount. Then it determines if the current requested charge will exceed the account balance.
if a valid account, print a validation message
If a valid amount then print if the charge is accepted
if anything is invalid print a message indicating what is wrong.
Store the values in the file in appropriate vectors. once stored in the vectors, use the selection sort method to sort the array and use a binary search method to find the account number.
Do not use the algorithms library for either the sort or search, write the functions yourself.
valid_accounts txt file
Explanation / Answer
#include<iostream>
#include<conio>
#include<fstream> //headerfile to include files
#include<vector> //headerfile to include vectors
using namespace std;
class accounts //Class that will hold account number, current balance, maximum account balance
{
private:
float accno;
float currentbalance;
float maxbalance;
public:
accounts(float a, float b, float c) //Overloaded para-meterised constructor
{
accno=a;
currentbalance=b;
maxbalance=c;
}
};
int main()
{
clrscr(); //function to clear the output screen everytime user runs the programme
int choice,count=0, i, j, loc,flag=0;
float a, b, c, min,temp, search,amount;
vector <accounts> newacc; //creating vector as object of class accounts
do
{
cout<<" Press 1 to enter values Press 2 to stop";
cout<<" Enter your choice:";
cin>>choice;
if(choice==1)
{
count++;
cout<<" Enter account number:";
cin>>a;
cout<<" Enter current balance:";
cin>>b;
cout<<" Enter maximum account balance:";
cin>>c;
accounts obj1(a,b,c);
/* Creating class accounts object and calling the overloaded parameterised constructor to initialise the variables of class */
newacc.push_back(obj1); //Intialising the vector newacc
}
else
{
if(choice==2)
break; //exits the loop if user enters 2
else
cout<<" You entered wrong choice";
}
}
while(1);
//Sorting vector using Selection Sort with account no as sorting criteria
for(i=0;i<newacc.size();i++)
{
min=newacc[i].accno;
loc=i;
for(j=i+1;j<newacc.size();j++)
{
if(min>newacc[j].accno)
{
min=newacc[j].accno;
loc=j;
}
}
temp=newacc[i].accno;
newacc[i]=newacc[loc].accno;
newacc[loc]=temp;
temp=newacc[i].currentbalance;
newacc[i]=newacc[loc].currentbalance;
newacc[loc]=temp;
temp=newacc[i].maxbalance;
newacc[i]=newacc[loc].macbalance;
newacc[loc]=temp;
}
//Storing the sorted vector in the file valid_accounts.txt
fstream file; //creating file object
file.open("valid_accounts.txt",ios::in|ios::out); //opening the file in read and write mode
if(!file)
{
cout<<"Cannot open file";
}
for(i=0;i<newacc.size();i++)
{
file.write((char*)&newacc[i], sizeof(newacc[i])); //writing the contents of vector newacc into the file valid_accounts.txt
file.puts(" ");
}
file.seekg(0); //resets the cursor to the begining of the file
//Reading the contents from valid_accounts.txt
cout<<" Enter the account number you want to search for:";
cin>>search;
for(i=0;i<newacc.size();i++)
{
if(search==newacc[i].accnumber)
{
cout<<" Details of Account Number:"<<newacc[i].accnumber;
cout<<" Current Balance:"<<newacc[i].currentbalance;
cout<<" Maximum Account Balance:"<<newacc[i].maxbalance;
flag=1;
}
}
if(flag==0)
cout<<" Sorry No Record Found!";
file.seekg(0);
//Updating data in File valid_accounts.txt
cout<<" Enter Account Number:";
cin>>search;
cout<<" Enter Charge amount:";
cin>>amount;
for(i=0;i<newacc.size();i++)
{
if(search==newacc[i].accnumber)
{
if(amount+newacc[i].currentbalance<newacc[i].maxbalance)
{
newacc[i].currentbalance=amount+newacc[i].currentbalance;
fout<<newacc[i].currentbalance;
cout<<" Record Updated";
flag=1;
}
}
}
if(flag==0)
cout<<" Sorry No Record Found!";
file.seekg(0);
file.close(); //closing the file valid_accounts.txt
return 0;
]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.