no candidates for error compile error - c++

I am trying to compile my application and it keeps running into "no match for 'operator<<'... it is not clear what the exact error of the program seems to be because he object is configured correctly as far as I can see.
#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdexcept>
using namespace std;
class MortgageCalc
{
protected:
float term;
public:
void setData(float, float, float);
float setTerm ();
float monthly;
float total;
float interest;
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
int years;
float loan;
};
void MortgageCalc::setData(float l, float i, float y)
{
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;
}
class mPayment : public MortgageCalc
{
public:
int monthly()
{
return ((loan * ((interest/100) / 12) * term ) / (term - 1));
}
};
class tPayment : public mPayment
{
public:
int total()
{
return (monthly() * (years * 12));
}
};
class iPayment : public tPayment
{
public:
int plusInterest()
{
return (total() - loan);
}
};
int main()
{
double loan(0), interest(0);
int years = 0;
MortgageCalc mort1;
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);
mPayment m;
cout << "Monthly payment due is " << m.monthly() << "." << endl;
tPayment t;
cout << "Total payment will be " << t.total() << "." << endl;
iPayment i;
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
return 0;
};

cout << "Total payment plus Interest will be " << i.plusInterest << "." << endl;
plusInterest is a class method pointer. Unsurprisingly, std::ostream has no clue what to do with a class method pointer, and is rightfully voicing its very strong objection, to such a preposterous proposition that it knows what to do with some strange class's method pointer.
You probably meant to write:
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
Now, that's a proper function call, that returns an int, and std::ostream is now delighted to take this int, and do its magic with it.

Related

My c++ code gives the following error (error: no matching function for call to ‘Bank::Bank()’) [duplicate]

This question already has answers here:
Default constructor error no matching function for call to
(2 answers)
C++ array of a self-defined class, no matching function call
(3 answers)
no matching function to call for "constructor"
(1 answer)
Closed last year.
I'm a beginner in c++. Though the code is still incomplete, I would like to know why I'm not able to create an array to store my objects from the class. I have to store 5 bank accounts in an array and I was trying to do so by soring the objects but it keeps showing error.
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class Bank
{
string depositor;
int accno;
char type;
float balance;
public:
Bank(string depositor, int accno, char type, float balance); //to assign initial values
void deposit(); //to deposit amount
float withdraw(); //to withdraw amount
void show(); //to show name and balance
Bank(string depositor, int accno); //constructor function for name and account no.
Bank(float balance, int accno); //constructor function for balance and account no.
Bank(char type, int accno); //constructor function for type and account no.
Bank(const Bank&); //copy constructor
//getter and setter functions for all data members
void setname(string depositor);
void setacc(int accno);
void settype(char type);
void setbal(float balance);
string getname();
int getacc();
char gettype();
float getbal();
};
Bank::Bank(string depos, int acno, char typ, float bal)
{
depositor=depos;
accno = acno;
type = typ;
balance = bal ? bal : 0;
}
void Bank::deposit()
{
float damt1;
cout << "Enter deposit amount: ";
cin >> damt1;
if (damt1 < 0.0) {
cout << "Can't deposit negative amount." << endl;
damt1 = 0.0;
}
balance += damt1;
}
float Bank::withdraw()
{
int amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount < 0.0) {
cout << "Negative amount can't be withdrawn" << endl;
amount = 0;
}
if (amount > balance - 1000.0) {
cout << "Not enough balance.";
}
balance -= amount;
return amount;
}
Bank::Bank(string name, int no)
{
depositor = name;
accno = no;
}
Bank::Bank(float bal, int no)
{
balance = bal;
accno = no;
}
Bank::Bank(char ty, int no)
{
type = ty;
accno = no;
}
Bank::Bank(const Bank& p)
{
balance = p.balance;
accno = p.accno;
}
void Bank::setname(string name)
{
depositor = name;
}
void Bank::setacc(int n)
{
accno = n;
}
void Bank::settype(char ty)
{
type = ty;
}
void Bank::setbal(float bal)
{
balance = bal?bal:0;
}
string Bank::getname()
{
return depositor;
}
int Bank::getacc()
{
return accno;
}
char Bank::gettype()
{
return type;
}
float Bank::getbal()
{
return balance;
}
void Bank::show()
{
cout << "Name: " << depositor<<endl;
cout << "Account number: " << accno<<endl;
cout << "Type: " << type<<endl;
cout << "Balance: " << balance<<endl;
}
int main()
{
Bank acct[5];//This is the line with error.I am unable to complete the code bcoz of this
int acno,i;
char ty;
string name;
float bal;
for (i=0;i<5;i++){
cout << "Enter details: \n";
cout << "name: ";
cin >> name;
cout << "\nEnter accno: ";
cin >> acno;
cout << "\nEnter type: ";
cin >> ty;
cout << "\nEnter balance: ";
cin >> bal;
Bank b1(name, acno, ty, bal);
}
return 0;
}
Can someone help me with what corrections I should make?

Calculations wont display in output

