开发者

Need help with function calling

This is my first year studying I.T. and c++ so I don't have much of any knowledge in 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开发者_C百科 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). And you're not verifying that the input worked: what happens if the user enters "abc" for his age.


#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. You can change the logic inside the insure(age,smokers) function.


I don't see anything fundamentally too wrong with it, although:

  1. You don't return anything from insure. Is this by design? Maybe you want to add 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. 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. Once you've resolved that,

  • How many and what types of variables does insure require?
  • What does insure return and how do you store it?
  • 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? (Has your class covered error-checking and error-handling yet?)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