我需要以下代码看起来像上面的输出,但是我很难得到这样的代码。由于某种原因,我认为费率变量并没有得到填充,我不知道为什么。此外,我很难获得奖金来计算和正确显示。
#include <iostream>
#include <iostream>
#include <iomanip>
#include "Chap1227.h"
using namespace std;
//function prototypes
double getBonusRate(double rate);
void columnsDashes();
void endPgm();
int main() {
double rate = 0.0;
//filling array
int salesArray[10][4] = {{2400, 3500, 2000, 2750},
{1500, 7000, 1000, 2225},
{2600, 2450, 2100, 2999},
{2790, 2240, 2500, 2885},
{2100, 2600, 2300, 3000},
{6300, 7000, 8000, 5550},
{3300, 1850, 2950, 2100},
{2700, 5500, 6000, 7200},
{4700, 4800, 4900, 5100},
{2375, 3300, 2550, 1925}};
getBonusRate(rate);
columnsDashes();
//bonus object
Bonus myBonus;
myBonus.calcAndDisplay(salesArray, rate);
endPgm();
return 0;
}
// ***** getBonusRate function description *****
//
double getBonusRate(double rate)
{
//display console title to user
cout << "JM SALES BONUS CALCULATOR\n\n";
//get bonus rate from user
cout << "Enter bonus rate in decimal form: ";
cin >> rate;
cout << "\n\n";
return rate;
}
// ***** columnsDashes function description *****
//
void columnsDashes()
{
//display column headings and dashes to user
cout << "Number Sales($) Bonus($)\n";
cout << "------ -------- --------\n";
}
// ***** endPgm function description *****
//
void endPgm()
{
//EOP message
cout << "\n\nEnd of Program.";
}
上面是cpp,下面是.h文件
#ifndef CHAP1227_H_
#define CHAP1227_H_
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// ***** CLASS DEFINITION *****
class Bonus
{
public:
Bonus();
void calcAndDisplay(int salesArray[10][4], double rate);
private:
int totSales;
int grandTotSales;
double salesBonus;
double bonusGrandTot;
};
// ***** CLASS IMPLEMENTATION *****
//default constructor
Bonus::Bonus()
{
totSales = 0;
grandTotSales = 0;
salesBonus = 0.0;
bonusGrandTot = 0.0;
}
//calcAndDisplay function
//
void Bonus::calcAndDisplay(int salesArray[10][4], double rate)
{
for (int row = 0; row < 10; row += 1)
{
cout << row << " ";
for (int column = 0; column < 4; column += 1)
{
totSales += salesArray[row][column];
salesBonus = totSales * rate;
bonusGrandTot += salesBonus;
}
cout << totSales << "\n";
grandTotSales += totSales;
totSales = 0;
}
cout << setprecision(2);
cout << salesBonus;
cout << grandTotSales << " " << bonusGrandTot;
}
#endif /* CHAP1227_H_ */
答案 0 :(得分:0)
确实,&#34;率&#34;没有被填充,因为你有多个变量称为&#34; rate&#34;。这就像有许多人命名为#Bob; Bob&#34; ...&#34; Bob Smith&#34;,&#34; Bob Jones&#34;,&#34; Bob Thompson&#34;
getBonusRate()获得了自己的&#34; rate&#34;所以&#34; getBonusRate :: rate&#34;并且主要功能有它自己的副本&#34; main :: rate&#34;。编译器会看到此全名,但您的代码只能看到短名称&#34; rate&#34;。
你的函数getBonusRate()有一个参数类型和一个返回类型。 它是一个门,一个门外。你通过率,但它没有出来。
要修复,您需要说出类似的内容:
outRate = getBonusRate(inRate);
inRate被复制到getBonusRate()函数中,它执行它的事情,然后将其返回,并将值赋给outRate。您可以重复使用/覆盖变量,因此您也可以这样做: inRate = getBonusRate(inRate);
答案 1 :(得分:0)
这里的问题是你正在通过&#39;率&#39;变量&#34;按值调用&#34;而不是&#34;通过引用调用&#34;。 要使其工作,您只需将getBonusRate函数重新定义为以下,
double getBonusRate(double& rate)
{
//display console title to user
cout << "JM SALES BONUS CALCULATOR\n\n";
//get bonus rate from user
cout << "Enter bonus rate in decimal form: ";
cin >> rate;
cout << "\n\n";
return rate;
}
请记住&#39;&amp;&#39;符号表示您通过参考传递了您的费率变量。
有关按值呼叫和按引用致电的详细信息,请查看以下内容,
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_value.htm https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm