Basic C++ question <searching char arrays>

This is a discussion on Basic C++ question <searching char arrays> within the C++ Programming forums, part of the General Programming Boards category; okay, what would the best way to check to make sure that all characters inputted by a user (which is ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    26

    Basic C++ question <searching char arrays>

    okay, what would the best way to check to make sure that all characters inputted by a user (which is stored as a char array) only consist of a-z, A-Z, and 0-9??


    I was thinking a for loop for obvious reasons, but I can't seem to get the logic down to make it work.


    This is the last part of my program I need to get to work and it's driving me nuts! heh

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Post your loop in [code][/code] tags and somebody may be able to figure out where your logic is off.

    Another way might be to use an algorithm combined with isalpha and isdigit, but since you are using C style strings I doubt you'd be allowed to use C++ algorithms.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Post some code, or at least explain your logic.

    A for loop sounds good, where is the detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    I forgot to add . and @ to the list of chars since it IS an e-mail addy.


    umm, I haven't formulated a for loop yet, I'm just trying to write down some pseudo-code, and I can't get the logic to work.

    the only thing I've come up with is if-else statements within the for loop, somethin like

    for(int a=0; a<length; a++)
    {
    if(email[a] < 'a' || email[a] > 'z')
    {
    ....


    But, as you can probably tell, doing it for lower case, upper case, then numbers isn't going to work...

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    At the very basic, you would need to nest the loops. One to check for the char ( letter ) and
    one to check the numbers ( int )

    But post some code, give it a try yourself and if you are still stumped, peole will be able to help you

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No need to nest the loops, although you might need to nest the ifs (not necessarily though).

    Your pseudo code looks fine. If you can use isalpha that would make your job a little easier. Otherwise, if you know functions, making separate functions that check for lowercase, uppercase, digits and the two extra characters might be a good way to go to reduce repeated code and make things clearer. In fact, just doing that in your pseudo-code could help.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    okay, this is what I come up with, but I still can't seem to get it to work right...


    I came up with the bounds by looking at an ascii value key...

    Code:
    	for(int g = 0; g < length; g++)
    	{
    		if(email[g] > '.' && email[g] < 'z')
    		{
    			if(email[g] == '/')
    			{
    				return 0;
    			}
    
    			else if(email[g] > '9' && email[g] < '@')
    			{
    				return 0;
    			}
    
    			else if(email[g] > 'Z' && email[g] < 'a')
    			{
    				return 0;
    			}
    		}
    
    		else
    		{
    			return 0;
    		}
    	}

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I still can't seem to get it to work right.

    What's going wrong with it? The logic there looks ok. Where's the rest of the code?

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    A way to avoid all those if statements is to create another char array (preferably a std::vector or std::string) with all the characters that are allowed. It's then a simple matter of traversing this array for each character in your string.

    If it is found, you break from the loop and continue to the next character in your string. If it is not found, then I think your search is over. The email is not valid. You set some flag variable to false indicating the email is not valid and break of the loops.

    If you are allowed to use the STL, then it all becomes much easier. With std::string and member functions like find_first_not_of()
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    Quote Originally Posted by Daved
    >> I still can't seem to get it to work right.

    What's going wrong with it? The logic there looks ok. Where's the rest of the code?

    It makes everything come up invalid...

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It makes everything come up invalid...

    The logic there looks ok. Where's the rest of the code? What input do you give it?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    nevermind, I got it working.

    Thanks, guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21