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;
}

Recommended Answers

All 2 Replies

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;
}

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

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.