Error Check

This is a function to validate a zipcode in this format #####-####. I'm having trouble Making Check == True without all the hard coded information at the bottom. Is there a way Simplify the hard code at the bottom of the 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
  string ValidateZip()
{
    const int ZipLength = 10;
    string Zip;
    bool Check = false;
   
    do {
        cout << endl;
        cout << "Enter the ZipCode of the listing ";
        cin >> Zip;
        if ( Zip.length() != ZipLength || Zip[5] != '-' ) {
            cout << endl;
            cout << "ERROR: Please enter the correct numeric format #####-####" << endl;
        }
        for (int i = 0; i <= 4; i++ ) {
            if ( !isdigit(Zip[i]) ) {
                cout << endl;
                cout << "ERROR: Please enter the correct numeric format #####-####" << endl;
            }
            
            
        }
        for (int j = 6; j < ZipLength; j++ ) {
                 
            if ( !isdigit(Zip[j]) ) {
                cout << endl;
                cout << "ERROR: Please enter the correct numeric format #####-####" << endl;
            }
        }
    if ( Zip.length() == ZipLength && Zip[5] == '-'
        && !isalpha(Zip[0]) && !isalpha(Zip[1])
        && !isalpha(Zip[2]) && !isalpha(Zip[3])
        && !isalpha(Zip[4]) && !isalpha(Zip[6])
        && !isalpha(Zip[7]) && !isalpha(Zip[8])
        && !isalpha(Zip[9]) ) {
            Check = true;
        }
    } while (Check == false);
    return Zip;
}
I would just set Check equal to true at the start of the do-while loop and then set it to false if any of those if statements are ran. Then delete all hardcoded stuff at the bottom.
Cool, got it to work.

thank you.
Topic archived. No new replies allowed.