本文介绍了为什么我的C ++程序不想运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您能否向我解释为什么这个C ++程序不想运行.我直接从教科书中复制了此内容:

Hi,

Can you please explain to me why this C++ program does not want to run. I copied this straight from a text book:

//**************************************************************
//Paycheck program
//This program computes an employee's wages for the week
//**************************************************************

#include <iostream>
using namespace std;

void CalcPay(float, float, float&);

const float max_hours = 40.0; //Maximum normal work hours 
const float overtime = 1.5; //overtime rate factor

int main()
{
float payrate; //emp pay rare
float hours; // hours worked
float wages; // wages earned
int empnum; //emp id no

cout <<"Enter emp no";
cin>>empnum;
cout <<"Enter pay rate";
cin>>payrate;
cout<<"Enter hours worked";
cin>>hours;

CalcPay(payrate, hours, wages);

cout <<"Emp no"<<empnum<<endl<<"pay rate"<<payrate<<endl<<"wages"<<wages<<endl;

return 0;
}
//**************************************************************

void CalcPay(float payrate, float hours, float& wages )
{
if (hours > max_hours)
wages = (max_hours * payrate) + (hours-max_hours) *payrate *overtime;
else
wages= hours * payrate;
}

推荐答案

float CalcPay(float payrate, float hours)
   {
   float wages;
   if (hours > max_hours)
      wages = (max_hours * payrate) + (hours-max_hours) *payrate *overtime;
   else
      wages= hours * payrate;
   return wages;
   }

这样,它返回值,而不是修改参数.这意味着您可以在计算中使用它:

This way it returns the value, instead of modifying a parameter. That means you can use it in calculations:

float allPay = CalcPay(JoesRate, JoesHours) + CalcPay(MikesRate, MikesHours);


这篇关于为什么我的C ++程序不想运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 08:10