#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')));
}发布于 2020-05-13 01:34:32
您的函数是按值获取输出参数的,因此它们对main()传递给它们的变量的副本起作用。函数对参数所做的任何更改都不会反映回main()。
要完成您正在尝试的操作,您需要通过引用传递输出参数:
#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')));
}https://stackoverflow.com/questions/61758100
复制相似问题