below is a .cpp file that i am including in another .cpp file. I am getting an error that states emp.grosPay() must be a modifiable lvalue. any ideas on fixing it?
#include <iostream>
#include <string>
#include "weeklyEmp.h"
using namespace std;
const double WEEKLY_ALLOWANCE = 39.42;
const double FICA_TAX_RATE = 0.0765;
weeklyEmp::weeklyEmp()
{
my_name = ?name?;
}
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus)
{
my_name; initName;
my_hours; initHours;
my_rate; initRate;
my_exemptions; initExemptions;
my_filingStatus; initFilingStatus;
}
//--modifiers
void weeklyEmp::set_hours(double thisWeeksHours)
//post: Set the hours worked for a given week
{
my_hours = thisWeeksHours;
}
void weeklyEmp::set_rate(double thisWeeksRate)
//post: Change the employee's hourly rate of pay
{
my_rate = thisWeeksRate;
}
//--accessors
double weeklyEmp::grosPay() const
//post: Return gross pay with overtime
{if(my_hours <=40) return my_hours * my_rate;
else
return (40*my_rate) + (my_hours-40) * 1.5 * my_rate;
}
double weeklyEmp::incomeTax() const
//post: Return the federal income tax
{
double result(0.0);
double taxableIncome(grosPay() - my_exemptions * WEEKLY_ALLOWANCE);
if(my_filingStatus == "S" || my_filingStatus == "s")
{
if (taxableIncome <= 23.00)
result = 0.00;
else if(taxableIncome <= 397.00)
result = 0.15 * (taxableIncome - 23.00);
else if(taxableIncome <= 928.00)
result = 56.10 + 0.28 * (taxableIncome - 397.00);
else if(taxableIncome <= 2121.00)
result = 204.78 + 0.33 * (taxableIncome - 928.00);
else
result = 598.47 + 0.28 * (taxableIncome - 2121.00);
}
if(my_filingStatus == "M" || my_filingStatus == "m")
{
if(taxableIncome <= 65.00)
result = 0.00;
else if(taxableIncome <= 689.00)
result = 0.15 * (taxableIncome - 65.00);
else if(taxableIncome <= 1573.00)
result = 93.60 + 0.28 * (taxableIncome - 689.00);
else if(taxableIncome <= 3858.00)
result = 341.12 + 0.33 * (taxableIncome - 1573.00);
else
result = 1095.17 + 0.28 * (taxableIncome - 3858.00);
}
/* round to the nearest penny */
/* include compfun.cpp for round function */
result =(result, 2);
return result;
}
double weeklyEmp::FICATax() const
//post: Return the social security tax
{
return grosPay() * FICA_TAX_RATE;
}
string weeklyEmp::name() const
//post: Return the employee's name
{ return my_name;
}
The portion of code with the error is below and marked * **
int main()
{
string name;
double rate;
double hours;
int exemptions;
string filingStatus;
cout <<"Name: ";
cin >> name;
cout << "Hourly Rate:";
cin >> rate;
cout << "Hours Worked:";
cin >> hours;
cout << "Exemptions: ";
cin >> exemptions;
cout<< "S) ingle / M) arried: ";
cin >> filingStatus;
cout << " " << endl;
weeklyEmp emp(name, hours, rate, exemptions, filingStatus);
double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax();
}
double net = emp.grosPay() = emp.incomeTax() - emp.FICATax();
// ^^^
grosPay doesn't return a reference, so I'm assuming you didn't mean to use an = sign there. May have been a slip of the thumb.
Did you mean to use the minus operator -?:
double net = emp.grosPay() - emp.incomeTax() - emp.FICATax();
// ^^^
The error was occurring because you can't modify a const object, an object returned by a const function, or an rvalue that isn't bound to a non-const rvalue-reference.
double net = ***emp.grosPay()*** = emp.incomeTax() - emp.FICATax();
^^^
The second '=' should be a '-' sign; I think you're trying to subtract, not assign.
Related
I have this program, which should give me the smallest original price of the item based on the input from the user.
There are some conditions, for example, if the quantity of the mask is more than 9 the price will be discounted by 10%, 15% if its more than 30 and 20% if it's more than 50. The result should give the answer Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int mprice; //price input
int mquantity; //quantity input
int n; //first input
int fee = 2000; //const fee
float finalprice;
float maskCalc(int price, int quantity) {
float holder = (float)(price - fee) / (float)(quantity);
if (quantity > 0) {
finalprice = holder;
}
//if between 10 and 30
else if (quantity > 9) {
finalprice = holder / 0.9;
}
//between 30 and 49
else if (quantity > 30) {
finalprice = holder / 0.85;
}
//more than 50
else if (quantity > 49) {
finalprice = holder / 0.8;
}
//less than ten
else {
finalprice = holder;
}
return finalprice;
}
int main()
{
cin >> n;
float arr[n];
// Input oruulah loop
for (int i = 0; i < n; i++) {
cin >> mprice >> mquantity;
x = maskCalc(mprice, mquantity);
arr[i] = x;
}
for (int i = 1; i < n; i++) {
if (arr[0] > arr[i]) {
arr[0] = arr[i];
}
}
printf("%.2f", arr[0]);
return 0;
}
I gave the input
3
5000 3
7000 10
3000 1
the answer was 555.56 which is correct, but when I give something like
3
2500 1
7000 10
3000 1
it is giving me 0.00 while I was expecting this to give me 500.00. Any help will be appreciated.
You need to check for the highest quantity first in your if-else switch, otherwise you always fall into the default (<10) case.
//more than 50
if (quantity >= 50) {
finalprice = holder / 0.8;
}
//between 30 and 49
else if (quantity >= 30) {
finalprice = holder / 0.85;
}
//if between 10 and 30
else if (quantity >= 10) {
finalprice = holder / 0.9;
}
//less than ten
else {
finalprice = holder;
}
I'm doing a homework problem right and need some help figuring out how to solve a compiler error: [Error] expected unqualified-id before 'if'.
I've tried including the functions in the int main function but that leads to more errors. I've tried looking for missing curly braces but can't seem to find any.
"if (surge != 'Y')" is where I'm getting [Error] expected unqualified-id before 'if'.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
double calcFare (double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, char carClass);
double calcFareSurge (double surgeFare, double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, double surgeMultiplier, char carClass);
double test;
void output (string name, double totalFare);
int main()
{
string name;
double surgeFare, perMinute, lengthMinutes, minuteFare, perMile, distanceMiles, mileFare, surgeMultiplier, baseFare, totalFare;
char surge, carClass;
cout << "Enter name: " << endl;
getline(cin, name);
cout << "Enter time in minutes: " << endl;
cin >> lengthMinutes;
cout << "Enter distance in miles: " << endl;
cin >> distanceMiles;
cout << "Enter 'X' for uberX or 'S' for SUV or 'L' for luxury: " << endl;
cin >> carClass;
carClass = (char) toupper(carClass);
cout << "Enter 'Y' if surge or 'N' if not: " << endl;
cin >> surge;
surge = (char) toupper(surge);
if (surge == 'Y')
{
cout << "Enter surge multiplier: " << endl;
cin >> surgeMultiplier;
}
totalFare = calcFare(perMinute, lengthMinutes, minuteFare, perMile, distanceMiles, mileFare, baseFare, totalFare, carClass);
output(name, totalFare);
system ("pause");
return 0;
}
if (surge != 'Y')
{
double calcFare (double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, char carClass)
{
if (carClass == 'X')
{
baseFare = 2.00;
perMinute = 0.22;
perMile = 1.15;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
else if (carClass == 'S')
{
baseFare = 15.00;
perMinute = 0.90;
perMile = 3.75;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
else if (carClass == 'L')
{
baseFare = 5.00;
perMinute = 0.50;
perMile = 2.75;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
}
else
{
double calcFareSurge (double surgeFare, double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, double surgeMultiplier, char carClass)
{
if (carClass == 'X')
{
baseFare = 2.00;
perMinute = 0.22;
perMile = 1.15;
surgeFare = baseFare * surgeMultiplier;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
else if (carClass == 'S')
{
baseFare = 15.00;
perMinute = 0.90;
perMile = 3.75;
surgeFare = baseFare * surgeMultiplier;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
else if (carClass == 'L')
{
baseFare = 5.00;
perMinute = 0.50;
perMile = 2.75;
surgeFare = baseFare * surgeMultiplier;
minuteFare = perMinute * lengthMinutes;
mileFare = perMile * distanceMiles;
totalFare = minuteFare + mileFare + baseFare;
}
}
}
void output(string name, double totalFare)
{
cout << setprecision(2) << fixed;
cout << "Rider's name: " << name << endl;
cout << "Total: $ " << totalFare << endl;
return;
}
if (surge != 'Y')
{.........
.......
}
The above mentioned code segment needs to be put in a separate function or inside main ().
(a) In case you are moving this to inside the main function you will still get another error as you have defined double calcFare(),inside both if and else ,you can only make a function call from inside of if -else ,not a function definition ,in both cases you will have to define the function outside of if and else.
(b) You can create a new function and have handle the cases accordingly .
Suggested Reading
(-) Difference between Function call and Function definition.
(-) Return a value from a function call
Hope this helps
cout << "Your change is " << change << ". Here's your change:";
char q = '#';
double ccounter = 0;
while(ccounter <= (change - .24)){
cout << q;
ccounter = ccounter + .25;
}
char d = '^';
while(ccounter <= (change - .09))
{
cout << d;
ccounter = ccounter + .10;
}
char n = '&';
while(ccounter <= (change - .04)){
cout << n;
ccounter = ccounter + .05;
}
char p = '*';
while(ccounter <= change){
cout << p;
ccounter = ccounter + .01;
}
return 0;
}
Once your customer pays you the amount that is greater or equal to 1.87, give them the change using the symbols listed above. So, for example, if your customer paid with 5 dollars, you should print out:
Your change is 3.13. Here’s your change: $$$^***
First is the code second block is the prompt.
When the user inputs 3 for what they'd like to pay everything is fine, but 4 gets four penny icons when really it should be three since the change is 0.13.
Any help or advice would be greatly appreciated!
thank you guys:)
[ * ] = Penny (1 cent)
[ & ] = Nickel (5 cents)
[ ^ ] = Dime (10 cents)
[ # ] = Quarter (25 cents)
[ $ ] = 1 dollar
The loop is ending at the wrong time because the ccounter variable is accumulating errors from floating point round-off. If you calculate 2 + 0.1 + 0.01 + 0.01 + 0.01, you get 2.1299999999999994. You should change everything to use integer math, i.e. start with a counter of 213 cents, then subtract 25, 10, 5, and 1 from that. If you stick with integer math, it'll work reliably.
Your loop termination conditions appear to all have one-off errors. It may be more clear to write the code as:
cout << "Your change is " << change << ". Here's your change:";
char q = '#';
double ccounter = 0;
while((change - ccounter) >= 0.25)
{
cout << q;
ccounter = ccounter + .25;
}
char d = '^';
while((change - ccounter) >= 0.10)
{
cout << d;
ccounter = ccounter + .10;
}
char n = '&';
while((change - ccounter) >= 0.05)
{
cout << n;
ccounter = ccounter + .05;
}
char p = '*';
while((change - ccounter) >= 0.01 )
{
cout << p;
ccounter = ccounter + .01;
}
return 0;
}
I need to write a program that calculates the cost for the distance traveled following these rules
Each of the first 100 miles (inclusive) cost £5.50
Over 100 miles and up to 500 miles (inclusive): £4.00 per mile.
Over 500 miles: £2.50 per mile.
This is my program so far
#include <iostream> //for cin >> and cout <<
using namespace std;
int main() // main algorithm
{
double distance;
double Milecost1;
double Milecost2;
double Milecost3;
double totalcost;
cout << "enter distance";
cin >> (distance);
void CFM();
if (distance >= 100) {
Milecost1 = 100 * 5.50;
} else {
totalcost = distance * 5.50;
}
void CSM();
if ( (distance >100) && (distance <= 500) ) {
Milecost2 = (distance - 100) *4.00;
}
void CTM();
if (distance > 500) {
Milecost3 = distance - 500 * 2.50;
}
void totalcost1();
totalcost = Milecost1 + Milecost2 + Milecost3;
cout << "the total cost for the distance travelled is" << totalcost
system("pause"); //to hold the output screen
return(0);
}
My first question is, is the maths correct to work out the cost?
Second question is I run the program and it says Milecost 2 is being used without being initialized how do I solve this?
No, the math is not correct, e.g. with distance = 501, you'd get
Milecost1: 550
Milecost2: (unitialised)
Milecost3: 2.50
That's assuming you correct the operator precedence on Milecost3, since right now you're multiplying 500 times 2.5 and subtracting that from distance.
Milecost2 is only assigned when distance is within its relevant values, instead, it should be 0 for distance <= 100 AND it should also be calculated when distance > 500, if I understood the exercise correctly.
I would write it down as:
#include <iostream> //for cin >> and cout <<
using namespace std;
int main() // main algorithm
{
double distance;
double Milecost1;
double Milecost2;
double Milecost3;
double totalcost;
cout << "enter distance";
cin >> (distance);
if (distance >= 100) {
Milecost1 = 100 * 5.50;
} else {
Milecost1 = distance * 5.50;
}
if ( (distance >100) && (distance <= 500) ) {
Milecost2 = (distance - 100) *4.00;
} else {
Milecost2 = 0;
}
if (distance > 500) {
Milecost3 = distance - 500 * 2.50;
} else {
Milecost3 = 0;
}
totalcost = Milecost1 + Milecost2 + Milecost3;
cout << "the total cost for the distance travelled is" << totalcost ;
system("pause"); //to hold the output screen
return(0);
}
getting rid of your calls to undefined functions; and not depending on initialization of variables to 0.
I think this should work fine
void main(void)
{
double distance;
double cost;
if(distance <= 100)
{
cost = distance * 5.50;
}
else if(distance > 100 && distance <= 500)
{
cost = 100 * 5.50;
cost += 4.00 * (distance - 100);
}
else
{
int temp = 100;
cost = temp * 5,50;
temp = 400;
cost += temp * 4.00;
temp = distance - 500;
cost += temp * 2.50;
}
}
Personally, this is the way I'd do it:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
double distance;
cout << "Enter distance (Miles): ";
cin >> distance;
double milesOver500 = max(0.0, distance-500.0);
double milesInBetween = max(0.0, distance-milesOver500-100.0);
double milesUnder100 = max(0.0, distance-milesInBetween-milesOver500);
double totalCost = milesOver500 * 2.5 +
milesInBetween * 4.0 +
milesUnder100 * 5.5;
cout << "The total cost for the distance traveled is £" << totalCost << endl;
return 0;
}
In your implementation, Milecost2 is only initialized if the conditional (distance >100) && (distance <= 500) is true, which won't always be the case. In the code above, everything is initialized at the time of its declaration with a meaningful value that doesn't need to be computed later. It just so happens that this approach works well in this case, but there are others where it's a bit more difficult to do so. When such cases arise you should try to assign some meaningful default value to each variable you declare at the time of its declaration which can change if certain conditions arise.
I am trying to print out the variable *loan_Amt* when the monthly payment exceeds/is equal to the 30% amount that is found in the *monthly_payment* function. This is my first attempt at writing a c++ program with functions!
#include<iostream>
#include<conio.h>
#include<string>
#include<iomanip>
#include<math.h>
using namespace std;
double monthly_Payment (double amt_Amt)
{
double r;
r = ( amt_Amt/ 12) * 30/100;
return (r);
}
double interest_Calculate(double interest_Amt)
{
double r;
r = (interest_Amt * .010);
return (r);
}
//double loan_Calculate()
//{
//int x = interest_Calculate(interest_Rate);
//double monthly = (loan_Amt * x) / (1 - pow(1.0 + i,-(12*30)));
//for (((int loan_Amt = 20000) * (x/12)) / pow(1.0 + i,-(12*30)); loan_Amt>0; loan_Amt++);
//}
int main()
{
double gross_Salary;
double interest_Rate;
int x;
int i;
double monthly;
std::cout << "Please enter your yearly gross salary:";
std::cin >> gross_Salary;
std::cout << "Please enter an interest rate:";
std::cin >> interest_Rate;
int z;
z = monthly_Payment (gross_Salary);
std::cout << "The target (30 percent of monthly salary) monthly payment range is:" << z;
for ( int loan_Amt = 0; loan_Amt <= 5000000; x++ ) {
do {
x = interest_Calculate(interest_Rate);
monthly = (loan_Amt * x) / (1 - pow(1.0 + x,-(12*30)));
std::cout << loan_Amt;
} while (monthly >= z );
}
getch();
return 0;
}
The for loop has problem, I think,
for ( int loan_Amt = 0; loan_Amt <= 5000000; x++ ) {
do {
x = interest_Calculate(interest_Rate);
monthly = (loan_Amt * x) / (1 - pow(1.0 + x,-(12*30)));
std::cout << loan_Amt;
} while (monthly >= z );
}
loan_Amt will never change its value, so this is an infinite loop.
This line in the do-while loop,
std::cout << loan_Amt;
will always print zero