My question is how do I get it to display the bonus. program lanuage C++ program
ID: 3567269 • Letter: M
Question
My question is how do I get it to display the bonus.
program lanuage C++
program dev-c++
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
void enterItems(double salesAmt, double rate);
void calcAndDisplayBonus(double salesAmt, double rate);
int main()
{
//declare variables
double sales = 0.0;
double bonusRate = 0.0;
//enter input values
enterItems(sales, bonusRate);
//calculate and display bonus
cout << fixed << setprecision(2);
calcAndDisplayBonus(sales, bonusRate);
system("pause");
return 0;
} //end of main function
//*****function definitions*****
void enterItems(double salesAmt, double rate)
{
cout << "Enter sales: ";
cin >> salesAmt;
cout << "Enter bonus rate (in decimal form): ";
cin >> rate;
} //end of enterItems function
void calcAndDisplayBonus(double salesAmt, double rate)
{
cout << "Bonus: $" << salesAmt * rate << endl;
} //end of calcAndDisplayBonus function
Explanation / Answer
The problem is that when you pass the variables sales and rate into the enterItems function, they are being passed by value. This means the variables in the function are just copies of the ones outside the function in the main. The ones that you are passing into the bonus function are still valued at 0. So you are displaying the bonus. You have to find a way to pass the integers by reference into the enterItems function that way it changes the values. You could also get rid of the enterItems function all together and make the values in the main.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.