简体   繁体   English

我的 output 结果在 C++ 中为 0。 可能是什么问题

[英]My output results are 0 in C++. What could be the issue

#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'))); }

Your functions are taking their output parameters by value , so they are acting on copies of the variables that main() passes to them.您的函数通过 value获取其 output 参数,因此它们作用于main()传递给它们的变量的副本 Any changes the functions make to the parameters are not reflected back to main() .函数对参数所做的任何更改都不会反映回main()

To do what you are attempting, you need to pass output parameters by reference instead:要执行您正在尝试的操作,您需要通过引用传递 output 参数:

#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现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM