I am trying to figure out what is wrong with my code. I must use a structure to prompt the user for a workers idNumber, hoursWorked, and hourlyWage. I have the idNumber, hoursWorked, and hourlyWage working well, however the problem lies in my calc function. I cannot figure out how to calculate the money earned with overtime being 1.5 times and to be able to print that to the screen. I keep getting a mash up of weird numbers.
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void calc(Worker & theWage);
void main()
{
Worker Data;
input(Data);
print(Data);
system("pause");
}
void input(Worker & theData)
{
cout << "Enter the Employee idNumber";
cin >> theData.idNumber;
cout << "Enter the Hours Worked.";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours.";
cin >> theData.hourlyRate;
}
void print(const Worker & w)
{
cout << w.idNumber << "\n"
<< w.hoursWorked << "\n"
<< w.hourlyRate << "\n"
<< w.earned << endl;
}
void calc(Worker & theWage)
{
if (theWage.hoursWorked <= 40)
{
theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
}
else
{
int basePay;
basePay = theWage.hoursWorked * theWage.hourlyRate;
theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
}
}
Your calc() function must be called in main before your print() function.
void main()
{
Worker Data;
input(Data);
calc(Data); // This line was forgotten.
print(Data);
system("pause");
}
The weird number you saw was the uninitialized earned variable.
Related
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?
So I put a while loop in my header file and I wanted to connect it to my main.cpp but then my main.cpp is not playing it unless it is stated like in my header file. So I'm wondering if a while loop in the header even necessary? The assignment is to create a program that calculates the user's salary given her sales.
This is my header file:
class Salary
{
public:
Salary(){};
Salary(double employeeSales, double employeeSalary)
:sales{employeeSales},salary(employeeSalary)
{
while(employeeSales != -1)
{
salary = 200 + (.09 * sales);
}
}
void setSales(double employeeSales)
{
sales = employeeSales;
}
void setSalary(double employeeSalary)
{
salary = employeeSalary;
}
double getSales() const{return sales;}
double getSalary() const{return salary;}
private:
double sales;
double salary;
};
this is my main.cpp
#include <iostream>
#include <iomanip>
#include "Salary.h"
using namespace std;
int main()
{
Salary mySalary;
double employeeSales;
double employeeSalary;
cout << fixed << setprecision(2);
cout << "Enter sales in dollars(-1 to quit): ";
cin >> employeeSales;
mySalary.setSales(employeeSales);
while(true)
{
employeeSalary = 200 + (.09 * employeeSales);
mySalary.setSalary(employeeSalary);
cout << "Salary is: $" << mySalary.getSalary();
cout << "Enter sales in dollars(-1 to quit): ";
cin >> employeeSales;
mySalary.setSales(employeeSales);
}
}
However the while loop in main won't work unless i put while(employeeSales != -1)
Rather than have your constructor handle garbage data (-1 as the argument), just create an if statement in the while-loop in main that breaks if the user entered -1.
while(true)
{
employeeSalary = 200 + (.09 * employeeSales);
mySalary.setSalary(employeeSalary);
cout << "Salary is: $" << mySalary.getSalary();
cout << "Enter sales in dollars(-1 to quit): ";
cin >> employeeSales;
if( employeeSales == -1)
{
cout << "Stopping program!" << endl;
break; // exits the while loop
}
mySalary.setSales(employeeSales);
}
Doing this allows for a while-loop in your Salary constructor to be unnecessary, and should hopefully solve your issue.
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.
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.
Been trying to figure out how to input information into an array and output it from structured variables. I can input and output the information easy without using arrays, however I am trying have a list of workers inputed from the keyboard. I also need to calculate the wage earned for each worker, but If I can just figure out the format of the simple information from the first three variables I can figure that part out on my own. I have tried to call the input function in the array but have had no success so I just deleted it and left the functions.
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void input(Worker[], int howMany);
void print(const Worker[], int);
void main()
{
Worker arr[10];
Worker Data;
input(Data);
print(Data);
input(arr[10],7);
system("pause");
}
void input(Worker& theData) {
cout << "Enter the Employee idNumber ";
cin >> theData.idNumber;
cout << "Enter the Hours Worked. ";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours. ";
cin >> theData.hourlyRate;
}
void input(Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
input(arr[i]);
}
}
void print(const Worker& w) {
cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}
void print(const Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
print(arr[i]);
}
}
You're almost there. This sounds like coursework, so I'm not going to suggest using a std::vector. Look up std::vector, by the way, and use it when you can. It'll save you a lot of grief.
To use an array, you first need to know the size of the array and reserve space. There are a number of ways to get the number of workers, but this is probably the simplest:
int main()
{
int howMany;
cout << "How many?";
cin >> howMany;
Worker * workers = new Worker[howMany];
After that, it's pretty much keep doing what you're doing, but call the array-based functions and clean up when you're done.
input(workers, howMany);
calc(workers, howMany);
print(workers, howMany);
system("pause");
delete[] workers;
}
Ok so if I am understanding you correctly, you would like to be able to read in information about a bunch of workers and store it into an array of structs. Correct my if I am wrong, but if that is what you would like to do I think you can actually cheat this a little bit using the single worker input function like so.
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker& theData) {
cout << "Enter the Employee idNumber ";
cin >> theData.idNumber;
cout << "Enter the Hours Worked. ";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours. ";
cin >> theData.hourlyRate;
}
void input(Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
input(arr[i]);
}
}
void print(const Worker& w) {
cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}
void print(const Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
print(arr[i]);
}
}
Some notes, you want to delete the const from the array input function. You are going to modify the array it can't be const. I added some spaces after the "Enter the blah. " so that it looks prettier. You can figure out the calc on your own as you said, just follow the form here to figure out how to do that for an array of things. Once you have written to the function for a single guy, it is pretty simple to do it for the array as you can see.
Also for your education just remember that arrays are inherently pass by reference so that's why we can just throw them into the pass by reference functions with no problems.
I should add that in the future you can access the parts of each workers information from the array directly by using:
arr[i].idNumber;
arr[i].any_struct_variable;
Thank you all for helping! I finally figured it out and It is working well.
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void calcInput(Worker[], int howMany);
void input(Worker[], int howMany);
void print(const Worker[], int);
void calc(Worker & theWage);
void main()
{
Worker Data;
int howMany;
cout << "How many?";
cin >> howMany;
Worker * workers = new Worker[howMany];
input(workers, howMany);
calcInput(workers, howMany);
print(workers, howMany);
system("pause");
delete[] workers;
}
void input(Worker& theData) {
cout << "Enter the Employee idNumber ";
cin >> theData.idNumber;
cout << "Enter the Hours Worked. ";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours. ";
cin >> theData.hourlyRate;
}
void input(Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
input(arr[i]);
}
}
void print(const Worker& w) {
cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}
void print(const Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
print(arr[i]);
}
}
void calc(Worker & theWage)
{
if (theWage.hoursWorked <= 40)
{
theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
}
else
{
int basePay;
basePay = theWage.hoursWorked * theWage.hourlyRate;
theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
}
}
void calcInput(Worker arr[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
calc(arr[i]);
}
}