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>
#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" ;
}