Advice on validating input

This is a discussion on Advice on validating input within the C++ Programming forums, part of the General Programming Boards category; I need to validate input for name, address etc. This is how I'm gonna go about doing it. 1) name ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Advice on validating input

    I need to validate input for name, address etc. This is how I'm gonna go about doing it.

    1) name (broken down into first, middle, last) input has to be all letters.
    2) address line1 and 2: has to be digits, letters, or space
    I might want to elaborate that first portion is digits and the rest letters, space i.e 200 First Ave
    3) city, state: letters, space
    4) zip: 5 digits

    The inputs are gonna be string type and will be read like this.
    Code:
    void Person::setFirstName(const string fn) {
    int charValue;
    int strLength = fn.size();
    int count = 0;
    bool invalidChar = false;
    	
    do
    {
        charValue = (int)fn[count];	// reads the ASCII equivalence
    	
        if ((charValue < 65 || bla...bla..bla.. { // not a letter
            invalidChar = true;
        }
        count++;
    } while (count < strLength && invalidChar != true);
        
        if (invalidChar == false) {
            firstName = fn;
        }
        else {
            firstName = "XXXX";
        }
    }
    What do you guys think? any suggestion welcomed

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    look up c-style strings, for example, isdigit(),isalpha(),isalnum()

    http://www.cppreference.com/all_c.html


    //edit: better link:http://www.cppreference.com/stdstrin...s.html#isalpha
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Boost (www.boost.org) also has a regular expressions library... though that may be overkill.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM
  5. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 08:42 PM

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