如何获得一个函数来重新枚举Main()中的变量?

时间:2017-11-18 20:52:38

标签: c++ visual-studio

在输入总收入后,我试图做一个带薪税计算器的东西。我使用函数分别计算每个税,然后在我的Main()中我想从总总额中减去所有税以获得净工资。我的问题是我无法弄清楚如何从函数中获取总数以在main()中减去它。

  #include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

double federalTax(double userInput, double total1) {
    double total = total1;
    total = userInput * 0.2;
    cout << "Federal Tax: " << setw(12) << "-$" << total << endl;
    total = total1;
    return 0;
}
double stateTax(double userInput, double total2) {
    double total = total2;
    total = userInput * 0.04;
    cout << "State Tax: " << setw(15) << "-$" << total << endl;
    total = total2;
    return 0;
}
double Medicare(double userInput, double total3) {
    double total = total3;
    total = userInput * 0.0275;
    cout << "Medicare: " << setw(16) << "-$" << total << endl;
    total = total3;
    return 0;
}
double Pension(double userInput, double total4) {
    double total;
    total = userInput * 0.06;
    cout << "Pension: " << setw(17) << "-$" << total << endl;
    total4 = total;
    return 0;
}
int main()
{
    double userInput;
    double total1 = 0, total2 = 0, total3 = 0, total4 = 0;
    double sum;

    cout << "What is your gross income?" << endl;
    cin >> userInput;
    cout << "Gross Income: " << setw(10) << "$" << userInput << endl;

    federalTax(userInput, total1);
    stateTax(userInput, total2);
    Medicare(userInput, total3);
    Pension(userInput, total4);
    cout << "Health Insurance: " << setw(10) << "-$80" << endl;

    sum = userInput - total1 - total2 - total3 - total4 - 80;

    cout << "Net Pay: " << setw(15) << "$" << sum << endl;
    system("pause");
    return 0;
}

当我尝试减去它时(你可以用我的double = sum的声明看到它),它只是取了我初始化的总数1到4的0。

3 个答案:

答案 0 :(得分:1)

在这种情况下,您必须通过引用传递值。默认情况下,C ++不接受引用的值。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

double federalTax(double userInput, double &total1) {
    double total = total1;
    total = userInput * 0.2;
    cout << "Federal Tax: " << setw(12) << "-$" << total << endl;
    total = total1;
    return 0;
}
double stateTax(double userInput, double &total2) {
    double total = total2;
    total = userInput * 0.04;
    cout << "State Tax: " << setw(15) << "-$" << total << endl;
    total = total2;
    return 0;
}
double Medicare(double userInput, double &total3) {
    double total = total3;
    total = userInput * 0.0275;
    cout << "Medicare: " << setw(16) << "-$" << total << endl;
    total = total3;
    return 0;
}
double Pension(double userInput, double &total4) {
    double total;
    total = userInput * 0.06;
    cout << "Pension: " << setw(17) << "-$" << total << endl;
    total4 = total;
    return 0;
}
int main()
{
    double userInput;
    double total1 = 0, total2 = 0, total3 = 0, total4 = 0;
    double sum;

    cout << "What is your gross income?" << endl;
    cin >> userInput;
    cout << "Gross Income: " << setw(10) << "$" << userInput << endl;

    federalTax(userInput, total1);
    stateTax(userInput, total2);
    Medicare(userInput, total3);
    Pension(userInput, total4);
    cout << "Health Insurance: " << setw(10) << "-$80" << endl;

    sum = userInput - total1 - total2 - total3 - total4 - 80;

    cout << "Net Pay: " << setw(15) << "$" << sum << endl;
    system("pause");
    return 0;
}

答案 1 :(得分:0)

您的问题是value而不是reference。因此,将复制通过值传递的变量,以便对函数中那些复制值的任何更改都不会影响main或其中的原始变量。因此,更改total4 = total不会触及主total4。相反,尝试从函数返回。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

double federalTax(double userInput) {
    double total = 0;
    total = userInput * 0.2;
    cout << "Federal Tax: " << setw(12) << "-$" << total << endl;
    return total;
}
double stateTax(double userInput) {
    double total = 0;
    total = userInput * 0.04;
    cout << "State Tax: " << setw(15) << "-$" << total << endl;
    return total;
}
double Medicare(double userInput) {
    double total = 0;
    total = userInput * 0.0275;
    cout << "Medicare: " << setw(16) << "-$" << total << endl;
    return total;
}
double Pension(double userInput) {
    double total;
    total = userInput * 0.06;
    cout << "Pension: " << setw(17) << "-$" << total << endl;
    return total;
}
int main()
{
    double userInput;
    double total1 = 0, total2 = 0, total3 = 0, total4 = 0;
    double sum;

    cout << "What is your gross income?" << endl;
    cin >> userInput;
    cout << "Gross Income: " << setw(10) << "$" << userInput << endl;

    total1 = federalTax(userInput);
    total2 = stateTax(userInput);
    total3 = Medicare(userInput);
    total4 = Pension(userInput);
    cout << "Health Insurance: " << setw(10) << "-$80" << endl;

    sum = userInput - total1 - total2 - total3 - total4 - 80;

    cout << "Net Pay: " << setw(15) << "$" << sum << endl;
    system("pause");
    return 0;
}

答案 2 :(得分:0)

在创建原始参数的新副本时传递值问题。与通过引用或指针传递相反,这意味着传递原始变量的地址;这意味着任何变化都会影响原始变更。因为没有创建副本。经验法则尽可能通过参考。

您可以在程序中将其更改为合理:

1-只要您对返回的值不感兴趣,将函数更改为返回void

2-通过引用传递。

3-删除所有函数中的局部变量total_x以影响原始函数。

其中一个功能如下:

void federalTax(double &userInput, double& total1) {
    total1 = userInput * 0.2;
    cout << "Federal Tax: " << setw(12) << "-$" << total1 << endl;
}
  • 对其余功能执行相同的操作。