While Loop not ending

Help please, i have this while loop and basically i want to check that the stuff in empoyeeID are as follows employeeID[0] = uppercase letter, employeeID[1] = uppercase letter. employeeID[2] = digit; employeeID[3] = digit. and sum of employeeID[2] and employeeID[2] is less than 10. The loop works if I employeeID contains something wrong but when it is correct, the loop never ends it keeps asking for more input. how can i break out of it if all my conditions are met?
#include <iostream>
#include <cctype>

using namespace std;


char *getEmployeeID()
{
char employeeID[5];
bool correct = false;

while (correct==false)
{
cout << "Enter ID: ";
cin.getline(employeeID, 5);
if (!isalpha(*employeeID))
cout << "invalid";

else if (!isalpha(*(employeeID + 1)))
cout << "invalid";

else if (!isupper(*employeeID))
cout << "invalid";

else if (!isupper(*(employeeID + 1)))
cout << "invalid";

else if (!isdigit(employeeID[2]) || !isdigit(employeeID[3]))
cout << "invalid";

else if (isdigit(employeeID[2]) && isdigit(employeeID[3]))
{

int num1 = employeeID[2] - '0';
int num2 = employeeID[3] - '0';

if ((num1 + num2) > 10)
cout << "invalid";
else
correct = true;

}

char *idPtr = employeeID;


return idPtr;

}

int main()
{
char *getID = getEmployeeID();

cout << "\n\nYour ID has been confirmed ";


return 0;


}
Use std::string. https://www.mochima.com/tutorials/strings.html

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
#include <iostream>
#include <string>
#include <cctype>

bool valid( std::string id ) // return true if id is a valid employee id
{
    return id.size() > 3 && // id has at least four characters and
           std::isupper( id[0] ) && std::isupper( id[1] ) && // id[0] and id[1] are uppercase letters and
           std::isdigit( id[2] ) && std::isdigit( id[3] ) && // id[2] and id[3] are decimal digits and
           ( ( id[2] - '0' ) + ( id[3] - '0' ) < 10 ) ; // sum of id[2] and id[3]  is less than 10
           // note: '3' - '0' == 3, '7' - '0' == 7 etc.
}

std::string get_id()
{
    std::string id ;
    std::cout << "enter employee id: " ;
    std::getline( std::cin, id ) ;

    if( valid(id) ) return id ;

    else
    {
        std::cout << "invalid employee id '" << id << "'\n" ;
        return get_id() ; // try again
    }
}

int main()
{
    const std::string id = get_id() ;
    std::cout << "your id '" << id << "' has been confirmed\n" ;
}
thank you helped a lot.
Topic archived. No new replies allowed.