input validation does not work

Doing input validation for the input of "hiGal" and the inputValidation_Hi() no longer works after receiving a error: 'h' is not a type problem(a rogue h character was in the program.)

after that my do-while loop for inputValidation_Hi stopped working. I am not sure if I deleted something or if it was wrong the entire time.

how do I fix this?

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

/*Global Function Declarations*/
void inputValidation_Low();
void inputValidation_Hi();
void errors();
/*****************************/


double lowGal, hiGal; //Lowest Gallon



/*for validation loops function*/
bool quit = false; // return value
bool error;       //condition
char charVal;    //for q termination
/****************************/


int main()
{
    cout << "This program creates a gallons to liters conversion table.\n\n";
    inputValidation_Low ();
    inputValidation_Hi ();
    errors();
 }
void inputValidation_Low()
{
    do
    {
        cout << "Enter the lowest gallon value to display <q to quit>: ";
        cin >> lowGal;
        cout << endl;

        if (cin.fail())
        {
            cin.clear();
            cin >> charVal; //if q is entered
            if(charVal == 'q')
            {
                quit = true;
                error = false;
            }else{
                cout << "You entered an invalid character: (" << charVal << ")\n";
                cout << endl;
            }
        }else{
            quit = error = true;
        }

    }while (!error);
}
void inputValidation_Hi()
{
    do
    {
        cout << "Enter the highest gallon value to display <q to quit>: ";
        cin >> hiGal;
        cout << endl;

        if (cin.fail())
        {
            cin.clear();
            cin >> charVal; //if q is entered
            if(charVal == 'q')
            {
                quit = true;
                error = false;
            }else{
                cout << "You entered an invalid character: (" << charVal << ")\n";
                cout << endl;
            }
        }else{
            quit = error = true;
        }

    }while (!error);
}

void errors()
{
    if (!quit)
    {
        if (lowGal < 0)
        {
            cout << "Error: low gallon value must not be negative.\n";
            error = true;
        }
        if (hiGal < 0)
        {
            cout << "Error: high gallon balue must not be negative.\n";
            error = true;
        }
        if (hiGal < lowGal)
        {
            cout << "Error: high gallon value must be larger than or equal to the low gallon value.\n";
        }
        if (hiGal - lowGal > 1000)
        {
            cout << "total gallon range must be less than or equal to 1000.\n";
        }
        if (error ==true)
        {
            cout << "Please re-enter low and high gallon values correctly.\n\n\n";
        }

    }else{
        error = true;
    }
}


  
Last edited on
Topic archived. No new replies allowed.