Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要函数调用方面的帮助吗 这是我第一年研究I.T.和C++,所以我对C++没有太多的了解。问题在于函数调用_C++ - Fatal编程技术网

需要函数调用方面的帮助吗 这是我第一年研究I.T.和C++,所以我对C++没有太多的了解。问题在于函数调用

需要函数调用方面的帮助吗 这是我第一年研究I.T.和C++,所以我对C++没有太多的了解。问题在于函数调用,c++,C++,函数标题为: bool insure(int age, bool smoker) 我的任务是写下正确的函数调用,并声明我将在调用语句中使用的所有变量 到目前为止,我的想法是: #include <iostream> using namespace std; bool insure(int age, bool smokers) { bool ret; } int main () { int age; bool isSmoker; bool ret;

函数标题为:

bool insure(int age, bool smoker)
我的任务是写下正确的函数调用,并声明我将在调用语句中使用的所有变量

到目前为止,我的想法是:

#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;
}
#包括
使用名称空间std;
bool保险(国际年龄,bool吸烟者)
{
布尔-雷特;
}
int main()
{
智力年龄;
布尔是烟民;
布尔-雷特;
年龄;
ret=保险(年龄、吸烟人数);
返回0;
}

我想知道这个程序是正确的还是我做错了什么。提前感谢

您没有从
保险
返回任何内容,而是将未初始化的变量(
isSmoker
)传递给它。您没有验证输入是否有效:如果用户输入“abc”作为其年龄,会发生什么情况。

我看不出它有什么根本性的错误,尽管:

  • 您不会从
    保险
    退回任何东西。这是故意的吗?也许您想添加
    返回ret

  • 您没有为任何变量定义起始值。这是一种不良形式,可能导致未定义的行为


  • 这项任务听起来像是你要把
    bool-insure(int-age,bool-smoker)
    作为一个黑盒子,并展示如何调用它。因此,我认为您需要自己回答的第一个问题是,您是否需要实际编写
    insure
    的定义,或者只是需要展示如何使用该函数。一旦你解决了这个问题

    • 需要多少和哪些类型的变量
    • 什么是
      保险
      退货,您如何储存
    • 如何设置/初始化用于调用和保存返回值的变量
    • 您应该执行何种类型的错误检查以确保将“良好”值传递给
      保险
      ?(您的课程是否涵盖了错误检查和错误处理?)
    #包括
    使用名称空间std;
    bool保险(国际年龄,bool吸烟者)
    {        
    如果(年龄>=25&&吸烟者==true)返回true;
    否则返回false;
    }
    int main()
    {        
    年龄=25岁;
    bool isSmoker=真;
    布尔-雷特;
    年龄;
    ret=保险(年龄、吸烟人数);
    
    coutGood yer提到错误处理。我认为他总是假设输入正确。当你运行它时发生了什么?
    #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;
    }