I need Help Solving My Project 2 I have Added the Completed Code from project 1!
ID: 3862203 • Letter: I
Question
I need Help Solving My Project 2 I have Added the Completed Code from project 1! Please help this is due on Friday Night and cant figure out what to do! Can someone please help me solve this!
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct bank
{
int accNumber;
string Fname, Lname;
float balance;
};
static int j = 0;
struct bank B[5];
void read_from_file(const char* filename)
{
/*Variable declarations*/
ifstream infile;
/*Opening file*/
infile.open (filename);
int i=0;
if (infile.is_open())
{
/*Reading all details from file*/
while(!infile.eof()) // To get you all the lines.
{
infile>>B[i].accNumber;
infile>>B[i].Fname; // Saves the line in STRING.
infile>>B[i].Lname;
infile>>B[i].balance;
/*To prevent read last line twice*/
if( infile.eof() )
{
break;
}
j++;
i++;
}
/*Printing all account details after reading*/
for (i = 0; i<j; i++){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << ' ';
}
infile.close();
}
}
int addAccount()
{
int accNum,i;
string fname, lname;
float balance;
cout << " --- Provide details --- ";
cout << "Acc Number: "; cin >> accNum;
for (i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
j = j + 1;
B[i].accNumber = accNum;
cout << "first name: "; cin >> B[i].Fname;
cout << "last name: "; cin >> B[i].Lname;
cout << "Balance:"; cin >> B[i].balance;
}
void deleteAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
for (int k = i; k<j - 1; k++)
B[k] = B[k + 1];
}
}
}
void searchAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
}
}
int withdraw()
{
float withd;
int accNum,flag=0;
cout << "Enter account number from you want to withdraw:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
flag=0;
cout << "Enter withdraw amount : ";
cin >> withd;
if (B[i].balance >= withd){
B[i].balance = B[i].balance - withd;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int deposit()
{
float dep;
int accNum,flag=0;
cout << "Enter account number you want to Deposit:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
flag=0;
if (B[i].accNumber == accNum)
{
cout << "Enter deposit amount : ";
cin >> dep;
B[i].balance = B[i].balance + dep;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int transfer()
{
float tran;
int fromAcc, toAcc, fr, to, i, f1, f2;
cout << "Enter account number you want to transfer from "; cin >> fromAcc;
cout << "to "; cin >> toAcc;
for (i = 0; i<j; i++)
if (B[i].accNumber == fromAcc){
f1 = 1;
fr = i;
}
for (i = 0; i<j; i++)
if (B[i].accNumber == toAcc){
f2 = 1;
to = i;
}
if (f1 == 1 && f2 == 1)
{
cout << "Enter amount to transfer : ";
cin >> tran;
if (B[fr].balance >= tran){
B[to].balance = B[to].balance + tran;
B[fr].balance = B[fr].balance - tran;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
void list()
{
for (int i = 0; i<j; i++)
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
/*Write into file*/
void write_into_file(const char* filename){
int i;
ofstream myfile (filename);
if (myfile.is_open()) {
for(i=0;i<j;i++){
myfile<<B[i].accNumber<<" ";
myfile<<B[i].Fname<<" "; // Saves the line in STRING.
myfile<<B[i].Lname<<" ";
myfile<<B[i].balance<<" ";
}
}
}
int main()
{
int op,i;
char ch;
read_from_file("account.txt");
do{
cout << "-- Operations menu --- ";
cout << " 1. Add an Account "
"2. Delete an Account "
"3. Search for an account by account number and display the account info "
"4. Make a Withdraw "
"5. Make a Deposit "
"6. Transfer money from one account to another "
"7. List all accounts ";
cout << "Choose the operation : ";
cin >> op;
switch (op){
case 1: addAccount();
break;
case 2: deleteAccount();
break;
case 3: searchAccount();
break;
case 4: withdraw();
break;
case 5: deposit();
break;
case 6: transfer();
break;
case 7: list();
break;
default: cout << "Invalid operation ";
exit(0);
}
cout << "Do you wants more operation (Y/y) by default No : ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
/*Writing into File at the End of all operations*/
write_into_file("account.txt");
}
Explanation / Answer
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct bank
{
int accNumber;
string Fname, Lname;
float balance;
};
static int j = 0;
struct bank B[5];
void read_from_file(const char* filename)
{
/*Variable declarations*/
ifstream infile;
/*Opening file*/
infile.open (filename);
int i=0;
if (infile.is_open())
{
/*Reading all details from file*/
while(!infile.eof()) // To get you all the lines.
{
infile>>B[i].accNumber;
infile>>B[i].Fname; // Saves the line in STRING.
infile>>B[i].Lname;
infile>>B[i].balance;
/*To prevent read last line twice*/
if( infile.eof() )
{
break;
}
j++;
i++;
}
/*Printing all account details after reading*/
for (i = 0; i<j; i++){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << ' ';
}
infile.close();
}
}
int addAccount()
{
int accNum,i;
string fname, lname;
float balance;
cout << " --- Provide details --- ";
cout << "Acc Number: "; cin >> accNum;
for (i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
j = j + 1;
B[i].accNumber = accNum;
cout << "first name: "; cin >> B[i].Fname;
cout << "last name: "; cin >> B[i].Lname;
cout << "Balance:"; cin >> B[i].balance;
}
void deleteAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
for (int k = i; k<j - 1; k++)
B[k] = B[k + 1];
}
}
}
void searchAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
}
}
int withdraw()
{
float withd;
int accNum,flag=0;
cout << "Enter account number from you want to withdraw:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
flag=0;
cout << "Enter withdraw amount : ";
cin >> withd;
if (B[i].balance >= withd){
B[i].balance = B[i].balance - withd;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int deposit()
{
float dep;
int accNum,flag=0;
cout << "Enter account number you want to Deposit:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
flag=0;
if (B[i].accNumber == accNum)
{
cout << "Enter deposit amount : ";
cin >> dep;
B[i].balance = B[i].balance + dep;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int transfer()
{
float tran;
int fromAcc, toAcc, fr, to, i, f1, f2;
cout << "Enter account number you want to transfer from "; cin >> fromAcc;
cout << "to "; cin >> toAcc;
for (i = 0; i<j; i++)
if (B[i].accNumber == fromAcc){
f1 = 1;
fr = i;
}
for (i = 0; i<j; i++)
if (B[i].accNumber == toAcc){
f2 = 1;
to = i;
}
if (f1 == 1 && f2 == 1)
{
cout << "Enter amount to transfer : ";
cin >> tran;
if (B[fr].balance >= tran){
B[to].balance = B[to].balance + tran;
B[fr].balance = B[fr].balance - tran;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
void list()
{
for (int i = 0; i<j; i++)
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
/*Write into file*/
void write_into_file(const char* filename){
int i;
ofstream myfile (filename);
if (myfile.is_open()) {
for(i=0;i<j;i++){
myfile<<B[i].accNumber<<" ";
myfile<<B[i].Fname<<" "; // Saves the line in STRING.
myfile<<B[i].Lname<<" ";
myfile<<B[i].balance<<" ";
}
}
}
int main()
{
int op,i;
char ch;
read_from_file("account.txt");
do{
cout << "-- Operations menu --- ";
cout << " 1. Add an Account "
"2. Delete an Account "
"3. Search for an account by account number and display the account info "
"4. Make a Withdraw "
"5. Make a Deposit "
"6. Transfer money from one account to another "
"7. List all accounts ";
cout << "Choose the operation : ";
cin >> op;
switch (op){
case 1: addAccount();
break;
case 2: deleteAccount();
break;
case 3: searchAccount();
break;
case 4: withdraw();
break;
case 5: deposit();
break;
case 6: transfer();
break;
case 7: list();
break;
default: cout << "Invalid operation ";
exit(0);
}
cout << "Do you wants more operation (Y/y) by default No : ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.