brothers, please teach me how to show error message when a user input non-character type value?
i try myself while i use if statement, but it will show me the error message too when type character type or string type.

if(name > 0)
		{
			system("cls");
			printf("\t\t============\n");
			printf("\t\t    ALERT!  \n");
			printf("\t\t============\n");
			printf("\t  Please Enter In Alphabets!\n");
			printf(" \n");
			break;
		}

please solve it for me, thanks.

how is name declared? Assuming name is a character array you will want to look at each character and use isalpha() to determine whether or not the character is a-z or A-Z.

Edited 6 Years Ago by Ancient Dragon: n/a

some hints :

char  ch1 = 'a';
char ch2 = '#';
cout << isalpha( ch1 ) << endl; //true
cout << isalpha( ch2 ) << endl; //false

Edited 6 Years Ago by firstPerson: n/a

how is name declared? Assuming name is a character array you will want to look at each character and use isalpha() to determine whether or not the character is a-z or A-Z.

the name declared as string type.

Edited 6 Years Ago by gahhon: n/a

some hints :

char  ch1 = 'a';
char ch2 = '#';
cout << isalpha( ch1 ) << endl; //true
cout << isalpha( ch2 ) << endl; //false

can't to work with this, because user input is string type name (:

well then access the string just like you would the character array. Put this in a loop: if( !isalpha(name[i]) ) If name is std::string why are you not using std::cout instead of printf()?

Edited 6 Years Ago by Ancient Dragon: n/a

can't to work with this, because user input is string type name (:

string stringName = "goshy";
char charName = "goshy";

cout << stringName[0] << " " << stringName[1] << endl;
cout << charName[0] << " " << charName[1] << endl;

strings are made to replicate char arrays but be better

Comments
Are you sure this is a C syntax !!!

well then access the string just like you would the character array. Put this in a loop: if( !isalpha(name[i]) ) If name is std::string why are you not using std::cout instead of printf()?

becasue my lecturer is teaching the printf way.
no teach us std::cout way =x

string stringName = "goshy";
char charName = "goshy";

cout << stringName[0] << " " << stringName[1] << endl;
cout << charName[0] << " " << charName[1] << endl;

strings are made to replicate char arrays but be better

but the input is randomly, it wasn't fix by system one. :)

It doesn't matter how the string was generated. Take the example that firstPerson posted and modify it for use it in your program.

It doesn't matter how the string was generated. Take the example that firstPerson posted and modify it for use it in your program.

is still cannot solve,
even there is no error list to me, but i type single character also come out the error message.

i copy the party to let you see more easy.

char name[31]
		printf("Enter salesperson name : \a");
		fflush(stdin);
		scanf("%[^\n]s", &name);
		if( !isalpha(name[31]) )
		{
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
			system("cls");
			printf("\t\t=============\n");
			printf("\t\t    ALERT!   \n");
			printf("\t\t=============\n");
			printf("\t  Please Enter In Numeral!\n");
			printf(" \n");
			exit(1);
		}

Please solve it for me as fast possible thanks, because next week is deadline.
sorry for making trouble for you guys.:$

>>if( !isalpha(name[31]) )

That is wrong -- it is checking the 31st character in the array, not the first character. Replace 31 with 0

I suppose it should also be checking each character in the array, not just the first one. So you will want to make a loop to check them

int i;
int valid = 1;
for(i = 0; name[i] != '\0'; i++)
{
   if( !isalpha(name[i]) )
   {
     valid = 0;
     break;
   }
}
if( valid )
{
    // put all those print statements here
}

Edited 6 Years Ago by Ancient Dragon: n/a

>>if( !isalpha(name[31]) )

That is wrong -- it is checking the 31st character in the array, not the first character. Replace 31 with 0

I suppose it should also be checking each character in the array, not just the first one. So you will want to make a loop to check them

int i;
int valid = 1;
for(i = 0; name[i] != '\0'; i++)
{
   if( !isalpha(name[i]) )
   {
     valid = 0;
     break;
   }
}
if( valid )
{
    // put all those print statements here
}

oh..
i can solve the problem already.
but i put the name is one of the error dude.
so i do what you said just now, put it as 0.
so it can solve the problem already.
thanks you anyway.:)

This question has already been answered. Start a new discussion instead.