Thread: noob problem in code

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    noob problem in code

    hey i wanted to do this character validator i tried running into for loop then
    chking if each character are == but i dunno whats wrong
    Code:
    #include <stdio.h>
    int main(void)
    {
    	char stuff[]="ASDFGHJKL;'/.,mnbvcxzqwertyuiop[]=-09038837351523535355,znbzvvvgjkkld";
    	char validator[]="asdfghjklmnbvcxzqwertyuiop";
    	int i;
    	int x;
    	for(i=0,x=0;stuff[i]!='\0',validator[x]!='\0';i++,x++)
    	{
    		if(stuff[i]==validator[x])
    			printf("%c",stuff[i]);
    			
    	}
    	return 0;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Can we assume that "asdfghjklmnbvcxzqwertyuiop" are are to be considered 'valid' characters? If so, try a nested loop - I think that is what you are after. Basically you want to test the first character in stuff ('A') agains every character in validator. Then you want to increment stuff[i], and check the next character ('S') against every character in validator, and so on. The outer loop would increment the array index for stuff, and the inner loop would run through each character in validator.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    but i alrdy did it in for loop shoudlnt this increment it ? and how can i increment a character in a for loop?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You increment the index to the array, which "moves" down to the next letter/character.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    how can i increment it inside the loop ? so i can chk each character == to other in the increment?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if int variable i is set to zero, what is stuff[i]?

    What is stuff[++i]?

    Awwwww!

    You'll have two for loops, with i and j as counters.

    Code:
    for(i = 0; i < MaxStuffSize; i++)  {
    
       for(j = 0; j < MaxValidatorSize; j++)  {
    
          if(stuff[i] == validator[j])
             //code to handle that
    
       }
    }
    You'll probably want to use strlen(stuff) to find the max length of stuff, (and same for validator). You'll need to probably include <string.h> for strlen.
    Last edited by Adak; 02-06-2009 at 09:07 PM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    oh thanks i ill try

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    I did this
    Code:
    #include <stdio.h>
    int main(void)
    {
    	char stuff[]="ASDFGHJKL;'/.,mnbvcxzqwertyuiop[]=-09038837351523535355,znbzvvvgjkkld";
    	char validator[]="asdfghjklmnbvcxzqwertyuiop";
    	int i;
    	int x;
    	for(i=0;i<=sizeof stuff;i++)
    	{
    		for(x=0;x<sizeof validator;x++)
    		{
    			if(stuff[i]==validator[x])
    			{
    				stuff[++i]=validator[++x];
    				printf("%c",stuff[i]);
    			}
    		}
    	}
    	return 0;
    }
    didnt rlly work out accurate i think
    dunno where is the problem

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Take out the 'stuff[++i]=validator[++x];'

    With that taken out, I get "mnbvcxzqwertyuiopznbzvvvgjkkld" as the output.

    Not 100% sure if that's what you wanted, but I think so.

    Btw, if you compare an uppercase character to a lowercase character it will return 0, because they are not equal. So if you were expecting the first 9 characters to be in there they won't be.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take out the ++i and ++x. I and x are incremented, through their respective for loops.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah thanks alot it works now
    Code:
    #include <stdio.h>
    int main(void)
    {
    	char stuff[]="ASDFGHJKL;'/.,mnbvcxzqwertyuiop[]=-09038837351523535355,znbzvvvgjkkld";
    	char validator[]="asdfghjklmnbvcxzqwertyuiop";
    	int i;
    	int x;
    	for(i=0;i<=sizeof stuff;i++)
    	{
    		for(x=0;x<sizeof validator;x++)
    		{
    			if(stuff[i]==validator[x])
    			{
    				printf("%c",stuff[i]);
    			}
    		}
    	}
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    thanks alot guys and lolguy i understand it now

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, lolguy's code is slightly incorrect: the outer loop should loop from i = 0 to i < sizeof stuff, not i <= sizeof stuff. Note that the string literal includes the terminating null character, which in turn is included in the sizeof stuff and sizeof validator value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM