C++ Adding sum of multiple function calls within a loop - c++

I've been working on an assignment that calculates the total profit/loss for multiple stock sales via a looping function with information inputted by user for each stock sale. I did a thorough amount of googling to no avail. I was able to get the function working within the loop but I have not been able to figure out how to add the profit/loss from multiple sales - instead only displaying profit/loss for each individual function call. My function's algorithm checks out if you manually add the totals for each sale, just unclear on how to find the sum of multiple function calls.
Here is the sample data I'm suppose to enter that should display the total profit of $3324.00:
sale numberOfShares salePrice salesCommission purchasePrice purchaseCommission
1 25 15 7.50 5 2.50
2 100 2.50 12 1.75 8
3 1000 5.10 51 2 20
And my code thus far:
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double stockProfitFunction(double NS, double PP, double PC, double SP, double SC);
// Main Function
int main()
{
// Format output
cout << fixed << setprecision(2);
// Initialize variables
double profit,
numberOfShares,
salePrice,
saleCommission,
purchasePrice,
purchaseComission;
int numberOfSales;
// Get # of sales from user
cout << "Multiple Stock Profit Calculator\n--------------------------------\n\n";
cout << "How many sales do you wish to enter?: ";
cin >> numberOfSales;
cout << endl;
// Perform function in loop for number of sales
for (int i = 1; i <= numberOfSales; i++)
{
system("cls"); // Clears screen
cout << "Multiple Stock Profit Calculator\n";
cout << "(Currently entering stock sale #" << i << ")\n----------------------------------\n";
// Get information from user
cout << "Enter number of shares: ";
cin >> numberOfShares;
cout << "Enter sale price: ";
cin >> salePrice;
cout << "Enter sales commission: ";
cin >> saleCommission;
cout << "Enter purchase price: ";
cin >> purchasePrice;
cout << "Enter purchase commission: ";
cin >> purchaseComission;
//Calcualtes profit with function
profit = stockProfitFunction(numberOfShares, purchasePrice, purchaseComission, salePrice, saleCommission);
// Display "profit" or "loss" depending on positive or negative value returned by function
if (profit >= 0)
{
cout << "\n-----------------------\n";
cout << "You earned a profit of: $" << profit << endl;
cout << "(Press enter to input next sale)";
cin.get();
cin.get();
}
else
{
cout << "\n-----------------------\n";
cout << "You had a loss of: $" << profit << endl;
cout << "(Press enter to input next sale)";
cin.get();
cin.get();
}
}
return 0;
}
// Stock Profit Function, returns profit
double stockProfitFunction(double NS, double PP, double PC, double SP, double SC)
{
return ((NS * SP) - SC) - ((NS * PP) + PC);
}
Thanks for taking a look!

Initialize a variable to zero.
Each time you calculate a profit, add it to that variable.
Where desired, output the value of that variable.
By the way:
system("cls"); // Clears screen
That's a very bad habit to get into. Maybe on your machine, cls clears the screen, but you have no way to know what the cls command might do on someone else's machine. (On mine, there is no command called cls, the clear screen command is clear.) Unless you absolutely have no choice, you should strongly avoid using system in your C++ code.

Related

Stumped with a transient population program in c++

