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.

Recommended Answers

All 13 Replies

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.

some hints :

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

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.

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 (:

maybe you can try to check then string length then check if its alpha or not.

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()?

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

commented: Are you sure this is a C syntax !!! +0

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
}

>>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.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.