My code requires the validation of a string that starts with two letters and ends with six numbers. So, an example would be br474897. This is what I got so far:

bool GetNID(char buf[])
{
	int i= 0;
	int length;
	bool valid = false;

	cin.getline(buf, 30);

	length = strlen(buf);

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

	return valid;
}

Thanks in advance for your help.

>>if (buf < 0 || buf > 9)
Wrong -- you need to check for '0' and '9' (in quotes).

Your program has a major logic flaw. First assume the string is valid, then stop looking if any of the characters are not valid. The way you have it the value of valid will flip back and borth between true and false. The final value after the loop terminates in undeterminable -- It could be either true or false depending on the last character in the string.

Yeah, I just noticed it. Any ideas on what I should do? Maybe use a 'While' loop?

Ancient Dragon just just told you. Set valid = true before the loop. In the loop, if you find a bad character, set valid = false. Never set valid = true inside the loop. This way, when the loop is finished, valid will be false if _any_ bad characters were found.

If would be even simpler not to use the valid variable:

bool validateFn(string st){
   while(!done){
      if(test next character in st == BAD CHAR){
         return false;
         }
      }
   return true;
   }

A while loop won't help that.

bool GetNID(char buf[])
{
	int i= 0;
	int length;
	bool valid = true;

	cin.getline(buf, 30);

	length = strlen(buf);

	for (i = 0; i < length && valie == true; i++)
	{
		if (i < 2)
		{
			if ((buf[i] < 'a') || (buf[i] > 'z'))
			{
				valid = false;
			}
		}
		if (i > 1)
		{
			if (buf[i] < '0' || buf[i] > '9')
			{
				valid = false;
			}
		}
	}

	return valid;
}

you don't need valid

bool GetNID(char buf[]) {
	cin.getline(buf, 30);
	int length= strlen(buf);

	for (int i = 0; i < length; i++) {
		if (i < 2) {
			if ((buf[i] < 'a') || (buf[i] > 'z')) {
				return false;
			}
		}
		if (i > 1) {
			if (buf[i] < '0' || buf[i] > '9') {
				return false;
			}
		}
	}

	return true;
}

you don't need valid

Depends on his teacher. Many teachers require only one return in a function. Otherwise I would also do it your way. Using valid is always correct.

forced to use only one return in a function- lol that's new for me :]

There are many companies who have that rule as a coding standard. One way in and only one way out of a function.

forced to use only one return in a function- lol that's new for me :]

Come to my class sometime! I teach that generally, the loop condition controls when the loop ends, exiting prematurely should be for exceptions. As AD said, one way into the function, one way out - that usually makes for simpler to understand code, easier debugging.

I don't get anal about this, my example search functions typically return immediately upon finding the target, and some other problems might also.

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.