I need help with input validation using overloaded functions

closed account (EAp4z8AR)
For some reason I can not get the while loop in the last function to end when it is true.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <numeric>

using namespace std;

double inputVal(string min, double max);
double inputVal(double min, string max);
double inputVal(double min, double max);

int main ()
{
    string minimum;
    double maximum=32;
    
    inputVal(minimum, maximum);
    
}

double inputVal(string min, double max)
{
    double input;
    cout << "Please enter a number (min: no limit, max: 32): ";
    cin >> input;
    
    while (input<=max)
    {
        inputVal(32.0, 212.0);
    }
    while (input>max)
    {
        cout << "Invalid input, try agian!\nPlease enter a number (min: no limit, max: 32): ";
        cin >> input;
        while (input<=max)
        {
            inputVal(32.0, 212.0);
        }
    }
}

double inputVal(double min, double max)
{
    string maximum;
    double input;
    cout << "Please enter a number (min: 32, max: 212): ";
    cin >> input;
    
    while (input>=min && input<=max)
    {
        inputVal(212.0, maximum);
    }
    while (input<min || input>max)
    {
        cout << "Please enter a number (min: 32, max: 212): ";
        cin >> input;
        while (input>=min && input<=max)
        {
            inputVal(212.0, maximum);
        }
    }
}

double inputVal(double min, string max)
{
    string maximum;
    double input;
    cout << "Please enter a number (min: 212, max: no limit): ";
    cin >> input;
    
    while (input>=min)
    {
        break;
    }
    
    while (input<min)
    {
        cout << "Invalid input, try agian!\nPlease enter a number (min: 212, max: no limit): ";
        cin >> input;
    }
}
Last edited on
Hello vmansuria,

Sorry I did not see this sooner.

You not only have a problem with the last function, but all three. All three functions say they return a double, but not one function returns anything. Or did you mean to make them "void" functions? A "void" return seems more appropriate.

Your third function receives a "double" and a "string" in that order, but back in "main" you define "minimum" as a string with no value and "maximum" as a double, so your function is sending a "string" and a "double" in the order thus calling the first function.

When I switched the variable definitions it called the third function.

In the third function "min" should have a valid minimum value so that when "min" is used in the function it is used correctly. This way "input" has something to compare to. When you send the correct information to the third function it works the way it should.

Generally using the same variable name in more then one function is not a problem, but here it is hard to follow. First your prompt in the third function Please enter a number (min: 212, max: no limit): . My first impression is that I will be entering two numbers, but only one is entered. The prompt should reflect what you are looking for and about to enter. Then the use of "input" really does not tell me which number will be entered. "minInput" is a name with a better description of what you are entering.

The opposite is true for the first function.

If you have the wrong value for "input" the second while will be entered until you enter a correct number. Then the condition will fail and the function will end. But as set up it should return something, I am thinking "input".

From what I can tell the second function is working properly.

For the first function the prompts should better reflect what number you need and "input" would look better as "maxInput".

Hope that helps,

Andy
Topic archived. No new replies allowed.