为什么我的C ++程序不想运行? [英] Why does my C++ program not want to run?

查看:76
本文介绍了为什么我的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;
}

推荐答案

它确实可以运行.
您唯一需要做的就是在窗口关闭之前添加一些内容以便查看结果:尝试在return语句上放置一个断点,这将有助于测试,或者使用cin强制从键盘在应用程序关闭之前.

顺便说一句:我会更改CalcPay函数:
It does run.
The only thing you need to do is add something to let you see the result, before the window closes: Try putting a breakpoint on the return statement, that would help for testing, or use cin to force another read from the keyboard before the application closes.

BTW: I would change the CalcPay function:
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 ++程序不想运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