this is my first time posting on here. Whenever I've gotten stuck on a programming problem, I've typically been able to find enough information to get me unstuck. I'm afraid that the issue I'm having though, I can't quite find an answer to. It's something I'd need someone to look at to tell me what I may be doing wrong in my code.
I have the program running successfully, and it DOES work. The issue however, is that my produced output is off by a few numbers when compared to the expected output on My Programming Lab. I'm really not sure of what to do to produce the correct output. Allow me to post both my source code, and a screenshot MPL's results screen.
SOURCE CODE:
#include <iostream>
using namespace std;
int populationCalculator(int, double, double, int, int);
int main()
{
int startingPopulation, newArrivals, peopleWhoLeft, years,
newPopulation, finalPopulation;
double deathRate, birthRate;
cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startingPopulation;
while (startingPopulation < 2)
{
cout << "\nThe starting population may not be less than two. Please
re - enter: ";
cin >> startingPopulation;
}
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "\nBirth rate percent cannot be negative. Please re -
enter:";
cin >> birthRate;
}
birthRate = birthRate / 100;
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "\nDeath rate percent cannot be negative. Please re -
enter: ";
cin >> deathRate;
}
deathRate = deathRate / 100;
cout << "How many individuals move into the area each year? ";
cin >> newArrivals;
while (newArrivals < 0)
{
cout << "\nArrivals cannot be negative. Please re - enter: ";
cin >> newArrivals;
}
cout << "How many individuals leave the area each year? ";
cin >> peopleWhoLeft;
while (peopleWhoLeft < 0)
{
cout << "\nDepartures cannot be negative. Please re - enter: ";
cin >> peopleWhoLeft;
}
cout << "For how many years do you wish to view population changes? ";
cin >> years;
while (years < 1)
{
cout << "\nYears must be one or more. Please re - enter: ";
cin >> years;
}
newPopulation = populationCalculator(startingPopulation, deathRate,
birthRate, newArrivals, peopleWhoLeft);
cout << "\nStarting population: " << startingPopulation << endl;
for (int loopCount = 1; loopCount <= years; loopCount++)
{
newPopulation = populationCalculator(newPopulation, deathRate,
birthRate, newArrivals, peopleWhoLeft);
cout << "Population at the end of year " << loopCount << " is: " <<
newPopulation << endl;
}
system("pause");
return 0;
}
int populationCalculator(int Population, double deathRate, double birthRate,
int newArrivals, int peopleWhoLeft)
{
int newPopulationCount;
newPopulationCount = Population + (Population * birthRate) - (Population
* deathRate) + newArrivals - peopleWhoLeft;
return newPopulationCount;
}
MPL RESULTS:
http://imgur.com/a/mRmpc
I really will appreciate if anyone can help me figure out why my produced output is off by a few numbers.
Step through your code. You're returning an int where you have double and int multiplication. Make sure that you aren't truncating values that might need to be rounded up or down.
Does birth happen before or after death? Should it occur in steps, or all at once like you have shown?
try casting to a double in your operations that involve doubles and integers. For example
birthRate = (double)(birthRate / 100);
deathRate = (double)(deathRate / 100);
Sometimes programming languages will cast a double to an integer, which causes your numbers to be off.
Perhaps the number were rounded off?
I assume this is the case because I noticed you declared some variables in double but the end result is integer. Try to change the data type of the method
populationCalculator(), newPopulation and of course the returning values of the method populationCalculator() from int to double.

Looping functions for a number of times