I'm a student in a basic programming class and I'm trying to complete this program for a class assignment. It's a simple program that calculates compounded interest by the inputs of the user. However, when writing the code, I noticed that the the result is 0 even though based on the input I would expect otherwise. Could anyone tell me why the program isn't showing results?
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}
Yes. You are not calling the futureValue function which would compute the value for you. Due to the value not being computed, it remains 0. Fix:
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
futureValue(); //Here we compute the value
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}

initialize class functions but output still 0

I am having problems with this program. When I compile it, I intialize all of the variables based upon the users input, but the cout still shows that the problem has '0' for most of the statements and for one of the statements its a '-negative' number. Any thoughts?
#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdexcept>
using namespace std;
class MortgageCalc
{
protected:
float term;
public:
void setData(float, float, float);
float setTerm ();
float monthly;
float total;
float interest;
int years;
float loan;
};
void MortgageCalc::setData(float l, float i, float y)
{
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;
}
class mPayment : public MortgageCalc
{
public:
int monthly()
{
return ((loan * ((interest/100) / 12) * term ) / (term - 1));
}
};
class tPayment : public mPayment
{
public:
int total()
{
return (monthly() * (years * 12));
}
};
class iPayment : public tPayment
{
public:
int plusInterest()
{
return (total() - loan);
}
};
int main()
{
double loan(0), interest(0);
int years = 0;
MortgageCalc mort1;
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);
mPayment m;
cout << "Monthly payment due is " << m.monthly() << "." << endl;
tPayment t;
cout << "Total payment will be " << t.total() << "." << endl;
iPayment i;
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
return 0;
};
You are taking all different objects like MortgageCalc mort1; mPayment m; tPayment t; iPayment i;.
These object do not have any relation.
Example:
mort1 = {term, monthly, total, interest, years, loan}
and suppose you have initialize with 1
mort1 = {term=1, monthly=1, total=1, interest=1, years=1, loan=1}
but it doesnot impact to the m because both are stored in memory on different location.
m = {term=0, monthly=0, total=0, interest=0, years=0, loan=0}
You can check both have different base address like cout<<&mort1<<endl<<&m; .
Data member you have set is part of MortgageCalc mort1 instead of mPayment m; tPayment t;.
You need to brush up your C++ basic's.
You use default constructors on those lines:
mPayment m;
tPayment t;
iPayment i;
They have no notion of previously input data held in mort1. You did not take care for any way to "share" or "communicate" this data.
m,t,i were all initialized with random data. There is no relation to mort1.
I won't go into details of what correct architecture here would be, but you should read about base class initialization. As a hint I'd say in your (a little weird) example you could try making this syntax work:
mPayment m(mort1);

[C++} Not sure if this is even possible

I'm trying to find the average net pay for a payroll program. What I'm wondering is it possible to create an array from the outputted tabular data and use the array to calculate the average? Or do I have to do this a different way? My code thus far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class employee {
ifstream fin;
char employeeid[12];
char employeename[20];
char martialstatus;
int hoursworked, overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay, average;
void calculateGrosspay();
void calculateTax();
void calculateNetpay();
void printHeadings();
void printData();
double findAverage();
void printAverage();
public: employee();
~employee();
void printReport(); };
employee::employee(){
fin.open(payrollData.txt);
}; //Constructor
employee::~employee() {
fin.close(); } //Destructor
void employee::calculateGrosspay() {
if (hoursworked > 40) {
overtime = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtime * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay;
}
else grosspay = hoursworked * hourlyrate;
} //Calculate gross pay function
void employee::calculateTax() {
taxrate = .30;
taxamount = grosspay*taxrate;
} // calculate tax function
void employee::calculateNetpay() {
netpay = grosspay - taxamount;
} //Calculate net pay
void employee::printHeadings() {
cout << setw(45) << "-Employee Payroll Report-" << endl;
cout << "____________________________________________________________" << endl;
cout << "NAME ID HW OT RT-PAY OT-PAY Gross Tax NetPay" << endl;
cout << "____________________________________________________________" << endl;
} // print table headings
void employee::printData() {
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
cout << setw(6) << employeename << setw(12) << employeeid << setw(4) << hoursworked << setw(3) << overtime << setw(8) << regularpay << setw(8)
<< overtimepay << setw(8) << grosspay << setw(8) << taxamount << setw(8) << netpay << endl;
} // print data
void employee::printReport() {
int i = 0;
printHeadings();
while (fin >> employeename >> employeeid >> hoursworked >> hourlyrate) {
calculateGrosspay();
calculateTax();
calculateNetpay();
printData();
i++;
}
}
double employee::findAverage() {
return average;
}
void printAverage() {
cout << endl;
cout << "The average netpay is" << average << endl;
}
void main() {
employee.printReport();
employee.findAverage();
employee.printAverage();
}
So after the program has printed the data to the console I want to find the average of the net pays and print it to the console. Best way to do this?
You'll probably need a vector like Soren mentioned. If you're trying to get some averages for all employees (which that's what I assume you're trying to do), then let's say we're going to try and get an average for gross pay.
You'll need a vector global, like such:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
vector<double> netPayList;
double globalAverage;
You'll then need to add information to the vector when GrossPay is calculated as such:
void employee::calculateNetpay()
{
netpay = grosspay - taxamount;
netPayList.push_back(netpay);
}
Finally, we can calculate the average in employee::findAverage(), we should also probably use a for loop for this as well, and in my opinion we should ditch the public function of "employee" and switch that over to a traditional function, since we are trying to get the average for many "employee" instances. The average calculation will now look like this:
double findAverage()
{
for (int iterator = 0; iterator < netPayList.size(); iterator++)
{
average += netPayList[iterator];
}
globalAverage /= netPayList.size();
return globalAverage;
}
Anyway, that's basically the gist of it. Keep working on your stuff, but also I would suggest that you title your questions better. Someone with more gumption and SO privileges than I will most likely edit your OP.

