CMSC 362: (Re)writing a CMSC 161 Lab in C++
Overview
Calculate the interest earned on a certificate of deposit (CD) over a specified period. Prompt the user for an initial balance, an annual rate, and the number of years the money will be invested. Output a table that shows the starting balance, interest earned, and updated balance at the end of each year.
Use at least the REQUIRED METHODS listed below, with the EXACT same SPELLING for method names and parameters.
Input Specification
Ask the user for an initial balance, annual rate, and the number of years the money will be invested. The balance and rate will be real numbers, while the number of years must be an integer.
Use PRECISELY the format below with the EXACT same SPACING.
Please enter the initial balance: <user input>
Please enter the interest rate : <user input>
Please enter the number of years: <user input>
Output Specification
Output an introductory message, prompts, and then the table. The sample output below assumes the user entered an initial balance of 1000, an interest rate of 6.50, and a number of years of 10. Note well the FORMATTING (spacing and justification) used. You may assume numbers will not overflow their columns.
This program will calculate the interest earned
on a CD over a period of several years.
Please enter the initial balance: 1000
Please enter the interest rate : 6.5
Please enter the number of years: 10
Year Balance Interest New Balance
---- ------- -------- -----------
1 1,000.00 65.00 1,065.00
2 1,065.00 69.23 1,134.23
.
.
.
10 ...
Required Methods
Include at LEAST the following methods.
// Inform user what program will do
void printIntro ();
// Print the header followed by all the rows.
void printTable (int numRows, double balance, double rate);
// Print one row of table. "interest" is a dollar amount,
// not a rate.
void printRow (int rowNum, double balance, double interest);
// Calculate interest given a balance and percentage rate (like 7.2%)
double calcInterest (double balance, double rate);Hints
Do NOT worry about formatting the table until you get the logic
correct. Once you do get the logic correct, use
std::print/std::println from C++23. Using
printf from C is NOT permitted and will result in a
ZERO.
The following width guide may be of use:
111111111122222222223333333333444444
123456789012345678901234567890123456789012345
Year Balance Interest New Balance
---- ------- -------- -----------
std::print
First, have a look at this cppreference page that describes a detailed overview of formatting standard types.
You also need to set the global locale (recommended) so you don’t
need to set it with every invocation of print. You should do so once in
main (). See std::locale::global.
You can also use "en_US.UTF-8" when creating a new
locale.
To do locale-specific formatting, remember to include L
as part of the formatting specifier. Doing so should automatically get
the commas to be added to the numeric values.
Style
Use good programming style:
- Write comments
- Choose mnemonic, meaningful variable names (e.g. balance, interestRate)
- Remember to include a comment block at the top of your program that
includes:
- Your name
- The course
- The last date of modification
- The assignment name
- A brief description of what your program does
- FORMAT YOUR CODE CONSISTENTLY. On the course page I have a
.clang-formatfile. Once your program is written and works, you can automatically format your code by doing the following:- Download the file to where your C++ source file is located
- Rename the file to
.clang-format - Run the following command from the terminal:
clang-format CDInterest.cpp > CDInterest-formatted.cpp(this assumes your file is calledCDInterest.cppand creates a formattedCDInterest-formatted.cppfile) - Inspect the new file and make sure it works
- Overwrite your original file with the
mvcommand:mv CDInterest-formatted.cpp CDInterest.cpp - Once you are more comfortable, steps 3 through 5 can be invoked with
a single command:
clang-format -i CDInterest.cpp
Submission
Please submit a single C++ source file with a .cpp file
extension to autolab
Grading
- [25pts] Your program is designed properly and is functionally correct
- [10pts] Your program is cleanly and consistently formatted
- [10pts] Your program meets the EXACT spacing/formatting requirements for output
- NOTE: if your program does not compile/run, the highest score you will earn will be a 10/45