Validating data!!

this function should validate the data.
its a word so it should only have char.
I cannot find a way to do it!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  int word_validation (char word_playerA[],int x, int i)
{
	int valid_word;
	//0=true & 1=false
	//its not working
	for (i=0; i < x; i++)
	{
		if (word_playerA[i] >= 'a' && word_playerA[i] <= 'z')
		{
			valid_word = 0;
		}
		else
		 valid_word = 1;
	} 	
	printf("%d.\n", valid_word );//testing
	
	if (valid_word != 0)
	{
		printf("Invalid Character.\n");
		printf("Enter only letters.\n");
	}
	
	if (x < 3)
	{ 
		printf("Word has less than 3 letters.\n"); 
		printf("Enter a valid length word.\n"); 
		valid_word = 1;
	}		
	
	return(valid_word);
}
Last edited on
If you're looping through all the characters in the word in your for loop, valid_word will have the result of only the last character. It gets overwritten every time the loop iterates. I think you'd want to stop checking once you find an invalid character (or you reach the end of the word without finding an invalid character).

If this function returns true/false, you could make it a bool function and define valid_word as a bool type. (Keeping in mind with bool that 0 is false, 1 is true, which is the opposite of what you have defined now).

You should lookup isalpha and isdigit, they come in handy for situations like this.

http://www.cplusplus.com/reference/cctype/isalpha/
http://www.cplusplus.com/reference/cctype/isdigit/


Thanks wildblue, but im stuck in this loop...

thanks softrix,
this tool is amazing, good to know
but i need to use a for-loop,

Thanks all for looking
3
4
5
6
7
8
9
10
  bool valid_word = true;  //  Assume valid until determined otherwise

  for (i=0; i<x; i++)
  {  if (! isalpha(word_playerA[i])) 
      {   valid_word = false;
           break;  // no need to check further
       }
  }


BTW, why are you passing i as an argument? i should be a local variable.

Hi, AbstractionAnon

That looks flash!! but i cannot use it since we didn't learn it.
It need to be a loop similar to mine, that is the reason its harder to put your head around that way. I think im almost there,
I need to find a way to hold the value of valid_word.
Or do a bool type, but im not sure how to do it in C. I cant use C++

I was bringing i to the function to make the loop, but i've changed to a local variable, it makes more sense..

thanks soo much for ur help
Why can I use this??
saying if the data isnt a letter valid_word=o

1
2
3
4
5
if (word_playerA[i] < 'a' && word_playerA[i] > 'z')
{
	valid_word = 0;
}
else


Thanks
Ive got ....uhuuuuuuu....
Thanks guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int word_validation (char word_playerA[],int x)
{
	int valid_word=0;
	int i, j;
		
	if (word_playerA[0] >= 'a' && word_playerA[0] <= 'z')
	{
		valid_word = 0;
	}
		
	for (i=0; i < x; i++)
	{
		if (word_playerA[i] >= 'a' && word_playerA[i] <= 'z')
		{
			j = 0;
		}
		else
			j = 1;
		
		if( valid_word < j)
		valid_word = 1;
	} 


wildblue, I knew what u were talking about and finally it clicked.
Thanks mate!!

PLS let me know if there is a better way!!! cheers
Last edited on
Lines 6-9 serve no purpose. You initialize valid_word to 0. You then check the first character for a valid letter and sets valid_word to 0. This changes nothing.

I'm really unsure what you're trying to do in lines 13-21. It looks like you're trying to set j to 0 if a character is valid. Then you compare that flag to valid_word.

Your use of 0 and for j and valid_word is unconventional. Usually 0 = false and 1 = true. You're using those values the other way around, which is confusing.

A simple variation of what I posted earlier should work in C.
1
2
3
4
5
6
  int valid_word = 1;  //  Assume valid until determined otherwise

  for (i=0; i<x; i++)
  {  if (word_playerA[i] < 'a' || word_playerA[i] > 'z') 
          valid_word = 0;  // found invalid character
  } 











It makes sense and I all ready tried to do like this way before, but doesnt work!! I have not idea why...

cheers
You need to post what you have now.
Topic archived. No new replies allowed.