0

I cannot figure out how to validate a string so that it accepts both uppercase and lowercase letters as in a name. Here is the code:

bool GetString(char buf[])
{
	int i;
	int length;
	bool valid;

	cin.getline(buf, 30);
	
	length = strlen(buf);

	for (i = 0; i < length; i++)
	{
		if ((buf[i] >= 'a' && buf[i] <= 'z') || (buf[i] >= 'A' && buf[i] <= 'Z') || (buf[i] >= 'a' && buf[i] <= 'Z') || (buf[i] >= 'A' && buf[i] <= 'z'))  
		{
			valid = true;
		}
		else
		{
			valid = false;
		}
	}

	return valid;
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by BBustos
0

use the isalpha() macro in ctype.h

bool GetString(char buf[])
{
	int i;
	int length;
	bool valid;

	cin.getline(buf, 30);
	
	length = strlen(buf);

	for (i = 0; i < length; i++)
	{
		if( !isalpha(buf[i]) )
		{
			return false;
		}
	}

	return true;
}
0

The problem that is beyond the scope of the class and I think they might not accept that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.