Please show me how to separate the code below so I can complie and run it in C++
ID: 3589287 • Letter: P
Question
Please show me how to separate the code below so I can complie and run it in C++. I am having some complications. Thank you.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
//Class Client definition
class Client
{
//Data members
string name;
string accountId;
double balances;
int numberOfRecords;
public:
//Member function prototype
void readFile(Client[]);
void writeFile(Client[]);
void encryptFile(Client[]);
void decryptFile(Client[]);
void displayClient(Client[]);
string getName();
void setName(string);
string getAccountId();
void setAccountId(string);
double getBalance();
void setBalance(double);
};//End of class
//Function to encrypt file contents
void Client::encryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Encrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = (c[x].name[t] + '0');
//Encrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = (c[x].accountId[t] + '0');
//Encrypt account balance
c[x].balances = c[x].balances + 'a';
}//End of for loop
c[0].writeFile(c);
cout<<" ************* Encrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function
//Decrypt the file contents
void Client::decryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Decrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = c[x].name[t] - '0';
//Decrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = c[x].accountId[t] - '0';
//Decrypt the balance
c[x].balances = c[x].balances - 'a';
}//End of for loop
cout<<" ************* Decrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function
//Function to return name
string Client::getName()
{
return name;
}//End of function
//Function to set name
void Client::setName(string n)
{
name = n;
}//End of function
//Function to return id
string Client::getAccountId()
{
return accountId;
}//End of function
//Function to sat id
void Client::setAccountId(string a)
{
accountId = a;
}//End of function
//Function to return balance
double Client::getBalance()
{
return balances;
}//End of function
//Function to set balance
void Client::setBalance(double b)
{
balances = b;
}//End of function
//Function to display client information
void Client::displayClient(Client c[])
{
//Loops till number of records
for(int x = 0; x < numberOfRecords; x++)
{
cout<<" Name: "<<c[x].name<<" Account Id: "<<c[x].accountId<<" Balance: "<<c[x].balances;
}//End of for loop
}//End of function
//Function to write client information onto the file
void Client::writeFile(Client c[])
{
//ofstream object created to write
ofstream wFile;
//Open the file for writing
wFile.open("client.txt");
//Loops till end of the record
for(int l = 0; l < numberOfRecords; l++)
{
//Write each data member to the file
wFile<<c[l].getName()<<endl;
wFile<<c[l].getAccountId()<<endl;
wFile<<c[l].getBalance()<<endl;
}//End of function
//Close file
wFile.close();
}//End of function
//Function to read file contents and store it in the data members
void Client::readFile(Client c[])
{
//ifstream object created to read
ifstream rFile;
//Open the file for reading
rFile.open("client.txt");
int x = 0;
string data;
double bal;
//Loops till end of the file
while(!rFile.eof())
{
//Reads the data and stores it in the data members
rFile>>data;
c[x].setName(data);
rFile>>data;
c[x].setAccountId(data);
rFile>>bal;
c[x].setBalance(bal);
//Increase the record counter
x++;
}//End of while loop
//Close file
rFile.close();
numberOfRecords = x;
}//End of function
//Main function definition
int main()
{
//Creates an array of objects
Client client[100];
client[0].encryptFile(client);
client[0].decryptFile(client);
return 0;
}//End of function
Explanation / Answer
First of all this code is already in C++ language. So you just need a C++compiler such as Devcpp to compile it. I am sending you the same program with some modifications and hope that will help you.
If you want to separate the program then you can do that in the following manner:
1. Make a class client and all its data members and member functions all together.
2. make main() program outside the class
Following is the class Client:
class Client
{
//Data members
string name;
string accountId;
double balances;
int numberOfRecords;
public:
//Member function prototype
void encryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c,0);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Encrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = (c[x].name[t] + '0');
//Encrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = (c[x].accountId[t] + '0');
//Encrypt account balance
c[x].balances = c[x].balances + 'a';
}//End of for loop
c[0].writeFile(c);
cout<<" ************* Encrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function
//Decrypt the file contents
void decryptFile(Client c[])
{
int t;
//Read file contents and store it in class data member
c[0].readFile(c,1);
//Loops till end of records
for(int x = 0; x < numberOfRecords; x++)
{
//Decrypt each character of name
for(t = 0; t < c[x].name.length(); t++)
c[x].name[t] = c[x].name[t] - '0';
//Decrypt each character of id
for(t = 0; t < c[x].accountId.length(); t++)
c[x].accountId[t] = c[x].accountId[t] - '0';
//Decrypt the balance
c[x].balances = c[x].balances - 'a';
}//End of for loop
cout<<" ************* Decrypted File Contents **************** ";
//Display the contents
c[0].displayClient(c);
}//End of function
//Function to return name
string getName()
{
return name;
}//End of function
//Function to set name
void setName(string n)
{
name = n;
}//End of function
//Function to return id
string getAccountId()
{
return accountId;
}//End of function
//Function to sat id
void setAccountId(string a)
{
accountId = a;
}//End of function
//Function to return balance
double getBalance()
{
return balances;
}//End of function
//Function to set balance
void setBalance(double b)
{
balances = b;
}//End of function
//Function to display client information
void displayClient(Client c[])
{
//Loops till number of records
for(int x = 0; x < numberOfRecords; x++)
{
cout<<" Name: "<<c[x].name<<" Account Id: "<<c[x].accountId<<" Balance: "<<c[x].balances;
}//End of for loop
}//End of function
//Function to write client information onto the file
void writeFile(Client c[])
{
//ofstream object created to write
ofstream wFile;
//Open the file for writing
wFile.open("client1.txt");
//Loops till end of the record
for(int l = 0; l < numberOfRecords; l++)
{
//Write each data member to the file
wFile<<c[l].getName()<<endl;
wFile<<c[l].getAccountId()<<endl;
wFile<<c[l].getBalance()<<endl;
}//End of function
//Close file
wFile.close();
}//End of function
//Function to read file contents and store it in the data members
void readFile(Client c[],int nop)
{
//ifstream object created to read
ifstream rFile;
//Open the file for reading
if(nop==0)
rFile.open("client.txt");
else if(nop==1)
rFile.open("client1.txt");
int x = 0;
string data;
double bal;
//Loops till end of the file
while(!rFile.eof())
{
//Reads the data and stores it in the data members
rFile>>data;
c[x].setName(data);
rFile>>data;
c[x].setAccountId(data);
rFile>>bal;
c[x].setBalance(bal);
//Increase the record counter
x++;
}//End of while loop
//Close file
rFile.close();
numberOfRecords = x;
}//End of function
};//End of class
Following is the required main():
If you need array of clients the use loop to call encryptFile() and decryptFile() for each object of class client.
Then again if you want to keep all client data in a single file, then yo do not need to have object array of class client, just make one object and call the required functions with that object.
But please make sure that you have written all the client data in the fale "client.txt".
this file "client.txt" should be in the same folder as will be this program.
First code will encrypt all the details such as name,accoundid,balance and save that in the file "client1.txt".
i checked the code and it is working properly.
int main()
{
//Creates an array of objects
Client client[10];
client[0].encryptFile(client);
client[0].decryptFile(client);
return 0;
}//End of function
So first make two files "client.txt" and "client1.txt" in the same folder as the program is .
Entry some details in the "client.txt" such as 'max 123 2000' and :
1. compile the cpp program
2. check the output on the console
3. check the output saved in the "client1.txt"
thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.