So I am trying to get this program to allow the user to input their data 4 times. I want the program to call each function until the user enters them for the 4th time, but it is not letting me and just stops at the 2nd attempt. Can someone help me with this problem? Thanks!
Output:
Enter the crop name:
Corn
Enter cost, yield, price per bushel, and increase data:
5
5
5
5
Minimum Profit Maximum Profit Average Profit
Corn $20000.00 $145000.00 $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
Peas
Minimum Profit Maximum Profit Average Profit
$20000.00 $145000.00 $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
Minimum Profit Maximum Profit Average Profit
$20000.00 $145000.00 $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
Minimum Profit Maximum Profit Average Profit
$20000.00 $145000.00 $82500.00
Enter the crop name:
Enter cost, yield, price per bushel, and increase data:
Minimum Profit Maximum Profit Average Profit
$20000.00 $145000.00 $82500.00
Press any key to continue . . .
enter code here
Program:
#include <iostream>
#include<iomanip>
#include<string>
#define ACRES 1000;
using namespace std;
//Function prototypes.
void getData(string*, float*, int*, float*, float*);
void calculate(float, int, float, float, float*, float*, float*);
void printResults(string, float, float, float);
int main()
{
string name;
float CostPerAcre, increase, bushel, MinNP, MaxNP, AvgProfit2, bestcrop;
int yield;
for(int i = 0; i <= 4; ++i)
{
getData(&name, &CostPerAcre, &yield, &bushel, &increase);
calculate(CostPerAcre, yield, bushel, increase, &MinNP, &MaxNP, &AvgProfit2);
printResults(name, MinNP, MaxNP, AvgProfit2);
}
//cout << endl << "Old MacDonald, you should plant " << bestCrop << endl << endl;
return 0;
}
// This function allows the user to input their data.
void getData(string *cropname, float *costpa, int *bpa, float *dpb, float *dpbincrease)
{
cout << "Enter the crop name: " << endl;
getline(cin, *cropname);
cout << "Enter cost, yield, price per bushel, and increase data: " << endl;
cin >> *costpa >> *bpa >> *dpb >> *dpbincrease;
}
// This function uses the data to calculate the projected range of net profit and the average net profit for each crop.
void calculate(float costpa, int bpa, float dpb, float dpbincrease, float *MnNP, float *MxNP, float *AvgProfit)
{
float MnGP, MxGP;
float costofCrop = costpa * ACRES;
MnGP = (bpa * dpb ) * ACRES;
MxGP = MnGP + (MnGP * dpbincrease);
*MnNP = (MnGP)-costofCrop;
*MxNP = (MxGP)-costofCrop;
*AvgProfit = (*MxNP + *MnNP) / 2;
}
// This function displays the minimum profit, maximum profit, and average profit.
void printResults(string cropname, float MnNP, float MxNP, float AvgProfit)
{
cout << setw(25) << right << "Minimum Profit" << setw(20) << right << "Maximum Profit" << setw(20) << right << "Average Profit" << endl;
cout << cropname << setw(5) << right << setprecision(2) << fixed << '$' << MnNP << setw(10) << right << setprecision(2) << fixed << '$' << MxNP << setw(10) << right << setprecision(2) << fixed << '$' << AvgProfit << endl;
}
In the getData function, instead of using getline, do:
cin >> *cropname;
Why? Like they said in a similar question, "If you're using getline after cin >> something, you need to flush the newline out of the buffer in between".
insert that after reading from console.
cin.clear();
fflush(stdin);
or
cin.flush();
or
cin.ignore(INT_MAX);
depends on the system you are running it
Check This for more Info

How can i make my functions stops executing at a certain line?

