Hello! i\'m trying to do a project with 5 programs in one, but I couln\'t make i
ID: 3837440 • Letter: H
Question
Hello! i'm trying to do a project with 5 programs in one, but I couln't make it work.
The programs are the followings
This is the code what I have so far
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
void Prog13_1();
void Prog13_3();
void Prog13_13();
void Prog13_15();
void Prog13_17();
int main()
{
while (true)
{
system("cls");
cout << " Main Menu - Chapter 13" << endl;
cout << "==============================" << endl;
cout << " 1: Programming Exercise 13.1" << endl;
cout << " 3: Programming Exercise 13.3" << endl;
cout << " 13: Programming Exercise 13.13" << endl;
cout << " 15: Programming Exercise 13.15" << endl;
cout << " 17: Programming Exercise 13.17" << endl;
cout << "other: Exit" << endl;
cout << "==============================" << endl;
cout << "Enter an exercise: ";
char exercise[2];
cin >> exercise;
cout << endl;
switch (atoi(exercise))
{
case 1: Prog13_1(); break;
case 3: Prog13_3(); break;
case 13: Prog13_13(); break;
case 15: Prog13_15(); break;
case 17: Prog13_17(); break;
default: exit(0);
}
cout << endl;
system("pause");
cin.clear();
}
return 0;
}
void Prog13_1()
{
ofstream output;
"Random.txt";
srand(time(0));
output.open("Random.txt", ios::out|ios::app);
cout << "Create a text file:" << endl;
cout << endl;
for (int i = 0; i < 100; i++)
{
cout << i;
}
output.close(); // Close file
cout << "Date appended" << endl;
}
void Prog13_3()
{
ifstream input;
int sum = 0;
double average;
input.open("D://scores.txt");
cout<<"Processing scores in a text file: "<< endl;
if (input.fail())
{
cout<< "File does not exist " << endl;
cout << "Exit the program" << endl;
//return 0;
}
int scores, count = 0;
/*Invoke the eof () function of the input object to detect it */
//Continue if it is not the end of the file
while(!input.eof())
{
input>>scores;
//Increment the count variable
count++;
sum+=scores;
}
// Calculate the average.
//avg=sum/count;
//Close the file.
//Close file
input.close();
//Display the sum and average of the scores in the file.
cout<<" Sum of the scores:" ""<< endl;
cout<<" Average of the scores:" ""<< endl;
//Pause the system
// system("Pause");
//Return the value
}
void Prog13_13()
{
fstream binaryio;
binaryio.open("Binary.dat",ios::binary|ios::app);
srand(time(0));
for (int i = 0; i < 100; i++)
{
binaryio;
}
binaryio.close();
cout << "Data appended" << endl;
}
class Loan
{
public:
// Default constructor
Loan()
{
interestRatePerYear = 9.5;
noOfYrs = 30;
loanMoney = 100000;
}
Loan(double rate, int years, double amount)
{
interestRatePerYear = rate;
noOfYrs = years;
loanMoney = amount;
}
double getInterestRatePerYear()
{
return interestRatePerYear;
}
int getNoOfYrs()
{
return noOfYrs;
}
double getLoanMoney()
{
return loanMoney;
}
void setInterestRatePerYear(double rate)
{
interestRatePerYear=rate;
}
void setNoOfYrs(int years)
{
noOfYrs = years;
}
void setLoanMoney(double amount)
{
loanMoney= amount;
}
double getMonthlyPayment()
{
double interestRatePerMonth =
interestRatePerYear / 1200;
return loanMoney * interestRatePerMonth/(1-
(pow(1/(1+interestRatePerMonth), noOfYrs *
12)));
}
double getTotalPayment()
{
return noOfYrs * 12*getMonthlyPayment();
}
private:
double interestRatePerYear;
int noOfYrs;
double loanMoney;
};
void Prog13_15()
{
double sum = 0; i= 1;
Loan;
fstream la;
a.open("Exercise13_15.dat", ios::in|ios::binary);
// read total payment from each object upto end of file
//is reached
while(!la.eof())
{
// read for each object
la.read(reinterpret_cast<char*>(&loan),
sizeof(Loan));
cout<<"The total payment of "<<i++<<" loan amountis: " << loan.getTotalPayment()<< endl;
// add all payments
sum+= loan.getTotalPayment();
}
// Closing file stream
la.close();
// Display sum of all loans
cout<<"The total amount of all loans is :"<<sum<<endl;
}
void Prog13_17()
{
int count1, num, parts, partsize;
double len;
// declaring variables
string filename,targetfilepartnum;
// Prompt user to enter filenames
cout << "Enter file name:";
// Read filename
cin >> filename;
fstream biout,bin;
// open file stream for read
bin.open(filename.c_str(),ios::in|ios::binary);
// Check for existence of file
if(bin.fail())
{
cout << "File does not exist Exit the program";
}
cout << "Enter the number of bites in each small file:";
// Read partsize
cin >>partsize;
// Check of exitence of file
bin.seekg(0,ios::end);
len = bin.tellg();
bin.seekg(0,ios::big);
parts = len/partsize+1;
char b[partsize];
for(int i=0; i < parts; i++)
{
targetfile=filename;
stringstream out;
out <<" "<<i;
partnum=out.str();
targetfile.append(partnum);
bin.read(b, partsize);
ofstream sf;
ofstream.open(targetfile.cstr(),ios::out|ios::binary);
for(int j=0; j < partsize; j++)
{
sf.put(b[j]);
sf.close();
cout << " File" << targetfile<< "produced";
}
bin.close();
cout << " split done";
}
Explanation / Answer
//Modifications are done to run the program .
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;
class Loan
{
public:
// Default constructor
Loan()
{
interestRatePerYear = 9.5;
noOfYrs = 30;
loanMoney = 100000;
}
Loan(double rate, int years, double amount)
{
interestRatePerYear = rate;
noOfYrs = years;
loanMoney = amount;
}
double getInterestRatePerYear()
{
return interestRatePerYear;
}
int getNoOfYrs()
{
return noOfYrs;
}
double getLoanMoney()
{
return loanMoney;
}
void setInterestRatePerYear(double rate)
{
interestRatePerYear=rate;
}
void setNoOfYrs(int years)
{
noOfYrs = years;
}
void setLoanMoney(double amount)
{
loanMoney= amount;
}
double getMonthlyPayment()
{
double interestRatePerMonth =
interestRatePerYear / 1200;
return loanMoney * interestRatePerMonth/(1-
(pow(1/(1+interestRatePerMonth), noOfYrs *
12)));
}
double getTotalPayment()
{
return noOfYrs * 12*getMonthlyPayment();
}
private:
double interestRatePerYear;
int noOfYrs;
double loanMoney;
};
void Prog13_1();
void Prog13_3();
void Prog13_13();
void Prog13_15();
void Prog13_17();
int main()
{
while (true)
{
system("cls");
cout << " Main Menu - Chapter 13" << endl;
cout << "==============================" << endl;
cout << " 1: Programming Exercise 13.1" << endl;
cout << " 3: Programming Exercise 13.3" << endl;
cout << " 13: Programming Exercise 13.13" << endl;
cout << " 15: Programming Exercise 13.15" << endl;
cout << " 17: Programming Exercise 13.17" << endl;
cout << "other: Exit" << endl;
cout << "==============================" << endl;
cout << "Enter an exercise: ";
char exercise[2];
cin >> exercise;
cout << endl;
switch (atoi(exercise))
{
case 1: Prog13_1(); break;
case 3: Prog13_3(); break;
case 13: Prog13_13(); break;
case 15: Prog13_15(); break;
case 17: Prog13_17(); break;
default: exit(0);
}
cout << endl;
system("pause");
cin.clear();
}
return 0;
}
void Prog13_1()
{
ofstream output;
srand(time(0));
output.open("Random.txt", ios::out|ios::app);
cout << "Create a text file:" << endl;
cout << endl;
for (int i = 0; i < 100; i++)
{
cout << i;
output<<i<<" ";
}
output.close(); // Close file
cout << "Date appended" << endl;
}
void Prog13_3()
{
ifstream input;
int sum = 0;
double average;
input.open("E://scores.txt");
cout<<"Processing scores in a text file: "<< endl;
if (input.fail())
{
cout<< "File does not exist " << endl;
cout << "Exit the program" << endl;
//return 0;
}
int scores, count = 0;
/*Invoke the eof () function of the input object to detect it */
//Continue if it is not the end of the file
while(!input.eof())
{
input>>scores;
//Increment the count variable
count++;
sum+=scores;
}
// Calculate the average.
float avg=sum/count;
//Close the file.
//Close file
input.close();
//Display the sum and average of the scores in the file.
cout<<" Sum of the scores:" ""<< sum<<endl;
cout<<" Average of the scores:" ""<< avg<<endl;
//Pause the system
// system("Pause");
//Return the value
}
void Prog13_13()
{
fstream binaryio;
binaryio.open("Binary.dat",ios::binary|ios::app);
srand(time(0));
for (int i = 0; i < 100; i++)
{
binaryio<<i<<" ";
}
binaryio.close();
cout << "Data appended" << endl;
}
void Prog13_15()
{
double sum = 0;
int i= 1;
Loan la;
fstream f;
f.open("Exercise13_15.dat", ios::in|ios::binary);
// read total payment from each object upto end of file
//is reached
while(!f.eof())
{
// read for each object
f.read(reinterpret_cast<char*>(&la),
sizeof(Loan));
cout<<"The total payment of "<<i++<<" loan amountis: " << la.getTotalPayment()<< endl;
// add all payments
sum+= la.getTotalPayment();
}
// Closing file stream
f.close();
// Display sum of all loans
cout<<"The total amount of all loans is :"<<sum<<endl;
}
void Prog13_17()
{
int count1, num, parts, partsize;
double len;
// declaring variables
string filename,targetfilepartnum;
// Prompt user to enter filenames
cout << "Enter file name:";
// Read filename
cin >> filename;
fstream biout,bin;
// open file stream for read
bin.open(filename.c_str(),ios::in|ios::binary);
// Check for existence of file
if(bin.fail())
{
cout << "File does not exist Exit the program";
}
cout << "Enter the number of bites in each small file:";
// Read partsize
cin >>partsize;
// Check of exitence of file
bin.seekg(0,ios::end);
len = bin.tellg();
bin.seekg(0,ios::beg);
parts = len/partsize+1;
char *b=new char[parts];
for(int i=0; i < parts; i++)
{
targetfilepartnum=filename;
stringstream out;
out <<" "<<i;
filename=out.str();
targetfilepartnum.append(filename);
bin.read(b, partsize);
ofstream sf;
sf.open(targetfilepartnum.c_str(),ios::out|ios::binary);
for(int j=0; j < partsize; j++)
{
sf.put(b[j]);
sf.close();
cout << " File" << targetfilepartnum<< "produced";
}
bin.close();
cout << " split done";
}
}
Note : For 3.15, Loan object input file must be available to processs the loan objects.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.