Pattern Finder C++

Pattern Finder C++ 1

Given this string that is the sequence:
TGGTTTAGGGCCTATAATTGCAGGACCACTGGCCCTTGTCGAGGTGTACAGGTAGGGAGCTAAGTTCGAAACGCCCCTTGGTCGGGATTACCGCCATTCTAGTAGTCTAACCCCGAACGCGCTCAGGCTTTGAGTTCGCGCAGCATTAAGAAGTCCATGCCGGCACCGAATGTCCCGACGACAGGCAACCAGCACGGATACCCGCCTTGAAGGCGCAATCAGTAGGTCGAGTTACAGAGGCTCCCCCCGAGCTTGTGCTTCCATTGAGTAGGGGCTATAGATATGTAGCACTCAGGTTTAGTAGCGCCCTTTTAACAGCGAGAGCCCGCCTGGTCAGAACCGAACGGCTGATACGCGAGCTGATGGCTAGAGGATGAACACGGTCCTTCTCTTCGCTTCGATCCGGGGTAGTTTTGTAGCGAAGGATAACGCTCTGTGGATTCTCCGAGAATAATCATCAGTACGGTGTGCGTACCCTCTCTTTGATCCACGCCTGGGGCTGGACATAGTCAGGCGCATTTCATCTACTTAACCCCGGTAAGGGCCACGGGCGCGACATCTCCTTACCAGGGTTGTCTTATGCTCGCTTTTCCCAGATGATAAGCATCTCGTTGTAATGAACAGGTACCTAAGAAAACTGAGTTTCGACGACCCGTCGGCTCGTGTTCTTATCTATTGATCTAACCGAGGTGAAGCTCGCCAAAAATTTCGTAATGTAAGAGAGAAATTGAAGGGGTGAATTTTGCACTCTCGTGCATACGTCTTGCTACAATAGCAAGAGCTGTATGCGTGCGACCACTTCACTACCTCTATAGCATGCGATCTCGCAGCCCGGATTGTCGCCTCCTTTGGGCCGCAAAAACGGTATACGGACACACTGCATCTGTGAGCACGCACCAACTCGATGCGCGTAATAGGCATCTGCTCCACCCAGGGGGCAAGGGCACCTGAGGACGATCTGTTCCGAACAGTATATTTGAGCCAATGTTCTATTAGTGAAGGCAAGGTGAGGGCAACTCACACTGGTACCTCGAGGAGTAATCCGATCCATCTAAACTGGCATGCCCGTTCGAGCTCGGGGGGTTCTTGAATTTAGCTGCGTGTACCTCAGGTCTCCTGAAGGTGACACCAAAGTACAAGATAGATAGATAGATAGATAGGTAATATCTGACGTGAATTGCTTACCGCTAGTGAGCGATTTAGGTCACGTCCTCTGAAGATGTACCGTGATCATCGATGAAATGGCTCTGCAGAGCGTTGCTCTTAACTTAGTGGGAGTGTTCCTTTGCAGTCTGATGAAGTCGCTGCATGTTAACTTTGCACTAAGGGCGTTTCCGAACCCTATAGTCATCCTTATTGATTCGCCCTGTCTTACCCAGGATACACTACCGTTCGAGGCTCTTAACGTACCAACGCATGCAGTCAGAAGATGATTCACCATCCAAGCAAATCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTCTGTGGCTTTAAAGCACCGAATGTAGTTGGCCATCGGTCTCGGTCACCAATAAGACGGCCTCGTGGGTCACTCGGTCGATGATCTAGGGTCGGGTGCATAGTGTTTCAGGTCGGCGCTCAGGGTTCTTGTCAGGGAAATCTACGGGTGAGTTGGAAAGCGCCGCCAGCGAGATGCCTGTAGGCGATTAGTGTAGAGAGAGCAACATCGGAAAATTGTCCGTGGGGCGCTACGTAAGTGTTCCCAGTATTCTCGTCCAGAGTAAGTCATGCATACCAGTATCAGGCGTCTGTGTGTTACGTTGCAGTGTATCCCGGTAGCGGGAAGCGTATAGAGCGTAACAGACCTGTCCTACAGCACGCAGGATGTCGACCCTTTCTCAGGCACGATACTTCGTGTAACAGCAGTTCCGGTGTCATCTGTAACTGTTCTGTGTTCCATAGTGAAGATATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCGGGCGACCCTGGCAATCAGGCGCCTGCTTATATGACAATTTGTCGCAATACGGGTCGCAGATGTATTGTCCGCATGAGTTTACGGTATCCGGAACTGTCACCGCCGATCCAATATGATCGCAAATCGGGGTGACACAATGGACCGCCGTAGATAAACCCTGCGATGCTGCAATAAGGATATGATATCGCGCGCGGGCCGTAAAACCGATCTTGGAAGGCGGGAAGTCTCCGGGAAAAACTCTCTGATAAAGCCTATTACAAGAAGAGCTCGAAGGCAAGATGGGCATGCCCCGTCGACCACACGGGCAAGCTCTGAGAATCGATGTGGTCGCTTAACCAACCCATACGGAGTGAACGAGACCACGCGGGCGGTTCTTGGTACGCATGATTCCTATTGGTTCTGCCGGGCGTGTGCAGGATTGTTCACTCCCCACCCTGTCGCTCACGAACGCGCTGGTTGCTTAAACCGACCGGAAATTCTGTAGCCGCCCCGTAAGTTTAACGCTTTGAAATACTCCACATGTGCGTACCGGGTCTGATCGCTTACGTGGCGCCACTATGTTAGGAGCTCATAGATATCGATGAATCAAATGTCTTTCATCGCTCCTTAAACAACCTGACGTATTCGCAAAATTGCGCGTATTGAGAAGGGAAAGTTAAAGGAACGATAACAATGAGTCTGCTTTCACCGGCTGCATAACGGGATCGCGCGCTATGGGATTTCCTAACTATAATTCGTGTCGATACTCAGACGCGTTGTACAGGTAAGAAGTCGGCGGGACAGTATTTGAGAAGGGGCTCTGCGGCACCAACGCCGAGCTGTATCAGGGGGGTTAATGTGTAGCGGGCATATAACACAATACAGCCCGCGGCGCGTCGTGGTTACCGTAGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATGGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAAGAAACACCGGAGCTAGGATCCACGACTACCAGTGGGAAACTGTGAATTGTGCATGGTAATTAAAGGATGACTGGTCAACACCGGTCTCCACGGGCGTTAAACAACCTCGCTCCAGTCAATCTCTAGCGGTGGTTGTGGCAGCTTATTCCTGGAGGTAATACTCTTCCGGGCCCACTAAAAATGTAACGAAGTCGAGGTTGGGTCAGGGGATTGAGTGGGGGCGACTCACTGATTCCACCAGGAATTGTCGTCAATCGCGACGTACTTTGAGCCTTGTATCTTGGCGTTTCTTGTTGGTACGCGGCCGTGTTCGTGAATCACGACGTCGTTCATGATTCATCCGTCCAAGCCTAGACCTAGCGTAAAAACGGTGTCGATCTGTGCTCCAACCGATGGATGGTTTTTACACAAGTGAACTTCGAGGCTGTGGGACAAACAGCACAACTTGTTCACTGCTGACCGTGGTACTAAACCACGCTTGCTTTCAGCCCTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTTTTTTTCTCAGATGCGGATCAAGGGTTACTCGAGCCGTTCTGAGGTCCTAAAATTTTAGCCCTTGGTGTTAGCTTCGGTTTAAGAACGTAGGTGCGACGCGGGGGTCCTAGAGCTCCGCGATCTGCACTCCCCACCTGGCACCAAAACGAATCCTGCATAACGGCTCTCTGTGCATGGGGGATGGTCGCAACAACGAGCATAGCTGGCATCACTTCGTTTGCTGTGGATTGCTGTTTTATACAGAATACGGTGGTGATCATCAAAGGAAGCATAATCCACATCGGGCACCCCGGGCCATCGTGCGTTCCCTTATAGCCGGCTTGCATGTTGGGGGAGGAGTAAGGCCGGTAACGTCTCGCAGCACTGTCGCGTAACACAGGTACATCTTTATTTCCGGTGCTGTAGAAGTGGTTTTTCGAAGGCGTAACCCAGAACGACTGATATAATAGTCCACTATTCCCTGGTTTAAGACTTCTACAAAGTTTTACGCAAAGTTACATGCACACTCGGCGACGTAAATATTAGCCTTGCTAAATTGCCACGGATATTAATCCCGAGCCAACCTGTTCCCACTAGCGGTCTACGGTCATAGTCCTTTGTGTAGAGCGTCATTGCGGTTGGGGCCCGTCCGCGGAGGTTCCCCTTATGATCTAACCGCGGTGCAGGTTGACTGAATGCCATACACTATAGAGAAGACGTCTAAGTAGAAACGTTCTTTAAAAATCTTGAACTGACGGCCGAGTATTATCAAGAGAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCAGATCATGCACAATAAGGTTAGAAGCAGCAAGCATGTATTCTTTGCATAGAGGCGGTAAAGCCGCCTTGCATACCCAGCAGCAGCCGCGAAGGCCTTACTCCAGAGGACAGAACTTCTCACACAGCGTCCGCATACACCGCGGACGTGACAAGGTTAGATAGCTCTAGTTTGCGGCAACCCTCGCATCAGGCCGACTCACCCGCGCTTGCTACCCGGAGGATGGGTCAAGGGATAAACATAGCACGTTAGTTAAGCCTAACGTCAGTTTTTAGAGTTTACATGCACGACTAAGTGCATCGAAATACACGCCGTTGACAGACCAACAGCGTGTCAACTGGGCCTTGAGAATTGTATCATAATAGCCAAATACGAGGCCAAGTAGTCCGACGAGAGGCACGTAGAGACCACTTTCCCTAAACGATCTGTCGCATTACCCTTTGACTCGCACCCTATGCCTTATGTTCCAAGCAGCACCGAAGTTAGATTTAAGGGCGTATCTATCGGTACCTCGGTTGGGCCGGTCCACAGCTCCAGCTGAATTAGTGCTCACCCCGCTTCGAGGTTGAGTAAGGGTCACTTTTAAAAATATGCTTAAGGGTGATTCACATGACAGTAATCGAATAGTGAGATATAAGTAGGTGCGCCCCGCGCACACATCAAAACTGTGCAGACTGAAACTGAATGCTGGAGGCTGAGGAAAATGAAGATCAGAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGTCTAGAGAAGAGTGAATGAATAGATCTGCCGCTGAATCCCCGCGTGAGGTTTTTGCGAC

