简体   繁体   English

在函数调用方面需要帮助

[英]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: 我没有发现任何根本上有问题的地方,尽管:

  1. You don't return anything from insure . 您不会从insure任何回报。 Is this by design? 这是设计使然吗? Maybe you want to add return ret ? 也许您想添加return ret

  2. 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, 解决之后,

  • How many and what types of variables does insure require? insure需要多少个变量和什么类型的变量?
  • What does insure return and how do you store it? insure收益是什么?如何存储?
  • How do you set up/initialize the variables you have to use for calling it and holding the value is returns? 如何设置/初始化用于调用它并保持返回值的变量?
  • What sorts of error checking should you do to make sure you're passing "good" values to insure ? 您应该执行哪种类型的错误检查,以确保传递“好”值来insure (Has your class covered error-checking and error-handling yet?) (您的课程是否已经涵盖了错误检查和错误处理?)

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

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