I need help making the readfromfile function use fstream ins
I need help making the read_from_file function use fstream instead of cout and malloc because i just get an abort message when i try to run my program with the function not commented out! Someone please help this is for a project i have due tomorrow! Here are the instructions to the Project. Thank You!
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include <fstream>
#include<string>
#pragma warning(disable:4996)
using namespace std;
struct bank
{
int accNumber = 0 ;
string Fname, Lname;
float balance;
};
static int j = 0;
struct bank B[5];
void read_from_file(const char* filename)
{
FILE *fp;
char *word[50];
int i, flag = 0, count = 0;
for (i = 0; i<50; i++)
word[i] = (char *)malloc(20);
i = 0;
fp = fopen(filename, \"r\");
while (fscanf(fp, \"%s\", word[i++]) != EOF)
{
count++;
if (count == 4) {
B[j].accNumber = atoi(word[0]);
B[j].Fname = word[1];
B[j].Lname = word[2];
B[j].balance = atof(word[3]);
j++;
i = 0;
count = 0;
}
}
for (i = 0; i<j; i++)
cout << B[i].accNumber << \" \" << B[i].Fname << \" \" << B[i].Lname << \" \" << B[i].balance << \"\ \";
fclose(fp);
}
int addAccount()
{
int accNum;
string fname, lname;
float balance;
cout << \" --- Provide details ---\ \";
cout << \"Acc Number: \"; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << \"Error: No changes should be made to the database\ \";
return 0;
}
return 0;
}
j = j + 1;
B[j].accNumber = accNum;
cout << \"first name: \";
cin >> B[j].Fname;
cout << \"last name: \";
cin >> B[j].Lname;
cout << \"Balance: \";
cin >> B[j].balance;
return 0;
}
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 << \"\\t\" << B[i].Fname << \"\\t\" << B[i].Lname << \"\\t\" << B[i].balance << \"\ \";
}
}
}
int withdraw()
{
float withd;
int accNum;
cout << \"Enter account number from you want to withdraw:\";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << \"Enter withdraw amount : \";
cin >> withd;
if (B[i].balance >= withd) {
B[i].balance = B[i].balance - withd;
cout << \"Total :\" << B[i].balance << \"\ \";
}
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;
}
}
return 0;
}
int deposit()
{
float dep;
int accNum;
cout << \"Enter account number you want to Deposit:\";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << \"Enter deposit amount : \";
cin >> dep;
B[i].balance = B[i].balance + dep;
cout << \"Total :\" << B[i].balance << \"\ \";
}
else {
cout << \"Error: No changes should be made to the database\ \";
return 0;
}
}
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;
}
return 0;
}
void list()
{
for (int i = 0; i<j; i++)
cout << B[j].accNumber << \"\\t\" << B[j].Fname << \"\\t\" << B[j].Lname << \"\\t\" << B[j].balance << \"\ \";
}
int main()
{
int op;
char ch;
do {
cout << \"-- Operations menu --- \ \";
cout << \"\\t1. Add an Account\ \\t\"
\"2. Delete an Account\ \\t\"
\"3. Search for an account by account number and display the account info\ \\t\"
\"4. Make a Withdraw\ \\t\"
\"5. Make a Deposit \ \\t\"
\"6. Transfer money from one account to another\ \\t\"
\"7. List all accounts \ \\t\";
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);
}
read_from_file(\"account.txt\");
cout << \"Do you wants more operation (Y/y) by default No : \";
cin >> ch;
} while (ch == \'Y\' || ch == \'y\');
return 0;
}
C201 Team Project 1 1 Goal The purpose of this assignment is to get you familiar with Streams and File IO, Your goal is to create a simple bank database (file) where all the individual accounts are stored in a file. 2 Requirements The following data should be stored comesponding to each account: Account Number: an integer number (must be unique for every account) Name: two words comesponding to the first and last name Balance: a float point number Your program should provide a menu of options that allow performing any of the following operations: Add an account Delete an account Search for an account by account number and display the account info Make a withdrawal Make a deposit Transfer money from one account to another List all accounts Your program should be coded in a way such that all previous changes are written to the file before any new operations are performed. 3 Testing Your program must be able to handle the following abnormal situations: For add an account option, if an existing account is entered, the program should respond with a message and no new account should be created For delete an account option, if the account number could not be found, a message should be displayed and no account should be deleted For withdrawal operation, if the account number does not exist or the balance is not sufficient, a message should be displayed and no changes should be made to the database For deposit operation, if the account number does not exist, a message should be displayed and no changes should be made to the database For transfer operation, if the source account number destination account number does not exist, or the balance of the source account is not sufficient, a be displayed and no changes should be made to the database 4 Notes Please ask the instructor if any part of the assignment is not clear. State your assumptions clearly. Document your code properly. Check and handle all error conditions related to file lo and logical errors related to account operations (e.g. overdraws). You must state clearly your team members at the beginning of the program. 5 Submission upload your source code files (cpp and h) using canvas. One team just needs to make one submission.Solution
//Tested on Windows OS with Dev C++
/**********************account.cpp*****************************/
#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 << \"\\t\" << B[i].Fname << \"\\t\" << B[i].Lname << \"\\t\" << 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 << \"\\t\" << B[i].Fname << \"\\t\" << B[i].Lname << \"\\t\" << 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 << \"\\t1. Add an Account\ \\t\"
 \"2. Delete an Account\ \\t\"
 \"3. Search for an account by account number and display the account info\ \\t\"
 \"4. Make a Withdraw\ \\t\"
 \"5. Make a Deposit \ \\t\"
 \"6. Transfer money from one account to another\ \\t\"
 \"7. List all accounts \ \\t\";
 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\");
 
 }
/*********************************output*****************************/
12345 Lalchand Mali 1000
 12456 Ramsingh Saini 1400
 -- Operations menu ---
 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
 Choose the operation : 3
 Enter account number: 12345
 12345 Lalchand Mali 1000
 Do you wants more operation (Y/y) by default No : y
 -- Operations menu ---
 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
 Choose the operation : 4
 Enter account number from you want to withdraw:12345
 Enter withdraw amount : 100
 Total :900
 Do you wants more operation (Y/y) by default No : y
 -- Operations menu ---
 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
 Choose the operation : 5
 Enter account number you want to Deposit:12345
 Enter deposit amount : 400
 Total :1300
 Do you wants more operation (Y/y) by default No : y
 -- Operations menu ---
 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
 Choose the operation : 6
 Enter account number you want to transfer from 12456
 to 12345
 Enter amount to transfer : 100
 Do you wants more operation (Y/y) by default No : y
 -- Operations menu ---
 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
 Choose the operation : 7
 12345 Lalchand Mali 1400
 12456 Ramsingh Saini 1300
 Do you wants more operation (Y/y) by default No : y
 -- Operations menu ---
 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
 Choose the operation : 1
 --- Provide details ---
 Acc Number: 15789
 first name: Sunneta
 last name: Saini
 Balance:9000
 Do you wants more operation (Y/y) by default No : n
--------------------------------
 Process exited after 108.1 seconds with return value 0
 Press any key to continue . . .
/*****************************account.txt******************************/
12345 Lalchand Mali 1400
 12456 Ramsingh Saini 1300
 15789 Sunneta Saini 9000
Thanks a lot. If you have any query please let me know