Get the maximum consecutive occurrences of the pattern “AGATC”
The result should be 37 because it’s the maximum consecutive occurrences given that pattern

Lab19C

Lab19C 2

Write a program called Lab19C that searches for a number in an array.
a. Write a bool function named search that receives 3 parameters: an int array, an int containing the length of the array, and an int number to search for. Return true if the number is ***** the array and false otherwise.
b. Write a void function called fillArray with the array as a parameter that uses a for loop to read the text file (Lab19C.txt) 20 times and place the values in the array.
c. In the main function, do the following:
i. Declare an array of 20 integers.
ii. Ask the user for a number to search for in the array
iii. Call fillArray sending the array as a parameter.
iv. Call the search function and send the array, the length of the array (20), and the search number as parameters.
v. Print the search number and the value returned by the function along with appropriate label to say if the number was found or not.

RandomMaxPerfectSquare

RandomMaxPerfectSquare 3

Problem: Given a positive integer to serve as the seed value for the random number generator, generate 15 random
numbers between 1 and 1,000,000 inclusive and display in reverse order (from last random number (array index #14) to
first random number (array index #0)) the largest perfect square greater than one (22
= 4, 32
= 9, 42
= 16, 52
= 25, 62
= 36
and so on) that is a divisor of the current random number. The use of a single integer array of size 15 is required.

Additional Requirements:
1. Add the homework assignment header file to the top of your program. A description of your program will need to
be included in the assignment header.
2. Each of the example executions provided for your reference represents a single execution of the program.
Your program must accept input and produce output exactly as demonstrated in the example executions. Your
program will be tested with the data seen in the example executions and an unknown number of additional tests
making use of meaningful data. The only input validation requirement in this problem is demonstrated in the final
example execution.
3. For this assignment you will be required to implement the user-defined functions (from chapter 4). Failing to
follow course standards as they relate to good user-defined function use will result in a zero for this assignment.
4. Revisit course standards as it relates what makes for good use of user-defined functions, what is acceptable
to retain in the main function, and when passing parameters by address is appropriate. In many cases userdefined function use should result in a main function that only declares variables and makes function calls.
5. Course standards prohibit the use of programming concepts not yet introduced in lecture. For this assignment you
may consider all material in the first eight chapters of the book, notes, and lectures to be acceptable for use.
◦ The use of any dynamic array structures (chapters 9 and 10) would violate this requirement and result in no
credit being awarded for your effort. See course standards below for array declaration expectations.

Lab18B

Lab18B 4

1. Write a new program called Lab18B that will read strings from a text file and turn them into “encrypted code”. (This will be for 10 pts extra credit, if it is completed & works.)
a. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise.
b. Create a string function named encrypt that will accept a string variable as a parameter and return the string with all the vowels deleted from it and a * in place of every space. Use the isVowel function inside your loop here.
(Ex – Rainy days and Mondays would become Rny*dys*nd*Mndys)
c. In the main function do the following:
i. Read an unknown number of strings from the text file (Lab18B.txt). Use getline to do this because the strings should include spaces.
ii. Follow the flowchart below to design your main function
iii. Make sure to include labels for everything printed, and print a blank line before you read a new string from the text file.

Lab18A

Lab18A 5

1. Write a program named Lab18A that will play Blackjack.
a. Define an enumerated type, cards, that has the values ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING. (Do this just above the function prototypes, before all the functions.)
b. Create an integer function called housePlay that will return a random integer between 2 and 21. (No parameters) (Note: put your srand(time(0)) statement at the beginning of the main function, and only do it once.)
c. Create an integer function named randomPick that will return a random number between 1 & 13. (No parameters)
d. Create a void function named drawTheCard that will accept a cards parameter and will draw the card for that value. (Sample cards shown below.)
e. Create an integer function named getPoints that will accept a cards value as a parameter and return the number of points for that card. (ACE = 1, JACK/QUEEN/KING = 10, all other cards = their face value)
f. Create a void function named PlayerPickCard (with one integer reference parameter named totalScore.) It will
i. call randomPick
ii. using the number from randomPick, determine the cards value based one the following table. Use a switch structure to determine the cards value.
1 – ACE 2 – TWO 3 – THREE 4 – FOUR 5 – FIVE
6 – SIX 7 – SEVEN 8 – EIGHT 9 – NINE 10 – TEN
11 – JACK 12 – QUEEN 13 – KINGiii. call drawTheCard sending the cards value as a parameter
iv. call getPoints sending the cards value as a parameter and add the returned value to totalScoreg. In your main function:
i. Call housePlay to determine the house’s total score. Print the house score with a label.
ii. Call PlayerPickCard twice sending a player score variable as a parameter.
iii. Ask the user if they want to pick another card. (They only get one more.) If they say yes, call PlayerPickCard again sending the player score variable as a parameter. (Do #’s iv & v, whether they picked again or not.)
iv. If the player score is over 21 print the score and a message saying they busted
v. Otherwise compare the player score to the house score. The one with the highest score wins. Print the player score along with a message saying who won.***** ***** *****
* A * * 2 * * J *
***** ***** *****

Lab14B C++

Lab14B C++ 6

Create a new program called Lab14B that will create a multiplication table for an input number.
a. Create an integer function named getNumber (with no parameters)
i. It should ask the user to enter a positive integer between 1 and 10.
ii. In the function write a while loop that will keep asking for a number until the user enters one that meets the requirements.
iii. Then return the number.
b. Create a void function called printTable that will accept an integer as a parameter and will print out a multiplication table for that number like the example below. You need to use a loop to do this.
(If the input number is *****)
1 7
2 14
3 21
4 28
5 35
6 42
7 49
8 56
9 63
10 70c. In the main function, call getNumber to get the number for your multiplication table and then call printTable (using the returned number from getNumber as the parameter) to print it.

Prime137

Prime137 7

Complete my program (Lab1.c program) such that it prompts the user for an input value in
form of a long int and prints all prime numbers that end with digits 137
between 2 and the user-provided input. Do not modify any lines of code already
written in the program. Also, do not add any other functions, #include
directives, or macros to this program.(Lab1.c program)
#include
#include
#include/* returns true if n is prime, otherwise false. */
bool isPrime(long int n)
{
}/* returns true if n ends with 137, otherwise false. */
bool endsWith137(long int n)
{
}int main(int argc, char* argv[])
{return 0;
}

CarClass

CarClass 8

You’ll design a class named Car that has the following fields:
yearModel—An Integer that holds the car’s year model
make—A String that holds the make of the car
speed—An Integer that holds the car’s current speed
The class should have the following constructor and other methods:
The constructor should accept the car’s year, model, and make
as arguments. These values should be assigned to the object’s
yearModel and make fields. The constructor should also assign
0 to the speed field.
Design appropriate accessor methods to get the values stored in
an object’s yearModel, make, and speed fields.
The accelerate method should add 5 to the speed field each time it’s called.
The brake method should subtract 5 from the speed field each time it’s called.

EmployeeData CPP

EmployeeData CPP 9

In this homework assignment, you will write a C++ program to store employee data in a List (ADT).You can study List(ADT) functions from your textbook at Chapter 3. Every employee has id, first name, last name, phone number, email address, salary. EmployeeTypeAttributeemployee_idintFirst namestringLast namestringPhone numberstringEmail addressstringsalarydoubleYou will implement the Employee class in a studentName.cppfile. I expect you to have1 file: studentName.cpp.So, Employee class definition will be in the studentName.cppfile.You will read the data from a employeeData.txt file given in the homeworkdescription in Canvas.The data is separated by space, you don’t need to do any string split operations.Once you start reading data from the file, the employees will be insertedto List ordered byemployee_id.This means a new employee may be inserted between two existing employees in the list. At this point, you should use the “insert” function of List class. The insert function takes an iterator as an input to identify the correct position. You can refer Chapter 3.3.1in the textbook. Please note that I do not accept if you add all the employees in random order and use a sort function later. If youdo it, you will lose substantial amount of credit. Whenyou insertall employees, you will print your employee List starting from the first element to the last. You should print all information of each employee. Here once you print the list, you should see employee information ordered based on id.

Loan Payment Calculator

Loan Payment Calculator 10

Loan Payment Calculator

The purpose of this assignment is to get you back into programming and give you some practice with some C++ topics that you should be familiar with from CSIS 111 as well as a new concept, random number generation.

Overview:

You are working as a computer programmer for a mortgage company that provides loans to consumers for residential housing. Your task is to create an application to be used by the loan officers of the company when presenting loan options to its customers. The application will be a mortgage calculator that determines a monthly payment for loans.

The company offers 10-, 15-, and 30-year fixed loans. The interest rates offered to customers are based on the customer’s credit score. Credit scores are classified into the following categories:

Table 1: Credit Score Categories
Rating Range
Excellent 720-850
Good 690-719
Fair 630-689
Bad 300-629

The program should initially prompt the user (the loan officer) for the principle of the loan (i.e. the amount that is being borrowed). It should then ask him or her to enter the customer’s credit score. Based on the customer’s credit score, the program will randomly generate an interest rate based on the following ranges:

Table 2: Interest Rate Assignments
Rating Interest Rate
Excellent 2.75% – 4.00%
Good 4.01% – 6.50%
Fair 6.51% – 8.75%
Bad 8.76% – 10.50%

The final input should be the number of years that the loan will be outstanding. Because the company only offers three different terms ( 10-, 15-, and 30-year loans), the program should ensure that no other terms are entered.

Formulas

Payment Calculator
[Adapted from Wittwer, J.W., “Amortization Calculation,” From Vertex42.com, Nov 11, 2008.]

The formula to calculate the monthly payment for a fixed interest rate loan is as follows:

where
A = payment Amount per period
P = initial Principal (loan amount)
r = interest rate per period
n = total number of payments or periods

Example: What would the monthly payment be on a 15-year, $100,000 loan with a 4.50% annual interest rate?
P = $100,000
r = 4.50% per year / 12 months = 0.375% (or 0.00375) per period
n = 15 years * 12 months = 180 total periods
A = 100,000 * (.00375 * (1 + .00375)180)/((1+.00375)180 – 1)

Using these numbers in the formula above yields a monthly payment of $764.99.

Requirements:

To receive credit for this assignment, certain programming features must be implemented in such a way as to demonstrate your knowledge of random number generation, use of enums, proper formatting of output, input error checking, switch statements, and if statements.

1. Start your program by prompting the user to enter a principle amount. The data type of this number should be a double. The amount must be positive and it must also be a numeric value. For example, when prompted to enter a number, if the user enters “abc,” the program will not be able to process the loan appropriately. Likewise, if the user enters -100000, the program will again produce erroneous results.

2. Prompt the user to enter the customer’s credit score. Again, appropriate error checking is essential here. In addition to ensuring that values are numeric and positive, you must also ensure that the credit score entered does not exceed 850. If a non-numeric, negative, or score that exceeds 850 is entered, prompt the user to re-enter the score.

If the credit score is below 300, display an error message to the user stating that the loan cannot be offered and exit the program. Make certain that the error message is displayed for a long enough duration for the user to read it before the program closes.

3. Prompt the user to enter the term of the loan. Valid terms are 10, 15, and 30 years. Any other numbers should be rejected, and the user should be prompted to re-enter an appropriate value.

4. Once the inputs have been entered and validated, your program must generate an interest rate to be used in the loan calculation.
a. Create an enum to represent the possible credit ratings: EXCELLENT, GOOD, FAIR, and BAD.
b. Assign the appropriate enum to the customer based on the ranges listed in Table 1 above. I recommend using if-statements for this.
c. Using a SWITCH statement with the enum values as cases, assign an interest rate based on the customer’s credit rating.
i. Within each case, use a random number generator to create an interest rate for each category based on the ranges listed in Table 2 above. Because the random number generator that you have been taught generates only integer values, you will need to generate a number in the hundreds and divide that number by 100 to get a result within the valid range for interest rates. Be careful with integer division here. Remember that an int divided by an int equals an int (i.e. int/int=int). To arrive at a double, you must make either the numerator or denominator a double to generate an interest rate with a decimal portion.

5. To perform the calculation for a monthly payment, create a function called CalcPayment that receives the principle, interest rate, and number of years as parameters. The function should return the monthly payment that is calculated.

Your program should check for invalid data such as non-numeric and non-positive entries for principle, credit score, and term. Use proper indentation and style, meaningful identifiers, and appropriate comments.

General notes about error checking
As in all applications, you should design your code to be robust enough to handle anything that a user may enter. In this program, you will prompt the user to enter three numbers: a principle amount, a credit score, and a number of years (i.e. term). Because users can make mistakes, you need to make sure that they enter numbers for these inputs (as opposed letters or other characters) and that any numbers entered are reasonable. For example, what if a user entered -10000 as the principle amount? Or negative years? What is a negative year anyway? It just doesn’t make sense.

Therefore, you may use the following code to determine if a non-numeric or negative number has been entered:

int num;
cout << "Enter an integer: " << endl; cin >> num;

while (cin.fail() || num < 0) { cout << "You must enter a number, and that number must be positive. Please try again. " << endl; cin.clear(); cin.ignore(numeric_limits::max(), ‘\n’);
cin >> num;
}

The reason that this code works is due to the behavior of “cin.” Whenever cin tries to read a letter (or any non-numeric character) into a variable that is designed to hold a number, cin enters a fail state, which can produce erroneous results and potentially even cause your program to crash. Therefore, you must trap for non-numeric data whenever the program is expecting a number. One way to trap for this error is to check if “cin” has entered the fail state using the “cin.fail()” function. If it has, then you first have to clear cin out of its fail state using the cin.clear() function. Afterwards, you can issue the “ignore” command which flushes any remaining garbage out of the input stream (aka “cin buffer”). Finally, you can prompt the user to re-enter a correct value. Note that the code above is not especially informative. That is, it produces a single generic error message to the user such that the user may not realize what he did wrong to cause the problem. Therefore, feel free to tweak it as necessary in order to customize your error messages. One other point of interest is that the code above doesn’t allow the user an opportunity to end the program if he can’t provide an acceptable value. It just continues to loop until an acceptable value is entered. In real life, you would definitely want to allow your user a means of escape. In this assignment, however, continuing to prompt the user for a valid value until one is entered is fine. Later in the course, we’ll learn more sophisticated ways of error checking when we discuss exception handling.

Good luck on this assignment! Have fun with it, and as always, let me know if you have any questions.

To give you an idea of the general criteria that will be used for grading, here is a checklist that you might find helpful:

Compiles and Executes without crashing
Style:
No global variables
Code is modular
Use of enum for credit rating
Use of switch statement for assigning interest rates
Requirements:
Prompts user for principle, credit score, and term with appropriate error checking
Generates a random interest rate within the specified ranges
Correctly calculates payment in MonthlyPayment function
Output is formatted appropriately

×
New Download