My code is returning two really weird errors

My code isn't working. I don't know why. The errors I am getting don't make sense.
17 18 C:\Users\the beast\Desktop\Case study phase 3.0.6.cpp [Error] a function-definition is not allowed here before '{' token
I dont even know why it throwing this error.
77 1 C:\Users\the beast\Desktop\Case study phase 3.0.6.cpp [Error] expected '}' at end of input
^ all brackets are present checked no idea why it is throwing this error
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
//loading libraries
float const taxnet = .9;
float const taxwh = .1;
float employeenumber;
float payrate=0;
float gross=0;
float net=0;
float manhours=0;
float overtime=0;
float taxes=0;
char usercontrols;
void data_loop(char usercontrols);
void writeWorkerInfo(ofstream &stream, float employeenumber, float manhours, float payrate, float gross, float taxes, float net);
void payrollcalc(float employeenumber, float manhours, float payrate, float gross, float taxes, float net);
void data_entry(float employeenumber, float manhours, float payrate);
void data_recall(float employeenumber);
void data_error_check(char usercontrols);
int main(){
// varibles
cout << "Hit 1 to enter new data; hit 2 load a file; hit escpae to exit." << endl;
cin >> usercontrols;
while (usercontrols != 27){
data_error_check(usercontrols);
}
return 0;
}
void data_error_check(char usercontrols){
cin >> usercontrols;
if(usercontrols == 49 || 50){
data_loop(usercontrols);
}
else if (usercontrols != 49 ||50){
cout << "Wrong button hit enter to return to main menu" << end;
cin >> usercontols;
if(usercontrols == 13){
main();
}
}
}
void data_loop(char usercontrols){
cin >> usercontrols;
if (usercontrols == 49){
data_entry();
}
else(usercontrols == 50){
data_recall();
}
}
void writeWorkerInfo(ofstream &stream, float employeenumber, float manhours, float payrate, float gross, float taxes, float net){
stream << " Your ID is " << employeenumber << endl;
stream << " # of hours worked " << manhours << endl;
stream << " Your Hourly rate is " << payrate << endl;
stream << " Your Gross pay is " << gross << endl;
stream << " Your tax rate is " << taxwh << endl;
stream << " Amount of taxes " << taxes << endl;
stream << " Your net pay is " << net << endl;
data_loop();
}
void payrollcalc(float employeenumber, float manhours, float payrate, float gross, float taxes, float net){
if (manhours > 40) {
overtime = manhours - 40;
gross = ((manhours - overtime) * payrate) + ((payrate * 1.5)* overtime);
//overtime claculation
}
else {
gross = manhours * payrate;
//no overtime calculation
}
taxes = gross * taxwh;
net = gross * taxnet;
//writeWorkerInfo(float employeenumber,float manhours,float payrate,float gross,float taxwh,float taxes,float net);
std::string empnum = std::to_string(employeenumber);
ofstream payroll;
payroll.open(empnum + ".txt");
writeWorkerInfo(float employeenumber,float manhours,float payrate,float gross,float taxes,float net);
payroll.close();
}
void data_entry(float employeenumber,float manhours,float payrate){
cout << "Enter Employee ID:";
cin >> employeenumber;
cout << "Enter Number of Hours Worked:";
cin >> manhours;
cout << "Enter Pay rate:";
cin >> payrate;
payrollcalc();
}
void data_recall(float employeenumber){
cout << "Enter employee number";
cin >> employeenumber;
///reading in data
std::string empnum = std::to_string(employeenumber);
ofstream payroll;
payroll.open(empnum + ".txt");
payroll.close();
data_loop();
}
Just get the functions definitions out of the main block code and call them inside main
These points should explain why it's not working:
The function main() needs to return a type.
You are defining all your functions inside main(), place them above main or below with function prototypes.
You are using variables inside functions that aren't declared yet.
Funtions are made so that it can be used in defferent place of the program, making it inside the main scope makes less sense!
Moreover we can't define funtion inside another function, we can just call it from nother fuction that comes after its declaration!
2ndly your main says nothing about returning.
ReturnType FunctionName (dataType args, ...){ FuntionBody}
Refer to the above syntax of a funtion declaration.
A function must always provide its return type as prefix or void if it dont return anything!
Note: In some compiler void main() works just fine, but some compiler says main should return something. So as a standard method you must define main as int not void to safegaurd urself.