do
{
for(i=0;i<(strlen(temp));i++)
{
if(!isdigit(temp[i]))
{
cout <<"\nis not a digit";
system("pause") ;
check = false ;
break ;
}
}
if(strlen(temp) != 7 || check == false)
{
system("cls") ;
cout <<"The telephone number must be 7 digit exactly" ;
cout <<"\nPlease key in telephone number again: " ;
cin.getline(temp,80,'\n') ;
}
}while(strlen(temp) != 7 && check == false) ;
strcpy(TelephoneNumber,temp) ;
The telephone number is only supposed to accept 7 digit only. But when i key in test data eg.123456a, it accepts too.. What went wrong in my programme?
char EmployeeType;
cout <<"\nPlease enter the Employee Type: ";
cin >> EmployeeType;
cin.ignore(20,'\n');
//you want assignment here, i.e. '=', not '=='
EmployeeType = toupper(EmployeeType);
//'||' should be '&&' and you don't have to use numbers
while (EmployeeType != 'S' && EmployeeType != 'H')
{
system("cls") ;
cout <<"The Employee Type must be either 'S' or 'H'";
cout <<"\nPlease key in the Employee Type again: ";
cin >> EmployeeType;
//it's good to add this here too
cin.ignore(20,'\n');
//you also want assignment here.
EmployeeType = toupper(EmployeeType) ;
}
EDIT:
I want you to notice that here you need '&&' while in the other example you need '||' because here you want the loop to stop when the char is either 'S' or 'H' (i.e. at least one of these conditions is true), while in the previous example the loop stops when it's strlen(temp)==7 and each character is a digit (i.e. both conditions are true)