In: Computer Science
PART A
A student wants to purchase an investment property that can
generate sufficient cash flow (net rental income) to pay her
monthly living expenses. The cash flow is calculated by taking the
rent received in a month and subtracting all of the expenses
associated with the property for that month.
The expenses are:
landlord insurance
property taxes
repair cost
management fees
Write a program to calculate the net income and determine if it’s sufficient to pay the living expenses.
In addition to main(), also use the following five functions in your program:
getIncomeNeeded() : a value-returning function that asks for the income needed to pay the monthly living expenses. Use a return statement to send it to main().
getRentalIncome() : a value-returning function that asks for the rental income received monthly. Use a return statement to send it to main().
getRentalExpenses() : a value-returning function that asks for all the expense amounts and returns the total expense. Use a return statement to send it to main().
calculateCashFlow() : a value-returning function that calculates the cash flow (net income) tomain().
canPayLivingExpenses(…): a value-returning function that returns true or false (true: can pay living expenses and false: cannot pay living expenses) – Display message in main() based on this return value.
(Part B)
Write a C++ program which prompts user to choose from the two alternative funtions specified below, each of which passes a random number between 1 and 4 and returns a random message.
The two functions are:
Function messageByValue that passes a random value between 1 and 4
and returns the message by value and
Function messageByReference that passes a random value between 1
and 4 and returns the message by reference.
Each function returns one of four random messages:
- “Sorry. You’re busted!”
- “Oh, you’re going for broke, huh?”
- “Aw cmon, take a chance!”
- You’re up big. Now’ the time to cash in your chips”
NOTE:
Use a while loop to continually run program until a -1 is entered
to Terminate program.
// PART A
#include <iostream>
using namespace std;
int getIncomeNeeded() {
int livingExpenses;
cout << "Enter income needed to pay living expenses: ";
cin >> livingExpenses;
return livingExpenses;
}
int getRentalIncome() {
int rentalIncome;
cout << "Enter rental income: ";
cin >> rentalIncome;
return rentalIncome;
}
int getRentalExpenses() {
int rentalExpenses = 0, expense;
cout << "Enter landlord insurance: ";
cin >> expense;
rentalExpenses += expense;
cout << "Enter property taxes: ";
cin >> expense;
rentalExpenses += expense;
cout << "Enter repair cost: ";
cin >> expense;
rentalExpenses += expense;
cout << "Enter management fees: ";
cin >> expense;
rentalExpenses += expense;
return rentalExpenses;
}
int calculateCashFlow(int rentalIncome, int rentalExpenses) {
return rentalIncome - rentalExpenses;
}
bool canPayLivingExpenses(int livingExpenses, int cashFlow) {
return livingExpenses < cashFlow;
}
int main() {
int livingExpenses = getIncomeNeeded();
int rentalIncome = getRentalIncome();
int rentalExpenses = getRentalExpenses();
int cashFlow = calculateCashFlow(rentalIncome, rentalExpenses);
if (canPayLivingExpenses(livingExpenses, cashFlow))
cout << "Can pay living expenses." << endl;
else
cout << "Cannot pay living expenses." << endl;
return 0;
}
// . PART B
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
string messageByValue(int value) {
switch(value) {
case 1: return "Sorry. You’re busted!";
case 2: return "Oh, you’re going for broke, huh?";
case 3: return "Aw cmon, take a chance!";
case 4: return "You’re up big. Now’ the time to cash in your chips";
}
return NULL;
}
void messageByReference(int value, string &message) {
switch(value) {
case 1: message = "Sorry. You’re busted!"; break;
case 2: message = "Oh, you’re going for broke, huh?"; break;
case 3: message = "Aw cmon, take a chance!"; break;
case 4: message = "You’re up big. Now’ the time to cash in your chips";
}
}
int main() {
while(true) {
cout << "Enter your choice of function (1 for messageByValue, 2 for messageByReference) or -1 to exit: ";
int choice;
cin >> choice;
if (choice == -1)
break;
else if (choice == 1) {
string message;
message = messageByValue(rand() % 4 + 1);
cout << message << endl;
}
else if (choice == 2) {
string message;
messageByReference(rand() % 4 + 1, message);
cout << message << endl;
}
}
return 0;
}