Question

C++ code and also provide comments explaining everything Credit Card Debt The True Cost of Paying...

C++ code and also provide comments explaining everything

Credit Card Debt

The True Cost of Paying Minimum Payment

Write a C++ program to output the monthly payment schedule for a credit card debt, when each month nothing more is charged to the account but only the minimum payment is paid. The output stops when the balance is fully paid - remaining balance = 0.

Input:

Data input must be done in a separate function.

Input the following: credit card balance, interest rate on your credit card, and percent of minimum payment. Test data: 624, 24, 4

Data must be read from a file called lab6.txt, which you must create first and format it the way you see fit to store the test data listed above. You can use a text editor to create lab6.txt; Don't need to write a C++ program to create that.

Calculate:

A loop can be configured in main() to periodically calculate and output monthly data. This (calculate)function must be called from main() and within the loop.

For each month, calculate the amount of interest to be paid that month (adding this into balance before calculating minimum payment), total of interest paid, amount of minimum payment due for the month. And, lastly, update new balance.

If the calculated minimum payment is less than $15.00, then reset it at $15.00 - a practical threshold. Otherwise, the calculated value will become impractically smaller and smaller for many payment periods.

And, lastly, the final payment would be the remaining sum owed, final balance, its value would be less than $15.00.

Output:

This function must be called from main(), from within the loop.

There needs to be a separate function, to output the table heading, including data read from the input data file; see sample below.

For each period, the data output function should output month(count), balance at the end of the month, interest paid that month, minimum payment for that month, and the sum of amount of interest paid so far(accumulated).

All outputs must be redirected to a file.

For background information, see: http://www.bankrate.com/calculators/managing-debt/minimum-payment-calculator.aspx

Sample output:

Balance Owing: $ 624.00

APR as % 24

Percent for minimum payment as % 4

------------------------------------------------------

Month Balance Interest this month Minimum Sum of interest paid
1 611.02    12.48 25.46    12.48   
2 598.31    12.22 24.93    24.70   
3 585.87    11.97 24.41    36.67   
4 573.68    11.72 23.90    48.38   
...
57 31.80    0.92 15.00    388.14   
58 17.44    0.64 15.00    388.78   
59 2.79    0.35 15.00    389.13   
60 0.00    0.06 2.84    389.18   

  

Checkpoints

  1. Absolutely No arrays
  2. Use reference variables when and only when you must
  3. Must check for file existence/opening failure for input file when it is opened and close the file when the file access is done
  4. Include file header, use the template provided in Module 3D
  5. Headers and comments for main() and each function (read Module 3D under "Functions"). That's right, comment header for main() is a new requirement.
  6. You must have sufficient, meaningful comments in main() and all functions. Grading on your work will measure more than ever on:
    • i. both the quality and quantity of your comments along the code;
    • ii. architecture and sectional structure of your code;
    • iii. nomenclature;
    • iv. overall readability.
  7. Output must be formatted as shown above with decimal points lined up. Output must go to a file and that file must be uploaded as .txt. No cout object should be left in your program, or at least they should be commented out if they were for debugging and testing.
  8. Submit both the .cpp and .txt files - including the input data file as well so that I can see the format of it.

Additional Notes on the "write_file" function

If you want to access a file from within a function, the file stream object has to be in the scope of the function. So, if the file object is created in main() and then you call the function(the output function, for instance) the file object won’t be visible, not in the scope, after having entered the function.

There are two ways to make it work. First, create the file object in main() and then pass the file object as an argument into the function. Second, create and open a file object inside the function and then close it after file access; it is carried out "every time" the function is called.

To use the First method, the code would have these parts:

1. Create the file object in main() before calling the function:-

ofstream out_file;

out_file.open("lab_6_paymt_schedule.txt");

.

2. Call the function:-

write_output( out_file, month_of_paymt, balance_due, intr_paid, paymt_amt, total_intr_paid)

.

3. Close the file, in main:-

out_file.close();

4. The function header looks like this:

void write_output(ofstream &out_file, int counter, double balance, double monthly_interest, double min_paymt, double sum_o_intr)

To use the Second method, the code would have these parts:

1. The function header doesn't have the file object as a parameter:

void write_output(int counter, double balance, double monthly_interest, double min_paymt, double sum_o_intr)

2. Inside the function, every time when it is invoked, you create the file object, open the file as shown here:

ofstream out_file;

out_file.open("lab_6_paymt_schedule.txt", ios::app);

Note: The the second argument, or option, ios::app specifies that the write operation is to be "appending", not over-writing existing data in the file, since the output information header has been written to it earlier using another function; read the second paragraph of the Output specification above.

3. Close the file before exiting the function:

out_file.closed( )

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to output the monthly payment schedule for a credit card debt

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

// function declaration

void readData(ifstream &fin, double &card_balance, int &apr, int &min_payment_percent);

void calculate(double &card_balance, double monthly_interest_rate, double &interest, int min_payment_percent, double &minimum, double &total_interest);

void outputHeader(ofstream &fout, double card_balance, int apr, int min_payment_percent);

void outputData(ofstream &fout, int month, double card_balance, double interest, double minimum, double total_interest);

int main() {

               ifstream fin("lab6.txt"); // provide the path to input file

               // declare the variables

               double card_balance, interest, total_interest = 0;

               int apr, min_payment_percent, month;

               double monthly_interest_rate, minimum;

               // check if file can be opened

               if(fin.is_open())

               {

                              ofstream fout("lab6_out.txt"); // create an output file

                              readData(fin,card_balance,apr,min_payment_percent); //read the data from the file

                              monthly_interest_rate = ((double)apr)/12; // calculate the monthly interest rate

                              month = 1; // initailize the month

                              outputHeader(fout,card_balance,apr,min_payment_percent); // output the header to output file

                              // loop that continues till the card balance > 0

                              while(card_balance > 0)

                              {

                                             // calculate the interest, minimum payment and update the balance

                                             calculate(card_balance,monthly_interest_rate, interest, min_payment_percent,minimum,total_interest);

                                             // output the monthly data

                                             outputData(fout,month,card_balance,interest,minimum,total_interest);

                                             month++; // increment the month

                              }

                              // close the files

                              fin.close();

                              fout.close();

               }else // file is not present or doesn't have the permission

                              cout<<"Unable to open lab6.txt"<<endl;

               return 0;

}

// function to read the card balance, annual interest rate and percent of minimum payment from file

// inputs :

// fin : reference to the input file

// card_balance : reference to the card_balance read

// apr : reference to annual interest rate

// min_payment_percent : reference to percent of minimum payment

void readData(ifstream &fin, double &card_balance, int &apr, int &min_payment_percent)

{

               // read the card_balance , annual interest rate and percent of minimum payment from the file

               fin>>card_balance>>apr>>min_payment_percent;

}

// function to calculate the amount of interest to be paid that month, total interest paid and amount of minimum payment due

// update the balance based on the interest and minimum paid

// card_balance : reference to the balance of the card

// interest : reference to the calculated interest

// minimum : reference to the minimum payment due

// total_interest : reference to total interest accumulated so far

void calculate(double &card_balance, double monthly_interest_rate, double &interest, int min_payment_percent, double &minimum, double &total_interest)

{

               interest = (card_balance*monthly_interest_rate)/100;

               card_balance += interest;

               minimum = (min_payment_percent*card_balance)/100;

               if(minimum < 15) // if minimum payment is < 15

               {

                              if((card_balance - 15) > 0) // check if balance is > 15 then set minimum to be 15

                                             minimum = 15;

                              else // else set minimum to card_balance

                                             minimum = card_balance;

               }

               card_balance -= minimum;

               total_interest += interest;

}

// function to output the table header to the output file

// fout - reference to the output file

// card balance - current balance of the card

// apr - annual rate of interest percent

// min_payment_percent - percent of minimum payment

void outputHeader(ofstream &fout, double card_balance, int apr, int min_payment_percent)

{

               fout<<"Balance Owing : $"<<fixed<<setprecision(2)<<card_balance<<endl;

               fout<<"APR as % "<<apr<<endl;

               fout<<"Percent of minimum payment as % "<<min_payment_percent<<endl;

               fout<<left<<setw(80)<<string(100,'-')<<endl;

               fout<<left<<setw(10)<<"Month"<<right<<setw(10)<<"Balance"<<right<<setw(25)<<"Interest this month"<<right<<setw(10)<<"Minimum"<<right<<setw(20)<<"Sum of interest paid";

}

// function to output the monthly data to the output file

// fout - reference to the output file

// month - month number

// card_balance - current balance of the card

// interest - current interest for the month

// minimum - current minimum payment

// total_interest - total interest accumulated so far

void outputData(ofstream &fout, int month, double card_balance, double interest, double minimum, double total_interest)

{

               fout<<endl<<left<<setw(10)<<fixed<<setprecision(2)<<month<<right<<setw(10)<<card_balance<<right<<setw(20)<<interest<<right<<setw(10)<<minimum<<right<<setw(20)<<total_interest;

}

//end of program

Output:

Input file:

Output file:

Add a comment
Know the answer?
Add Answer to:
C++ code and also provide comments explaining everything Credit Card Debt The True Cost of Paying...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the...

    Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...

  • Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

    Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...

  • 1. Suppose you wrote a program that reads data from cin. You are now required to...

    1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...

  • Write a C++ program to manage a credit card company with at least one ADT (Account)...

    Write a C++ program to manage a credit card company with at least one ADT (Account) with the following members: card number, customer name, credit limit, and balance. • The customer can pay the total amount of his/her balance or part of it. • The customer can make a purchase using the credit card. • The user can create, modify, and delete accounts. • All new accounts are created with $300 credit limit. • Customers’ data is stored in a...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

  • Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...

    need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after the decimal point. Process and sample run: a) Function 1: A void function that uses parameters passed by reference representing the name of a data file to read data from, amount of principal in an investment portfolio, interest rate (any number such as 5.5 for 5.5% interest rate, etc.) , number of times the interest is compounded, and the number of years the money is...

  • c++ programming no use of classes use of functions pseudo code with comments Requirement: Provide one...

    c++ programming no use of classes use of functions pseudo code with comments Requirement: Provide one application for a business store that sale 2 models of one proo model FA218 and model FA318. The application first provides the menu to allow users can select one of the following tasks per time and when they finish one task, they can continue the application to select other task from the menu until they choose exit. All the output on the screen and...

  • In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops...

    In C++, Please help me compute the following flowchart exactly like this requirement: Use while loops for the input validations required: number of employees(cannot be less than 1) and number of days any employee missed(cannot be a negative number, 0 is valid.) Each function needs a separate flowchart. The function charts are not connected to main with flowlines. main will have the usual start and end symbols. The other three functions should indicate the parameters, if any, in start; and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT