[英]Need help with function calling
This is my first year studying IT and c++ so I don't have much of any knowledge in c++. 这是我学习IT和c ++的第一年,所以我对c ++的了解不多。 The question is about function calling.
问题是关于函数调用。
The Function Header is: 函数头是:
bool insure(int age, bool smoker)
My task is to write down the correct function call and declare all the variables that I will use in the calling statement. 我的任务是写下正确的函数调用,并声明将在调用语句中使用的所有变量。
So far, this is what I've come up with: 到目前为止,这是我想出的:
#include <iostream>
using namespace std;
bool insure(int age, bool smokers)
{
bool ret;
}
int main ()
{
int age;
bool isSmoker;
bool ret;
cout << "Enter your age: ";
cin >> age;
ret = insure(age, isSmoker);
return 0;
}
I'd like to know if this program is correct or if I'm doing something wrong. 我想知道这个程序是正确的还是做错了什么。 Thanks in advance
提前致谢
You're not returning anything from insure
, and you're passing it an uninitialized variable ( isSmoker
). 你不返回任何东西从
insure
,而你传递一个未初始化的变量( isSmoker
)。 And you're not verifying that the input worked: what happens if the user enters "abc" for his age. 而且您没有验证输入是否有效:如果用户输入年龄的“ abc”会发生什么。
#include <iostream>
using namespace std;
bool insure(int age, bool smokers)
{
if(age>=25&&smokers==true)return true;
else return false;
}
int main ()
{
int age=25;
bool isSmoker=true;
bool ret;
cout << "Enter your age: ";
cin >> age;
ret = insure(age, isSmoker);
cout<<"Insure:"<<ret;
return 0;
}
This code block is written assuming that insurance is true for people aged 25 yrs or more and smoker. 编写此代码块的前提是,年龄25岁或25岁以上且有烟民的保险适用。 You can change the logic inside the
insure(age,smokers)
function. 您可以更改
insure(age,smokers)
函数中的逻辑。
I don't see anything fundamentally too wrong with it, although: 我没有发现任何根本上有问题的地方,尽管:
You don't return anything from insure
. 您不会从
insure
任何回报。 Is this by design? 这是设计使然吗? Maybe you want to add
return ret
? 也许您想添加
return ret
?
You don't define starting values for any of your variables. 您没有为任何变量定义起始值。 This is bad form and may cause undefined behaviour.
这是错误的形式,可能会导致不确定的行为。
The task sounds like you are to take bool insure(int age, bool smoker)
as a black box and show how to call it. 任务听起来像您要将
bool insure(int age, bool smoker)
当作黑匣子,并说明如何调用它。 So I suppose the first question you need to answer for yourself is whether or not you are required to actually write a definition of insure
or just have to show how you would use the function. 因此,我想您需要为自己回答的第一个问题是,是否需要您实际编写
insure
的定义,还是只需要说明如何使用该函数。 Once you've resolved that, 解决之后,
insure
require? insure
需要多少个变量和什么类型的变量? insure
return and how do you store it? insure
收益是什么?如何存储? insure
? insure
? (Has your class covered error-checking and error-handling yet?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.