[C++] Setting a default parameter based on another parameter?

Here's my function definition

1
2
bool validateNumber(string& text, int min = 0, int max = -1, bool useMin = true, 
                    bool getValid = true)


The code takes the string text, and checks the make sure that the input is valid and safe to convert and use as a number. However, sometimes there is not min, and sometimes there is no max. The lack of min is done by using the parameter useMin, while the lack of max is done by max < min.

My predicament is the following call:
validateNumber(text, -2);

Now, max will be used, even though I don't want it. Ideally, I would want to do something like
... int max = (min - 1), ... but that doesn't work. I also can't check to see if the parameter hasn't been changed (that I know of), because the following call would make it look like it hasn't
validateNumber(text, -2, -1);

So the question is, is there a way to do what I want, without having to add in a bool useMax parameter? Or is this my only option? I don't want to do that for simplicity, but if I have to, I have to.

Thanks.
My first instinct would be to use separate functions:

1
2
3
4
5
6
7
8
9
bool validateMinNumber( const string& text, int min )
{
    return validateNumber( /*...*/ );
}

bool validateMaxNumber( const string& text, int max )
{
    return validateNumber( /*...*/ );
}


Having too many default parameters makes the function a little confusing. Splitting it up into separate functions (or even just overloaded functions) keeps it a little more organized IMO.
You can set the default max to a very large negative number (preferably the largest negative integer). See http://www.cplusplus.com/reference/cstdint/ or http://www.cplusplus.com/reference/climits/
Disch, how would you break it up? I want to be able to validate a number, but I also want full control over the function, should I need it. Below is the full function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
bool validateNumber(string& text, int min = 0, int max = (min - 1), bool useMin = true, 
                    bool getValid = true,
                    string prompt = "Please enter a valid number: "){
    double number; // User double so all numbers are valid

    do{
        // If the first character is a digit, OR and . OR - AND the second character is a digit we can convert.
        // Otherwise we need to get valid input if getValid, or return false
        while(!isdigit(text[0]) && ((text[0] != '.' && text[0] != '-') && !isdigit(text[1]))){
            if(getValid){
                cout << prompt;
                getline(cin, text);
            }
            else{
                return false;
            }
        }

        // Convert to a number
        number = atof(text.c_str());

        // Now check to make sure the number is within the given range
        // If not, get valid input if getValid, else return false
        if((number < min && useMin) || (number > max && max > min)){
            if(getValid){
                cout << prompt << ": ";
                getline(cin, text);
            }
            else{ // If we're not getting a valid input while in here, return false
                return false;
            }
        }
        //Loop this as long as we're not within the given range.
    }while((number < min && useMin) || (number > max && max > min));

    cout << "\t" << number << " > " << min << endl;

    // If we get this far, we're good.
    return true;
}


Ats15, that's should work, thanks.
Topic archived. No new replies allowed.