首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的输出结果在C++中是0。可能的问题是什么

我的输出结果在C++中是0。可能的问题是什么
EN

Stack Overflow用户
提问于 2020-05-13 01:27:29
回答 1查看 38关注 0票数 0
代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

void getInput(string name, float weekly_Pay){
    cout << "Please enter customer name" << endl;
    cin >> name;
    cout << "Enter weekly salary" << endl;
    cin >> weekly_Pay;
}

void calcFedTaxes(float weekly_Pay,float PIT_Rate, float SOSEC_Rate, float PIT, float SOSEC){
    PIT = weekly_Pay * PIT_Rate; 
    SOSEC = weekly_Pay * SOSEC_Rate;
}

void calcNetPay(float weekly_Pay, float PIT, float SOSEC, float weekly_Net_Pay){
    weekly_Net_Pay = weekly_Pay - (PIT + SOSEC);
}

void displayInfo(string name, float PIT, float SOSEC, float weekly_Net_Pay){
    cout << "Customer name is:" << name << endl;
    cout << "PIT is:" << PIT << endl;
    cout << "SOSEC is:" << SOSEC << endl;
    cout << "Weekly Pay is:" << weekly_Net_Pay << endl;
}

int main(){
    char response = 'n';
    string name =""; 
    float weekly_Pay = 0.0;
    float weekly_Net_Pay = 0.0;
    const float PIT_RATE = (0.2); 
    const float SOSEC_RATE = (0.08); 
    float SOSEC = (0.0);
    float PIT = (0.0);

    do {
        getInput(name, weekly_Pay);
        calcFedTaxes (weekly_Pay, PIT_RATE, SOSEC_RATE, PIT, SOSEC);
        calcNetPay (weekly_Pay, PIT, SOSEC, weekly_Net_Pay);
        displayInfo (name, PIT, SOSEC, weekly_Net_Pay);

        cout << "Enter n or N to end:";
        cin >> response;
        cout << endl;
    }
    while (!((response == 'n')  || (response == 'N')));
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-13 01:34:32

您的函数是按值获取输出参数的,因此它们对main()传递给它们的变量的副本起作用。函数对参数所做的任何更改都不会反映回main()

要完成您正在尝试的操作,您需要通过引用传递输出参数:

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

void getInput(string &name, float &weekly_Pay){
    cout << "Please enter customer name" << endl;
    cin >> name;
    cout << "Enter weekly salary" << endl;
    cin >> weekly_Pay;
}

void calcFedTaxes(float weekly_Pay, float PIT_Rate, float SOSEC_Rate, float &PIT, float &SOSEC){
    PIT = weekly_Pay * PIT_Rate; 
    SOSEC = weekly_Pay * SOSEC_Rate;
}

void calcNetPay(float weekly_Pay, float PIT, float SOSEC, float &weekly_Net_Pay){
    weekly_Net_Pay = weekly_Pay - (PIT + SOSEC);
}

void displayInfo(string name, float PIT, float SOSEC, float weekly_Net_Pay){
    cout << "Customer name is:" << name << endl;
    cout << "PIT is:" << PIT << endl;
    cout << "SOSEC is:" << SOSEC << endl;
    cout << "Weekly Pay is:" << weekly_Net_Pay << endl;
}

int main(){
    char response = 'n';
    string name =""; 
    float weekly_Pay = 0.0;
    float weekly_Net_Pay = 0.0;
    const float PIT_RATE = (0.2); 
    const float SOSEC_RATE = (0.08); 
    float SOSEC = (0.0);
    float PIT = (0.0);

    do {
        getInput(name, weekly_Pay);
        calcFedTaxes (weekly_Pay, PIT_RATE, SOSEC_RATE, PIT, SOSEC);
        calcNetPay (weekly_Pay, PIT, SOSEC, weekly_Net_Pay);
        displayInfo (name, PIT, SOSEC, weekly_Net_Pay);

        cout << "Enter n or N to end:";
        cin >> response;
        cout << endl;
    }
    while (!((response == 'n')  || (response == 'N')));
}

Live Demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61758100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档