Hello I am trying to make a program that will calculate an employees bonus when given his salary and job performance, although each time i run the code the bonus is calculated to 1 regardless of the salary I input. and I have to keep my function i created it is part of my homework. Any guidance on what the issue is would be appreciated
#include <iostream>
using namespace std;
double bonus(double salary,int a);
int main()
{
double salary;
enum jobp{poor = 0,average = 1,good = 2};
jobp performance;
int a = performance;
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus(salary,a);
cout<<"your bonus is "<<bonus;
return 0;
}
double bonus(double salary, int a)
{
double bonus;
if (a == 2)
{
double c;
c = .10;
bonus = salary * c;
return bonus;
}
else if (a == 1)
{
double b;
b = .05;
bonus = salary * b;
return bonus;
}
else
{
return 0;
}
}
Hi this should help you fix your problem.
Change this:
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus(salary,a);
cout<<"your bonus is "<<bonus;
To this:
cout << "Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)" <<endl;
cin >> salary >> a;
cout << "your bonus is " << bonus(salary, a);
catch the value of returned bonus
#include <iostream>
using namespace std;
double bonus(double salary,int a); //this is for function right?
int main()
{
double salary;
double bonus; //create a variable for bonus
enum jobp{poor = 0,average = 1,good = 2};
jobp performance;
int a = performance;
cout<<"Enter your salary and job performance (as a 0 for poor,1 for average and 2 for good)s$
cin>>salary>>a;
bonus = bonus(salary,a); //catch the return
cout<<"your bonus is "<<bonus;
return 0;
}
Try to have the user to enter salary and performance separately.
cout << "Please enter your salary: " << endl;
cin >> salary;
cout "Please job performance (as a 0 for poor,1 for average and 2 for good)" << endl;
cin >> a;
Related
I am working on this exercise and it is pretty much complete but the math has to be wrong somewhere when calculating the senior citizen discount. The software that I use runs these two pieces of input to check the problem.
20
10
n
2
12 (which runs fine)
20
10
y
2
12 (does not not give me expected result)
This leads me to believe that the problem lies within the senior citizen discount part in the double determineMembershipCost function.
The expected result is "The membership cost = $162.80" but my code gives me "The membership cost = $152.00"
I am not sure what is wrong here. I'm hoping a second set of eyes can help find it. Thank you in advance.
Here is the code:
// headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// prototypes
void displayGeneralInformation ();
void readNecessaryInformation (double& regularCostPerMonth,
double& costPerPersonalTrainingSession,
bool& seniorCitizen, int& numberOfSessions,
int& numberOfMonths);
double determineMembershipCost (double regularCostPerMonth,
double costPerPersonalTrainingSession,
bool seniorCitizen, int numberOfSessions,
int numberOfMonths);
// main
int main()
{
//variables
double regularCostPerMonth;
double costPerPersonalTrainingSession;
bool seniorCitizen;
int numberOfSessions;
int numberOfMonths;
double cost;
// print menu
// calls
// call displayGeneralInformation
displayGeneralInformation ();
// call readNecessaryInformation
readNecessaryInformation (regularCostPerMonth,
costPerPersonalTrainingSession,
seniorCitizen, numberOfSessions,
numberOfMonths);
// call determineMembershipCost
cost = determineMembershipCost (regularCostPerMonth, costPerPersonalTrainingSession, seniorCitizen, numberOfSessions, numberOfMonths);
// Display cost of membership
cout << "\nThe membership cost = $" << setprecision(2)<< fixed << cost << endl;
return 0;
}
// displayGeneralInformation function definition
void displayGeneralInformation ()
{
cout << "\nWelcome to Stay Healthy and Fit center." << endl;
cout << "This program determines the cost of a new membership." << endl;
cout << "If you are a senior citizen, then the discount is 30% off of the regular membership price." << endl;
cout << "If you buy membership for twelve months and pay today, the discount is 15%." << endl;
cout << "If you buy and pay for 6 or more personal training session today, the discount on each session is 20%." << endl;
}
// readNecessaryInformation function definition
void readNecessaryInformation (double& regularCostPerMonth,
double& costPerPersonalTrainingSession,
bool& seniorCitizen, int& numberOfSessions,
int& numberOfMonths)
{
cout << "\nEnter the cost of a regular membership per month: $";
cin >> regularCostPerMonth;
cout << "Enter the cost of one personal training session: $";
cin >> costPerPersonalTrainingSession;
cout << "Are you a senior citizen (Y,y/N,n): ";
char ch;
cin >> ch;
if (ch == 'Y' || ch == 'y')
seniorCitizen = true;
else
seniorCitizen = false;
cout << "Enter the number of personal training sessions bought: ";
cin >> numberOfSessions;
cout << "Enter the number of months you are paying for: ";
cin >> numberOfMonths;
}
// determineMembershipCost function definition
double determineMembershipCost (double regularCostPerMonth, double costPerPersonalTrainingSession, bool seniorCitizen, int numberOfSessions, int numberOfMonths)
{
double cost = regularCostPerMonth * numberOfMonths;
if (seniorCitizen)
{
cost = cost - (regularCostPerMonth * 0.30 * numberOfMonths);
}
if (numberOfMonths >= 12)
{
cost = cost - (regularCostPerMonth * 0.15 * numberOfMonths);
}
cost = cost + (costPerPersonalTrainingSession * numberOfSessions);
if (numberOfSessions > 5)
{
cost = cost - (costPerPersonalTrainingSession * 0.20 * numberOfSessions);
}
return cost;
}
Try this instead:
cost = (cost - (cost * 0.30)); //for 30% off
cost = (cost - (cost * 0.15)); //15% off
I'm new in programming and new in here.
Sorry for stupid question but i have problem with result in my "calculate your age in seconds" code. It gives me weird result like 6.17725e+10 or -6.17414e+10.
Program isn't finished yet but everything except results looks fine (i don't get any error.
Sorry again and I hope for your understanding:)
#include <iostream>
using namespace std;
void title()
{
cout << "Age Calculator" << endl << endl;
}
int byear()
{
cout << "Enter your birth year: ";
int by;
cin >> by;
return by;
}
int bmonth()
{
cout << "Enter your birth month: ";
int bm;
cin >> bm;
return bm;
}
int bday()
{
cout << "Enter your birth day: ";
int bd;
cin >> bd;
return bd;
}
int cyear()
{
int cy;
cout << "Enter current year ";
cin >> cy;
return cy;
}
int cmonth()
{
cout << "Enter current month: ";
int cm;
cin >> cm;
return cm;
}
int cday()
{
cout << "Enter current day: ";
int cd;
cin >> cd;
return cd;
}
void calculate(int by, int bm, int bd, int cy)
{
double y = 31104000;
long double cby = y * by;
long double cbm = 259200 * bm;
long double cbd = 8640 * bd;
long double ccy = 31104000 * cy;
cout << endl << cby << endl;
cout << endl << ccy << endl;
cout << endl << ccy - cby << endl;
}
int main()
{
title();
int by = byear();
int bm = bmonth();
int bd = bday();
int cy = cyear();
int cm = cmonth();
int cd = cday();
calculate(by, bm, bd, cy);
cin.get();
return 0;
}
Like Kenny Ostrom commented, the shown values may look strange due to the scientific notation used by cout. To show all digits, you can change cout's precision using cout.precision(your_precision_here). See question below.
How do I print a double value with full precision using cout?
First, the numeric format you are confused by is "scientific notation". That will be enough info to open up a world of google searches, or you can just force it not to print in scientific notation.
Second, you really want to use a time library for any calendar stuff. It will handle all kinds of calendar weirdness for you, including leap years. Fortunately we have time.h
Third, I recommend using an integer type for seconds, partly to avoid rounding errors and ugly decimals, but mainly because that's what time.h uses. Just make sure it is big enough. My compiler uses a 64 bit integer for time_t, so I used that:
#include <time.h>
#include <memory>
time_t get_age_in_seconds(int year, int month, int day)
{
struct tm birthday;
memset(&birthday, 0, sizeof(birthday));
birthday.tm_year = year - 1900; // years since 1900
birthday.tm_mon = month - 1; // months since January (0,11)
birthday.tm_mday = day; // day of the month (1,31)
time_t birthday_in_seconds = mktime(&birthday);
time_t now = time(NULL);
return now - birthday_in_seconds;
}
Don't use doubles to do the calculation. You're not going to have any fractional values since you're not doing any division.
More importantly, look into mktime(), time(), and difftime(). You should be using these to do your calculation.
Hello I've ran into some trouble creating a GroceryItem class and using functions to accept and set input from a user.
Currently when I run the dataEntry function, the compiler moves on to the next function before accepting input from the first function.
I've created a test milk object to test my code but It doesn't allow me to enter data before moving to the next input prompt.
Once I can figure out the class functions, I will also create an array of objects and input values for such.
Any advice for how I can go about fixing this class and functions would be greatly appreciated!
#include <iostream>
using namespace std;
class GroceryItem{
private: int stockNumber;
double price = 0.0;
int quantity;
double totalValue;
double setPrice();
int setStockNum();
int setQuantity();
void setTotalValue();
public:
void dataEntry();
void displayValues();
};
int GroceryItem::setStockNum(){
int stock = 0;
cout << "Enter the stock number for the grocery item: ";
do {
cout << "Stock Number(1000-9999): ";
cin >> stock;
} while (!(stock >= 1000 && stock <= 9999));
stockNumber = stock;
return stockNumber;
}
double GroceryItem::setPrice(){
double x = 0.0;
cout << "Enter the price of the item: ";
while (!(x > 0)) {
cout << "Please enter a positive number for price!";
cin >> x;
}
price = x;
return price;
}
int GroceryItem::setQuantity(){
int x = 0;
cout << "Enter the quantity in stock: ";
while (!(x > 0)){
cout << "Please enter a positive number for quantity!";
cin >> x;
}
quantity = x;
return quantity;
}
void GroceryItem::setTotalValue(){
totalValue = (quantity * price);
}
void GroceryItem::dataEntry(){
setStockNum();
system("pause");
setPrice();
system("pause");
setQuantity();
system("pause");
setTotalValue();
}
void GroceryItem::displayValues(){
cout << "Stock number: " << stockNumber;
cout << "\nItem price: " << price;
cout << "\nQuantity on hand: " << quantity;
cout << "\nTotal value of item: " << totalValue;
}
int main(){
GroceryItem Milk;
Milk.dataEntry();
Milk.displayValues();
system("pause");
return 0;
}
Dude, pay attention to the condition of the while statement, the line
!(stock >= 1000 || stock <= 9999)
returns true for stock = 0 (always true, in this case), so the program won't enter the loop.
Maybe you meant something like:
!(stock >= 1000 && stock <= 9999)
AND(&&) not OR(||)
I've been trying to figure this out for sometime and I think it has something to do with the values I'm using for the calculations. I'm not exactly familiar with compound interest so I'm not sure were I'm going wrong. Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
double interest_credit_card(double initial_balance, double interest_rate, int payment_months);
// Calculates interest on a credit card account according to initial balance,
//interest rate, and number of payment months.
int main ()
{
double initial_balance, interest_rate;
int payment_months;
char answer;
do
{
cout << "Enter your initial balace: \n";
cin >> initial_balance;
cout << "For how many months will you be making payments?\n";
cin >> payment_months;
cout << "What is your interest rate (as a percent)?: %\n";
cin >> interest_rate;
cout << endl;
cout << "You will be paying: $ " << interest_credit_card( initial_balance, interest_rate, payment_months) << endl;
cout << "Would you like to try again? (Y/N)\n";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
cout << "Good-Bye.\n";
return 0;
}
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double compound_interest, compounding, compounding2, compounding3, compounding4;
while(payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate/100.0);
compounding = (interest_rate /12);
compounding2 = compounding + 1;
compounding3 = interest_rate * (payment_months/12);
compounding4 = pow(compounding2, compounding3);
compound_interest = initial_balance * compounding4;
initial_balance = initial_balance + compound_interest;
payment_months--;
}
return initial_balance;
}
Inputs and expected outputs:
Enter your initial balance: 1000
For how many months will you be making payments?: 7
What is your interest rate (as a percent)?: 9
You will be paying: $1053.70
It looks like you were trying a bunch of things and then left them in. The first solution you tried was almost right, you just forgot "/12":
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
while (payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate / 100.0/12);
payment_months--;
}
return initial_balance;
}
With a little better style:
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double total_payment = initial_balance;
double monthly_rate = interest_rate / 100.0 / 12;
for (int month = 1; month <= payment_months; ++month)
total_payment += total_payment * monthly_rate;
return total_payment;
}
I am trying to make a talent show type voting program using functions.
I have the majority of it figured out. The program prompts you to enter a name, followed by five scores, if you type "Done" rather than a name, it will close. I'm using functions for a majority of the code to practice with them.
My big issue is that there could be an infinite amount of names (as many as the user enters) and I am unaware on how to add up all 5 scores per name, I do not know how to differentiate between them. The 5 scores will be averaged and the person with the highest average of (3 scores, dropping 2) will be the winner.
Side Note: I need to drop the highest and lowest score of each person, I believe I could figure it out but an example with a function of this would be helpful to someone who is new to them.
I've researched this a lot but I could not find any examples that are similar enough to mine (having a possibly infinite amount of contestants.)
Here is my code so far, the function at the bottom is me messing around with functions to get a hang of them and see if i can get any sums of scores from a name.
#include <iostream>
#include <string>
using namespace std;
void validCheck();
void calcAvgScore();
void findHigh();
void findLow();
int main(){
int judge = 1;
double score = 0;
string name;
while (name != "done" || name != "Done"){
cout << "Enter Contestant Name, if no more, type 'done': ";
cin >> name;
if (name == "done" || name == "Done"){ break; }
for (judge = 1; judge < 6; judge++){
cout << "Enter score " << judge << " ";
validCheck();
}
}
system("pause");
return 0;
}
void validCheck(){
double score;
cin >> score;
if (score < 1 || score > 10){
cout << "Please Enter a score between 1 and 10: ";
cin >> score;
}
}
void calcAvgCheck(){
double score = 0, value = 0;
static int average;
score += value
}
Declare a string "winner", double "win_avg", double "avg" outside the while loop.
Have your validCheck() return the double value given as input (named score).
Declare a double array before your for loop (double[5] scores). Store each value returned from validCheck() into the array).
Call std::sort(std::begin(scores), std::end(scores)) to sort your scores ascending. Find the average (ignoring the max and min), and hold the max average as well as the names of the person with the max average.
#include <algorithm> // std::sort
...
double validCheck();
...
int main(){
string name;
string winner;
double win_avg;
double avg;
while (name != "done" || name != "Done"){
cout << "Enter Contestant Name, if no more, type 'done': ";
cin >> name;
double scores[5];
if (name == "done" || name == "Done"){ break; }
for (int judge = 0; judge < 5; ++judge){
cout << "Enter score " << judge << " ";
scores[judge] = validCheck();
}
std::sort(std::begin(scores), std::end(scores));
for(int score = 1; score < 4; ++score)
avg += scores[score];
avg /= 3;
if(avg > win_avg) {
winner = name;
win_avg = avg;
}
avg = 0;
}
std::cout << "Winner is: " << winner << "\n";
}
double validCheck(){
double score;
cin >> score;
if (score < 1 || score > 10){
cout << "Please Enter a score between 1 and 10: ";
cin >> score;
}
return score;
}
If you want to find the average in a function and return the value you can do this
double calcAvgCheck(const double& scores[5]) {
double avg = 0.0;
for(int score = 1; score < 4; ++score)
avg += scores[score];
avg /= 3;
return avg;
}