Validation : What went wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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?
bumpz someone help me ~~ ty
This is where the problem is located -> while(strlen(temp) != 7 && check == false) ;

It should be like this -> while(strlen(temp) != 7 || check == false) ;

Perhaps you'll also have to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do
{
    check=true; //<-add this here!

    for(i=0;i<(strlen(temp));i++) 
    {
        //...
    }

    if(strlen(temp) != 7 || check == false)
    {
        //...
    }

}while(strlen(temp) != 7 || check == false) ;

Last edited on
Thanks, ur suggestion work wonders..
1 more question ...

How do I check a char?

For example i want only EmployeeType to be either S or T....

1
2
3
4
5
6
7
8
9
10
11
12
13
char EmployeeType ;
cout <<"\nPlease enter the Employee Type: " ;
cin >> EmployeeType ;
cin.ignore(20,'\n') ;
EmployeeType == toupper(EmployeeType) ;
while (EmployeeType != 83|| EmployeeType != 72) 
{
                system("cls") ;
	cout <<"The Employee Type must be either 'S' or 'H'" ;
	cout <<"\nPlease key in the Employee Type again: " ;
	cin >> EmployeeType ;
	EmployeeType == toupper(EmployeeType) ;
}


strcmp doesn't seems to work too

with regards
DoomCarnage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

Also notice that:
(strlen(temp) != 7 || check == false) == !(strlen(temp) == 7 && check == true)

and
(EmployeeType != 'S' && EmployeeType != 'H') == !(EmployeeType == 'S' || EmployeeType == 'H')

(these are called De Morgan rules)
Last edited on
Topic archived. No new replies allowed.