I wrote this this for a simple vendding machine, and this code is to calculate the back change under 1 dollar.
The only problem I am facing is: I want my function to stop excuting the rest of the function if the second if statement in the void function is true.
So, How can I do it?
#include <iostream>
#include <string>
using namespace std;
void changecalculator (int purchaseAmount, int& Qav, int& Dav, int& Nav,int& totalPurchases, int& purchases)
{
int changeAvailable;
//set the variables for the change back function.
int QBack ,DBack ,NBack ;
//calculating the change that the machine should give back.
int chaneBack = 100 - purchaseAmount ;
//test if the purchase amount is a multiple of 5.
if (purchaseAmount %5 == 0)
{
//printing the purchase amount to the screen for the customer.
cout << "You entered a purchase amount of " << purchaseAmount << " cents." <<endl;
cout <<endl;
//calculating the denominations of the change.
QBack = std::min(chaneBack / 25, Qav) ; //Calculating quarters back.
chaneBack -= QBack * 25 ; // subtract the back quarters from the back change.
DBack = std::min(chaneBack/10, Dav); //Caculating the back dimes.
chaneBack -= DBack* 10; //Subtracting the back dimes from the back change.
NBack = std::min(chaneBack/ 5, Nav); //Calculating the back nickels.
chaneBack = QBack*25 + DBack*10 + NBack*5 + chaneBack; //Caculating the change back by adding the denominations.
changeAvailable = Qav * 25 + Dav * 10 + Nav * 5 ;
if (changeAvailable < chaneBack )
{
cout << "No Enough change for the purchase." <<endl;
cout << "Thanks for using my program" <<endl;
}
else
{
}
//Caculating the number of the coins that should be given to the customer as a change.
int coinsNum = QBack + DBack + NBack;
//printing information amount the back change given to the customer.
cout <<"Your change of " <<chaneBack <<" cents is given as " <<QBack <<" Q, " <<DBack <<" D,and " <<NBack <<" N." <<endl;
cout << "The value of your " <<coinsNum <<" coins adds up to " <<chaneBack <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
//Subtract the given denominations from the available denominations.
Qav -= QBack;
Dav -= DBack;
Nav -= NBack;
//Print visual information for the number of the remaining denominations.
//This part is only to show that the program can keep track of coins.
cout << "Quarters available: " <<Qav <<endl;
cout << "Dimes available: " <<Dav <<endl;
cout << "Nickels available: " <<Nav <<endl;
cout << "total purchases: " <<totalPurchases <<endl;
cout << "purchases: " <<purchases <<endl ;
/* set back the variable the original value so we can keep going
with function that would run after this step if the customer annswered yes. */
chaneBack -= chaneBack;
}
else
{
//print out an error if the purchase amount is not a multiple of 5.
cout << "Unable to process an invalid purchase amout of " <<purchaseAmount <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
}
}
int main()
{
//set the variables
int Qav=0 ; //Quarters available
int Dav=0 ; //Dimes available
int Nav=0 ; //Nickels available
int purchaseAmount ; //The purchase amount
int totalPurchases = 0; //The total of puchases the cusomer did.
int purchases = 0; //The number of purchases the cutomer did.
string answer; //The answer of the customer (y/n)
//The header of the program
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
//Get the puchase amount from the customer.
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
//Calculating the total of the purchases by adding them together.
totalPurchases += purchaseAmount;
//Calculating the number of purchases.
purchases += 1;
changecalculator (purchaseAmount, Qav, Dav, Nav, totalPurchases, purchases);
//asking the customer if they want to do another ourchase.
cout << "Process again (y/n)?";
cin >> answer;
//this loop helps rerun the function everytime the customer wants to.
while(answer == "y")
{
//Enter the new purchase amount
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
//adding the second purchase amount to the last amount.
totalPurchases += purchaseAmount;
//adding this purchase to last number of purchases.
purchases += 1 ;
//run the function to caculate the change for the new purchase amount
changecalculator (purchaseAmount, Qav, Dav, Nav, totalPurchases, purchases);
//asks if the customer wants to do another ourchase again.
cout << "Process again (y/n)?";
cin >> answer;
}
}
You will want to call return.
if (changeAvailable < chaneBack )
{
cout << "No Enough change for the purchase." <<endl;
cout << "Thanks for using my program" <<endl;
return;
}
Add a return statement to your code. return:
Terminates the execution of a function and returns control to the calling function (or to the operating system if you transfer control from the main function). Execution resumes in the calling function at the point immediately following the call.
Source: MSDN
Applied to your code:
...
if (changeAvailable < chaneBack )
{
cout << "No Enough change for the purchase." <<endl;
cout << "Thanks for using my program" <<endl;
return;
}
...
You can do so in one of two ways:
Instead of having nothing in your else block, put everything else (that you don't want to run in the event that your if block is executed) in this block.
Put a return; at the end of your if block. This will cause your function to quit instead of executing any further code.

C++ basic menu driven program (calling functions)

I'm new to programming and am trying to figure out why this is happening.
Basically I'm asked to design a menu driven program that does some basic deposit/withdrawal calculations. I'm supposed to write different functions for each process and call on them when necessary.
I'm having 2 problems:
1) My functions aren't updating my variables within the program. For example it will run, I can enter my starting balance, I can enter my transaction type and amount but every time I switch from deposit to writing a check, it resets the balance to the original user input.
2) When I want the program to exit, it is still asking me for the double input. Not sure how to make it accept just an "E" instead of "E number"
Thanks in advance.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes for my functions that will be called in
void displayMenu();
double checkProcess(double, double);
double depositProcess(double, double);
double totalServCharge(double);
int main()
{
//Variables needed for the problem
char choice; //menu choice
double transAmt, balance, numbServCharge; //transaction amount, current balance, total service charges, number of service charges
numbServCharge = 0; //Start the counter for number of service charges at zero
cout << "Checkbook Program\n\n";
cout << "Enter the beginning balance: ";
cin >> balance; //get the initial balance
cout << endl;
do
{
//Call the display menu function
displayMenu();
//Get user's choice and transaction amount from menu
cout << "Enter a transaction: ";
cin >> choice >> transAmt;
choice = toupper(choice);
cout << endl;
//Create an error message for invalid choice and get a second choice
while((choice != 'C') && (choice != 'D') && (choice != 'E'))
{
cout << "Invalid selection. Please choose C, D or E: ";
cin >> choice;
}
//Set up for option #1 -- using a check
if(choice=='C')
{
//Call check function and service charges function
checkProcess(transAmt, balance);
totalServCharge(numbServCharge);
}
//Set up for option #2 -- deposits
if(choice=='D')
{
//Call deposit function and service charges function
depositProcess(transAmt, balance);
totalServCharge(numbServCharge);
}
}while(choice != 'E'); //Close program if option 3 is selected
//Display final balance
cout << "Processing end of the month";
cout << "\nFinal balance : $ " << fixed << setprecision(2) << balance << endl << endl;
system("pause"); //Pause screen so it doesn't close right away
return 0;
}
void displayMenu()
{
//Highlight menu options
cout << "\nCommands\n";
cout << "C amount - process a check in a specific amount\n";
cout << "D amount - process a deposit in a specific amount\n";
cout << "E - end the program\n\n";
}
double checkProcess(double transAmt, double balance)
{
cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
transAmt = transAmt + .25; //Add the service charge onto the transaction
balance = balance - transAmt; //Update account balence
cout << "\nBalance: $" << fixed << setprecision(2) << balance;
cout << "\nService charge: $0.25 for a check\n";
return balance;
}
double depositProcess(double transAmt, double balance)
{
cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
transAmt = transAmt - 0.25; //Add the service charge onto the deposit
balance = balance + transAmt; //Update account balence
cout << "Balance: $" << fixed << setprecision(2) << balance << endl;
return balance;
}
double totalServCharge(double numbServCharge)
{
double totalServCharge = 0;
numbServCharge++; //Add one to the number of service charges there have been
totalServCharge = .25 * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
return numbServCharge;
}
(1) Your variables aren't updating because you aren't changing them; you're changing the copies passed to your subroutines. You either need to pass the current balance by reference (so that when you change it in the subroutine scope, you're also changing the original) or assign the return value from your subroutines to your current balance.
When you pass a variable by value (as you are now), the program makes a copy of that variable for use in the function scope. When you leave that scope, the copy is deleted. Do this when you want to be sure a subroutine won't alter anything in the scope from which it is called. If you want the subroutine to alter the variables you pass to it, pass the variables by reference. In this function signature, "transAmt" is passed by value and "balance" is passed by reference:
double foo(double transAmt, double& balance);
Incidentally, if you pass the balance by reference there's no reason to return it. You aren't doing anything with the returned value right now anyway.
(2) If you want to not ask for a number when "E" is given, make separate "cin" statements for the number in the conditional blocks for "C" and "D". Right now all your input code is shared between all cases. It would be even better if you arrange things so that you exit the loop before a number is asked for. This way, you only have to write that "cin" statement once.
It's becouse in C++ when you call a function with parameter, you only operate on COPIES of those parameters. If you want to change the original value, pass variable as reference.
See those simple examples:
#include <iostream>
using namespace std;
void normal(int a)
{
a += 10;
}
void by_reference(int &a) //Notice ampersand
{
a += 10;
}
int by_return(int a)
{
return a + 10;
}
int main()
{
int a = 5;
cout << "Start: " << a <<endl;
normal(a);
cout << "Normal: " << a <<endl;
by_reference(a);
cout << "Reference: " << a <<endl;
a = by_return(a);
cout << "Return: " << a <<endl;
}
Now you can see that if you pass the variable as reference it gets changed.
Even if just putting the & before parameter names would fix your problem, I'd recommend you using a return, it generally is simplier and makes your code look better (it's much easier to understand).
Also, correct your indentation. It's very bery important when you work with bigger projects.
Hope this helps. :)
1 | The first problem is occurring because you are not actually modifying the user's input. You are passing the variable in but once the new value is returned you are not storing it, thus the value is forgotten in the main function. Not the best solution, but it might suit you to do this:
if (choice == 'D')
{
//Call deposit function and service charges function
balance = depositProcess(transAmt, balance); // Overwrite balance
numbServCharge = totalServCharge(numbServCharge); // Overwrite numbServCharge
}
Or in a more efficient way, you can pass in the variable by reference. All you would need to do is change the function prototype.
void totalServCharge(double&);
void depositProcess(double&, double&);
Now when the variable is passed in, it will be modified directly by the function. Also note the function data type changed to void because you no longer need to return a value from the function.
2 | Your second problem is the way you handle user input. If you want to ask for multiple input, it might sometimes be best to do it with separate cout and cin statements.
The way it is set in your program is for the user to enter two inputs at once, therefore if you only enter E, it waits patiently as it should for your next input (transAmt). It might be better to process the action desired and then have an amount entered afterwards.
Very general example:
do {
cout << "1 : Transaction or 2 : Exit" << endl;
cin >> choice;
if (choice == 2)
break;
cout << "1. Deposit\n" << "2. Check" << endl;
cout << "Enter transaction type followed by amount" << endl;
cin >> transType >> amount;
// Then do what was specified in choice
} while (true); // Or whatever condition pleases you

Basic problems with writing functions. Program randomly skips to the end

I'm very new to writing functions. This is my first attempt at using call by reference parameters specifically for a homework assignment. This program should calculate how long it takes to pay off a loan based on the users input of the amount they borrow, their monthly payment and the interest rate. The program should make sure the user doesnt input a payment number less than monthly interest amount and check for negative integers etc.
The area I commented out was my first go before i attempted to turn all of that into a function that uses call by reference parameters. At that point the program ran fine and i got output within a dollar of what I was looking for. After i rewrote the program like this and tried to make it a function, the program compiles but break to the bottom cout statements after asking for the interest rate. I assume my problem is somewhere within the interest1 function where i convert the percent to a decimal but i cant figure out why it would skip the rest of the functions afterwards.
Thanks for any advice you might have on this issue or anything else that looks wrong with the program itself.
#include <iostream>
#include <fstream>
using namespace std;
double amount();
double interest1();
double pay(double amt, double interest);
void payoff(double monthly, double Minpayment,double borrow,double interest, double&
totalinterest,int& month);
int main()
{
double monthly,month,totalinterest;
cout << fixed;
cout <<showpoint;
cout <<setprecision(2);
double borrow=amount();
double interest=interest1();
double Minpayment=pay(borrow, interest);
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month);
/*
// cout << "What is the monthly payment amount? \n";
// cin >> monthly;
// if(monthly<Minpayment)
// {
// cout << "You must make payments of at least: " << Minpayment++ << endl;
// cout << "Because your monthly payment is " << Minpayment << endl;
// cout << "Dont get overwhelmed with debt!!!!!! \n";
// }
// else
//
// {
// int month = 1;
// double totalinterest=0;
// do
// {
// double Minpayment=pay(borrow, interest);
// borrow=(borrow+Minpayment)-monthly;
// totalinterest=totalinterest+Minpayment;
// month++;
// }while(borrow>monthly);
*/
cout << "Your debt will be paid off in " << month << " months \n";
cout <<" with a final payment of " << borrow << endl;
cout << "You paid " << totalinterest << " in interest. \n";
return 0;
}
double amount()
{
double borrow,amt;
do
{
cout << "How much money do you want to borrow? \n";
cin >> amt;
if(amt<1)
cout << "You must enter a positive number!";
}while(amt<1);
borrow = amt;
return borrow;
}
double interest1()
{
double rate;
do
{
cout << "What is the annual interest rate expressed as a percent? \n";
cin >> rate;
if(rate<1)
cout << "You must enter a positive number!";
}while(rate<1);
double interest = (rate/12)*.01;
return interest;
}
double pay(double borrow,double interest)
{
double Minpayment=borrow*interest;
return Minpayment;
}
void payoff(double monthly,double Minpayment,double borrow,double interest, double&
totalinterest,int& month)
{
cout << "What is the monthly payment amount? \n";
cin >> monthly;
if(monthly<Minpayment)
{
cout << "You must make payments of at least: " << Minpayment++ << endl;
cout << "Because your monthly payment is " << Minpayment << endl;
cout << "Dont get overwhelmed with debt!!!!!! \n";
}
else
{
int month = 1;
do
{
double Minpayment=pay(borrow, interest);
borrow=(borrow+Minpayment)-monthly;
double totalinterest=totalinterest+Minpayment;
month++;
}while(borrow>monthly);
}
}
When you call a function inside main, you don't need to specify parameter types anymore:
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month); //wrong
should be
payoff(monthly, Minpayment,borrow, interest, totalinterest, month);
You should put double& andint& to the function's parameter list when define the function like you did.