Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************************
- * This program produces a personalized loan amortization table for the user.*
- *****************************************************************************/
- #include <iostream> // Needed for I/o
- #include <iomanip> // Need for formatting
- #include <cmath> // Needed for the pow function
- #include <string> // Needed to read strings
- #include <fstream> // Needed to manipulate files
- #include <windows.h> // Needed to change screen colors
- using namespace std; // namespace std
- /****************************************************************************
- * Display Message 1 - This void function displays a message! *
- *****************************************************************************/
- void displaymessage1()
- {
- cout << "\nHello and Welcome to MidSouthern Savings Bank's Loan Amortization Calculator!\n\n";
- }
- /*****************************************************************************
- * Display Message 2 - This void function displays another message! *
- ******************************************************************************/
- void displaymessage2()
- {
- cout << "\nWe Appreciate your business and hope you have\na wonderful experience working with us!\n\n"
- "If you have any concerns or complaints please call 1-800-EAT-SHIT\n\n";
- }
- /******************************************************************************
- * Function to return numerical value 1 *
- ******************************************************************************/
- int sum(int num1, int num2); // Function prototype
- /******************************************************************************
- * Function to return numerical value 2 *
- ******************************************************************************/
- int calc(int num3, int num4); // Function prototype
- /******************************************************************************
- * Function to return Boolean value *
- ******************************************************************************/
- bool isEven(int); // Function prototype
- /******************************************************************************
- * Functions for decision structure and loop *
- ******************************************************************************/
- void displayMenu(); // Function prototype
- int getChoice(); // Function prototype
- void makeSmile(); // Function prototype
- // Rectangle class declaration
- class Rectangle
- {
- public:
- double length;
- double width;
- double stories;
- void setLength(double);
- void setWidth(double);
- void setHeight(double);
- double getLength();
- double getWidth();
- double getHeight();
- double getArea();
- };
- /*************************************************************
- * main *
- *************************************************************/
- int main() // Start Main Program
- {
- Rectangle box; // Declare a Rectangle object
- double boxLength, boxWidth, boxHeight;
- //Get box length and width
- cout << "This will calculate the square footage area of your house.\n";
- cout << "What is the length? ";
- cin >> boxLength;
- cout << "What is the width? ";
- cin >> boxWidth;
- cout << "How many stories? ";
- cin >> boxHeight;
- // Call member functions to set box dimensions
- box.setLength(boxLength);
- box.setWidth(boxWidth);
- box.setHeight(boxHeight);
- // Call member functions to get box information to display
- cout << "\nHere is your house's data:\n";
- cout << "Square Footage Area of your home is : " << box.getArea() << endl;
- double loan, // Loan amount
- rate, // Annual interest rate
- moInterestRate, // Monthly interest rate
- years, // Years of loan
- balance, // Monthly balance
- term, // Used to calculate payment
- payment, // Monthly payment
- age; // User's age
- int totalage, // Payoff age
- numPayments, // Number of payments
- totalyear, // Payoff year
- colorchoice, // Color choice
- curryear; // current year
- char ans; // answer for run again
- const string bankname = "MidSouthern Savings Bank"; // Constant
- string username;
- ofstream outputFile; // Open file and write message to user
- outputFile.open ("C:\\Users\\joebrady\\Desktop\\names.txt");
- outputFile << "Thanks\n";
- outputFile << "For\n";
- outputFile << "Your\n";
- outputFile << "Business!";
- outputFile.close();
- displaymessage1(); // Void Functions Display Message 1
- do
- {
- // Get loan information
- cout << "Please type your name and hit Enter\n";
- cin >> username;
- cout << "Please enter your age and hit Enter\n";
- cin >> age;
- cout << "Please enter the current year and hit Enter\n";
- cin >> curryear;
- cout << "Enter your Loan amount in numbers only and hit Enter\n";
- cin >> loan;
- cout << "Enter your Annual interest rate (entered as a decimal, example: .05 for 5%) and hit Enter\n";
- cin >> rate;
- cout << "Enter the Total number of Years of your loan and hit Enter\n";
- cin >> years;
- cout << "\n\n\n";
- while (years <= 0) // Variable Defined Loop to correct invalid data
- {
- cout << "Number must be positive. Please try again dummy: " << flush;
- cin >> years;
- }
- // Calculate monthly payment
- // A zero interest loan must be handled as a special case to avoid
- // a divide by 0 in the payment formula used for loans interest.
- numPayments = static_cast<int>(12 * years);
- moInterestRate = rate / 12.0;
- if (rate == 0)
- payment = loan / numPayments;
- else
- { term = pow((1 + moInterestRate), numPayments);
- payment = (loan * moInterestRate * term) / (term - 1.0);
- }
- // Display monthly payment
- cout << fixed << showpoint << setprecision(2);
- cout << "Your Monthly payment is: $" << payment << endl;
- cout << username << " owes " << bankname << endl;
- // Display report header
- cout << endl;
- cout << setw(5) << "Month" << setw(10) << "Interest";
- cout << setw(10) << "Principal" << setw(9) << "Balance" << endl;
- cout << "----------------------------------\n";
- balance = loan; // Remaining balance starts out as full loan amount
- // Produce a listing for each month
- for (int month = 1; month <= numPayments; month++)
- {
- double moInterest, // Amount of pmt that pays interest
- principal; // Amount of pmt that lowers the balance
- // Calculate amount paid for this month's interest and principal
- moInterest = moInterestRate * balance; // Calculate interest first
- if (month != numPayments) // If not the final month
- principal = payment - moInterest; // rest of pmt goes to principal
- else // It's the last month so
- { principal = balance; // pay exact final balance
- payment = balance + moInterest;
- }
- // Calculate new loan balance // Only principal reduces the
- balance -= principal; // balance, not the whole pmt
- // Display this month's payment figures
- cout << setw(4) << month << setw(10) << moInterest;
- cout << setw(10) << principal << setw(10) << balance << endl;
- }
- if (loan == 100000) // If Else If Usage
- cout << "\nCongratulations! You bought something crappy.\n Dont forget to Pay Your Bill!\n";
- else if (loan == 120000)
- cout << "\nCongratulations! You bought something nice!\nDont forget to Pay Your Bill!\n";
- else if (loan == 200000)
- cout << "\nWhoa, you really want to be in this much debt?\nDon't forget to Pay Your Damn Bills!\n";
- else if (loan != 100000, loan != 120000, loan != 200000)
- cout << "\nPay up or Else\n";
- totalage = sum(years, age); // Function to return numerical value 1
- cout << "\nYou will be " << totalage << " years old before you pay off this loan!\n\n";
- totalyear = sum(years, curryear); // Function to return numerical value 2
- cout << "The year will be " << totalyear << ".\n\n";
- if (isEven(totalage)) // Function to return Boolean Value
- cout << "We don't think you will make it, say hello to our Repo-Man, Donny\n\n";
- else
- cout << "We think you will do just fine!\n\n";
- ifstream inFile; // Open file and read message to user
- string name;
- inFile.open ("C:\\Users\\joebrady\\Desktop\\names.txt");
- inFile >> name;
- cout << name << endl;
- inFile >> name;
- cout << name << endl;
- inFile >> name;
- cout << name << endl;
- inFile >> name;
- cout << name << endl;
- outputFile.close();
- displaymessage2();
- const int quit = 6;
- int colorChoice;
- // Get the handle to standard output device (the console)
- HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
- do
- { SetConsoleTextAttribute(screen, 7); // Set to white on black
- displayMenu(); // for menu display
- colorChoice = getChoice();
- if (colorChoice != quit)
- { SetConsoleTextAttribute(screen, colorChoice + 9);
- makeSmile();
- }
- } while (colorChoice != quit);
- cout << "\n\nDo you want to run this program again? y/n?"; // User Defined loop using Do-while
- cin >> ans;
- } while ((ans == 'y') || (ans == 'Y'));
- return 0;
- }
- /**************************************************************
- * Sum function to return numerical value 1 *
- **************************************************************/
- int sum(int num1, int num2)
- {
- return num1 + num2;
- }
- /**************************************************************
- * Sum function to return numerical value 2 *
- **************************************************************/
- int calc(int num3, int num4)
- {
- return num3 + num4;
- }
- /**************************************************************
- * Function to return Boolean Value True/False *
- **************************************************************/
- bool isEven(int number)
- {
- if (number % 2 == 0)
- return true;
- else
- return false;
- }
- /***************************************************************
- * This functions displays the menu of color choices *
- ***************************************************************/
- void displayMenu()
- {
- cout << "Just for fun, What is your favorite color? Pick one\n\n"
- << "Enter\n1 for Green\n2 for Blue\n3 for Red\n4 for Purple\n5 for Yellow\nHit 6 to continue with the program\n";
- }
- /**************************************************
- * getChoice *
- * This function inputs, validates, and returns *
- * the user's menu choice. *
- **************************************************/
- int getChoice()
- {
- int choice;
- cin >> choice;
- while (choice < 1 || choice > 6)
- { cout << "The only valid choices are 1-6. Please re-enter. ";
- cin >> choice;
- }
- return choice;
- }
- /**************************************************************************
- * This function draws a smiley face in the users selected color *
- **************************************************************************/
- void makeSmile()
- {
- cout << "\n\n";
- cout << " <> <> \n ";
- cout << " * \n ";
- cout << " \\_______/ \n\n\n ";
- cout << " Press Enter to Return to the menu";
- cin.get(); // clear the previous \n out of the input buffer
- cin.get(); // wait for the user to press enter
- }
- // Member function implementation section
- /********************************************************************
- * Rectangle::setLength *
- * This function sets the value of the member variable length. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into length. If it is negative, 1.0 is assigned to length.*
- ********************************************************************/
- void Rectangle::setLength(double len)
- {
- if (len >= 0)
- length = len;
- else
- { length = 1.0;
- cout << "Invalid length. Using a default value of 1.\n";
- }
- }
- /********************************************************************
- * Rectangle::setWidth *
- * This function sets the value of the member variable width. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into width. If it is negative, 1.0 is assigned to width. *
- ********************************************************************/
- void Rectangle::setWidth(double w)
- {
- if (w >= 0)
- width = w;
- else
- { width = 1.0;
- cout << "Invalid width. Using a default value of 1.\n";
- }
- }
- /********************************************************************
- * Rectangle::setHeight *
- * This function sets the value of the member variable height. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into width. If it is negative, 1.0 is assigned to width. *
- ********************************************************************/
- void Rectangle::setHeight(double h)
- {
- if (h >= 0)
- stories = h;
- else
- { stories < 1.0;
- cout << "Invalid height. Using a default value of 1.\n";
- }
- }
- /**************************************************************
- * Rectangle::getLength *
- * This function returns the value in member variable length. *
- **************************************************************/
- double Rectangle::getLength()
- {
- return length;
- }
- /**************************************************************
- * Rectangle::getWidth *
- * This function returns the value in member variable width. *
- **************************************************************/
- double Rectangle::getWidth()
- {
- return width;
- }
- /**************************************************************
- * Rectangle::getHeigth *
- * This function returns the value in member variable Height. *
- **************************************************************/
- double Rectangle::getHeight()
- {
- return stories;
- }
- /*******************************************************************
- * Rectangle::getArea *
- * This function calculates and returns the area of the rectangle. *
- *******************************************************************/
- double Rectangle::getArea()
- {
- return length * width * stories;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement