Why am I getting "Too few arguments to function"? - c++

The code is to calculate the monthly payment of a car. Although I know I am not finished, I wish to see why I am not passing functions correctly.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void instructions()
{
cout << "We will calculate the monthly payment for your particular car." << endl;
cout << "Please Follow the instructions." << endl;
}
string getCarType()
{
string carType;
cout << "Please enter the type of your car: " << endl;
cin >> carType;
return carType;
}
double getPurchasePrice()
{
double purchasePrice;
cout << "Please enter the price in which you purchased the vehicle: " << endl;
cin >> purchasePrice;
return purchasePrice;
}
double getDownPayment()
{
double downPayment;
cout << "Please enter the down payment made on the vehicle: " << endl;
cin >> downPayment;
return downPayment;
}
int getYears()
{
int years;
cout << "Please enter the number of years remaining to pay off the loan: " << endl;
cin >> years;
return years;
}
double getRate()
{
double rate;
double correctedRate;
cout << "Please enter the annual interest rate of the loan as a whole number: " << endl;
cin >> rate;
//calculations
correctedRate = (rate/100);
return correctedRate;
}
double findAmountFinanced(double &purchasePrice, double &downPayment)
{
double amountFinanced;
//calculations
amountFinanced = purchasePrice - downPayment;
return amountFinanced;
}
double findMonthlyPayment(double &amountFinanced, double &rate, int &years)
{
double monthlyPayment;
double sideOne;
double sideTwo;
//calculations
sideOne = amountFinanced * (rate/12);
sideTwo = pow(1 - (1 + (rate/12)) / (-12*years));
monthlyPayment = sideOne/sideTwo;
return monthlyPayment;
}
int findNumberOfPayments(int &years)
{
int payments;
payments = 12 * years;
return payments;
}
int main()
{
instructions();
string carType;
double purchasePrice;
double downPayment;
int years;
double rate;
double amountFinanced;
double monthlyPayment;
int payments;
carType = getCarType();
purchasePrice = getPurchasePrice();
downPayment = getDownPayment();
years = getYears();
rate = getRate();
monthlyPayment = findMonthlyPayment();
payments = findNumberOfPayments();
cout << "Make of car: " << carType << endl;
cout << "Price Purchased at: " << purchasePrice << endl;
cout << "Down payment made at purchase: " << downPayment << endl;
cout << "Years to pay off loan: " << years << endl;
cout << "Annual rate of interest: " << rate << endl;
cout << "Your monthly payment is: " << monthlyPayment << endl;
cout << "The total amount of payments is: " << payments << endl;
return 0;
}
Again, my error is that I have too few arguments to function.

In some of the functions like findMonthlyPayment you don't pass an argument from the main, whereas these functions expect arguments. You error is self-explanatory you should have debugged it yourself.

If you look at your method definitions for findAmountFinanced, findMonthlyPayment and findNumberOfPayments, they take arguments when they are called. In your main() function where you calling them, you are not passing any arguments. Hence, too few arguments :)
FYI, a trick to troubleshooting the issue is to look at the complete stack trace of the error message and work your way down through line numbers.

Yes, when you call a certain method and that method have X numbers of arguments or parameters, whenever you call that function in your program you need to call it with exactly the same number of arguments and type of arguments.

Related

"+1 Overload --- Function definition for '...' not found" Along With "Unable to start program. The system cannot find the file specified"

This code is written for homework where I am prompting user input to calculate hospital bills based on whether the patient was admitted as an inpatient or an outpatient, along with user-inputted rates and charges. The issue I am having is that when I try to run the code, the system first tells me that there are build errors and asks if I would like to continue. After continuing, I get the following error message:
"Unable to start program.
[File Path]
The system cannot find the specified file"
I was going through my code and the only thing I could see was that the function prototypes before the main function had a message stating that the function definition could not be found.
Are there issues with my code or is this purely an issue with Visual Studio 2017? My directory doesn't look out of order or anything like that.
// PGM6 - Overloaded Hospital - 11.15.2020
#include "pch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double inpatient(int days, double rateDaily, double chargesService, double chargesMeds, double chargesTotal);
double outpatient(double chargesService, double chargesMeds, double chargesTotal);
int main()
{
int patientType;
cout << "Please enter (1) for Inpatient or (2) for Outpatient" << endl;
cin >> patientType;
if (patientType == 1)
double inpatient();
else
if (patientType == 2)
double outpatient();
else
cout << "Please enter (1) or (2)." << endl;
}
double inpatient()
{
int days;
double rate;
double chargesService;
double chargesMeds;
double chargesTotal = (days * rate) + chargesService + chargesMeds;
cout << "Days: ";
cin >> days;
cout << endl;
cout << "Daily Rate: ";
cin >> rate;
cout << endl;
cout << "Service Charges: ";
cin >> chargesService;
cout << endl;
cout << "Medication Charges: ";
cin >> chargesMeds;
cout << endl << endl;
cout << "Total Charges: " << chargesTotal << endl;
return chargesTotal;
}
double outpatient()
{
double chargesService;
double chargesMeds;
double chargesTotal = chargesService + chargesMeds;
cout << "Service Charges: ";
cin >> chargesService;
cout << endl;
cout << "Medication Charges: ";
cin >> chargesMeds;
cout << endl << endl;
cout << "Total Charges: " << chargesTotal << endl;
return chargesTotal;
}
Here is what you did wrong
The prototypes of your functions didn't need parameters as the values are typed by the user within the function.
When you call outpatient and inpatient, just type the name of the function with the parenthesis (don't type double in front, that's a new declaration).
chargesTotal had to be initialized just before printing it (after asking the needed values to the user)
I think this working code do what you need :
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
double inpatient();
double outpatient();
int main()
{
int patientType;
cout << "Please enter (1) for Inpatient or (2) for Outpatient" << endl;
cin >> patientType;
if(patientType == 1)
inpatient();
else if(patientType == 2)
outpatient();
else
cout << "Please enter (1) or (2)." << endl;
}
double inpatient()
{
int days;
double rate;
double chargesService;
double chargesMeds;
cout << "Days: ";
cin >> days;
cout << endl;
cout << "Daily Rate: ";
cin >> rate;
cout << endl;
cout << "Service Charges: ";
cin >> chargesService;
cout << endl;
cout << "Medication Charges: ";
cin >> chargesMeds;
cout << endl << endl;
double chargesTotal = (days * rate) + chargesService + chargesMeds;
cout << "Total Charges: " << chargesTotal << endl;
return chargesTotal;
}
double outpatient()
{
double chargesService;
double chargesMeds;
cout << "Service Charges: ";
cin >> chargesService;
cout << endl;
cout << "Medication Charges: ";
cin >> chargesMeds;
cout << endl << endl;
double chargesTotal = chargesService + chargesMeds;
cout << "Total Charges: " << chargesTotal << endl;
return chargesTotal;
}

Attempting to loop back to the beginning to run my program again

I am currently writing a code where in my main function I am just calling other functions. I am attempting to reloop back to the begining so the user can run the program again. The main function is just to call functions so my question is I know it is not possible to go back to the main function, but is it possible to create a function that will loop all other functions again? I feel as though I tried everything and continue to get infinite loops. I attached my code.
To condense the code please understand that all variables/classes are declared
void instructions();
void full_outputs(string, double, double, double);
int main()
{
instructions();
employee_num = employee_ID();
//cout << employee_num << " This is the employee ID."<<endl;
base_salary = baseSalary();
//cout << base_salary << " This is the employee's base salary." <<endl;
per_commission = percentage_commission();
//cout << per_commission << " This is the employee's percentage commission." << endl;
base_commission = base_and_com(base_salary, per_commission);
cout<< base_commission << "This is the total base salary with comission" << endl;
gross_pay = grossPay(base_commission);
//cout << gross_pay << "This is the gross pay"<<endl;
state_tax_hold = stateTax_hold(gross_pay);
//cout<< state_tax_hold << "This is the state tax hold on the amount" <<endl;
fica_total = ficaTotal(gross_pay);
//cout << fica_total << " This is the fica hold on the amount" <<endl;
fed_tax = fedTax(gross_pay);
//cout << fed_tax << " THis is the federal tax hold on the amount" << endl;
total_tax_hold = withholding_total(state_tax_hold, fica_total, fed_tax);
//cout << total_tax_hold << " This is the total tax withholding" << endl;
net_pay = netPay(total_tax_hold, gross_pay);
//cout << net_pay << " This is the total net pay" << endl;
full_outputs(employee_num, gross_pay, total_tax_hold, net_pay);
return 0;
}
void instructions()
{
cout << " This program will process sales employee's base salary \n";
cout << " and their percentage commission. \n";
cout << " You will be prompted to enter the employee's ID, base salary \n";
cout << " and percentage commission. \n";
cout << " \n";
cout << " The program will terminate if unspecified characters are used. \n";
}
string employee_ID()
{
string employee_num;
cout << " Please enter the employees eight digit ID number" << endl;
cin >> employee_num;
return employee_num;
}
double baseSalary()
{
double base_salary;
cout << " Please enter the employees base salary " << endl;
cin >> base_salary;
return base_salary;
}
float percentage_commission()
{
float per_commission;
cout << " Please enter the employees percentage commission."<< endl;
cout << " Please do not enter the percent symbol." << endl;
cout << " Percentage commission is between 0.05% - 10%" << endl;
cin >> per_commission;
while ((per_commission < 0.05)||(per_commission > 10))
{
cout << "The commission rate is not between 0.05% and 10%" << endl;
cout << "Please try again " << endl;
cin >> per_commission;
}
per_commission = per_commission / PERCENT_TO_DECIMAL;
return per_commission;
}
double base_and_com(double base_salary, float per_commission)
{
double base_commission;
double total;
total = base_salary*per_commission;
base_commission = total + base_salary;
return base_commission;
}
double grossPay(double base_commission)
{
double gross_pay;
gross_pay = base_commission;
cout << fixed << showpoint << setprecision(2);
return gross_pay;
}
double stateTax_hold(double gross_pay)
{
double state_tax_hold;
state_tax_hold= gross_pay*STATE_TAX;
return state_tax_hold;
}
double ficaTotal (double gross_pay)
{
double fica_total;
fica_total = gross_pay* FICA;
return fica_total;
}
double fedTax (double gross_pay)
{
double fed_tax;
if (gross_pay <= 500)
{
fed_tax = gross_pay * FEDERAL_TAX_UNDER;
}
else
{
fed_tax = gross_pay * FEDERAL_TAX_OVER;
}
return fed_tax;
}
double withholding_total(double fed_tax, double fica_total, double state_tax_hold )
{
double tax_withholding_total;
tax_withholding_total = fed_tax + fica_total + state_tax_hold;
cout << fixed << showpoint << setprecision(2);
return tax_withholding_total;
}
double netPay(double total_tax_hold, double gross_pay)
{
double net_pay;
net_pay = (gross_pay - total_tax_hold);
cout << fixed << showpoint << setprecision(2);
return net_pay;
}
void full_outputs(string employee_num, double gross_pay, double total_tax_hold, double net_pay)
{
cout << " The employee ID : " << right << employee_num << endl;
cout << " The gross pay is: " << right << gross_pay << endl;
cout << " The total tax withholding amount is : " << right << total_tax_hold << endl;
cout << " The net pay is: " << right << net_pay << endl;
}
As you know, in main you can just have a while loop with the code you want to repeat inside it:
int main()
{
// this will loop forever, until you explicitly break or return
while (true) {
// your code here
}
}
... but since you are constrained by the artificial limitation of only having function calls in main...
void run()
{
// this will loop forever, until you explicitly break or return
while (true) {
// your code here
}
}
int main()
{
run();
}

Math results in zero. New to coding

I'm trying to complete an assignment but I'm having difficulty with the math expressions and variables in general. I'm trying to make a program that takes user info on groceries and then outputs a receipt. Here is my code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//user input
string firstItem, secondItem;
float firstPrice, secondPrice;
int firstCount, secondCount;
double salesTax = 0.08675;
double firstExt = firstPrice * firstCount;
double secondExt = secondPrice * secondCount;
double subTotal = firstExt + secondExt;
double tax = subTotal * salesTax;
double total = tax + subTotal;
//user input
cout << "What is the first item you are buying?" << endl;
getline(cin, firstItem);
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
cin.ignore();
cout << "What is the second item you are buying?" << endl;
getline(cin, secondItem);
cout << "what is the price of the " << secondItem << "?" << endl;
cin >> secondPrice;
cout << "How many " << secondItem << "s?" << endl;
cin >> secondCount;
// receipt output
cout << "1st extended price: " << firstExt << endl;
cout << "2nd extended price: " << secondExt << endl;
cout << "subtotal: " << subTotal << endl;
cout << "tax: " << tax << endl;
cout << "total: " << total << endl;
return 0;
}
The program output either 0 for all or negatives.
Your calculations must go after you read in the values, not before. You're making your calculations based on uninitialized variables.
A declaration and initialisation like
double firstExt = firstPrice * firstCount;
initialises firstExt to be the product of the current values AT THAT POINT of firstPrice and firstCount.
It doesn't set up some magic so that the value of firstExt is recalculated whenever the values of firstPrice or firstCount are changed.
In your case, firstPrice and firstCount are uninitialised variables when you do this. Accessing values of uninitialised variables of type int gives undefined behaviour.
What you need to do is something like
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
firstExt = firstPrice*firstCount; // do the calculation here
If the value of firstExt is not needed until this point, you can declare it here instead;
double firstExt = firstPrice*firstCount; // do the calculation here
which means any earlier use of firstExt will give a compiler diagnostic.

Mortgage Calc outputs -e value

I have compiled and run the my equation and it seems to work completely fine from a an user interface and from the compile side, but the equation is getting lost in the inheritance somewhere. Any thoughts?
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
}
float MortgageCalc::setLoan(void)
{
return loan;
}
float MortgageCalc::setIntrest(void)
{
return interest;
}
float MortgageCalc::setYears(void)
{
return years;
}
float MortgageCalc::setTerm()
{
term = pow((1 + ((interest/100) / 12)), (12 * years)); //simple interest calculation with power calc to establish whole number translation
return term;
}
float MortgageCalc::getMonthlyDue()
{
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}
Here is the text output:
Enter the total Loan Amount: $100,000
Enter the interest Rate: 5
Enter the length of the loan: 10
Program to calculate mort payments
1. Monthly Payments
2. Total Payments
3. Exit Enter and Option Above: 1
Monthly Payment Due is: -1.56868e-036
Thanks for the support on this one, I found out that the term call was being placed outside of the child reference and therefore was able to update the field and return the correct input.
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{ //copies arguments from input to private members
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
float MortgageCalc::setLoan(void)
{ //returns loan amt to private member
return loan;
}
float MortgageCalc::setIntrest(void)
{ //returns interest amt to private member
return interest;
}
float MortgageCalc::setYears(void)
{ //returns years to private member
return years;
}
float MortgageCalc::getMonthlyDue()
{ //simple cal to output monthly
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}

C++: Calculations are incorrect when entering a certain input

I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.