Error checking for capital letters, hyphens and apostrophes

This is a discussion on Error checking for capital letters, hyphens and apostrophes within the C++ Programming forums, part of the General Programming Boards category; I'm working on a piece of code that requires an error check of a string to be checked for capital ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Error checking for capital letters, hyphens and apostrophes

    I'm working on a piece of code that requires an error check of a string to be checked for capital letters, hyphens and apostrophes. Can anyone give me some guidance as to where to go next? I've tried a few other versions but it seems that to check for an apostrophe is a tricky task...


    Code:
    void errorcheck3(string& templast_name)
    {
        bool test; 
        string::size_type len; 
        int index = 0; 
       char testletter = templast_name[index];
        
        static_cast<unsigned int> (templast_name.length()); 
        len = templast_name.length(); 
         
        while (len > 15){ 
           cout << "Incorrect length of your lastname"; 
           cout << "Please re-enter it again  " << endl; 
           cin >> templast_name; 
        static_cast<unsigned int> (templast_name.length()); 
        len = templast_name.length();}   
            
        for(index = 0; index <= len; index++){ 
             
             test = isupper(templast_name[index]); 
                    
             
             while (test == 0){
                cout << "Incorrect character of your last name " << endl;
                cin >> templast_name;} 
                                        
                  }   
             
         }
    Thanks,
    Bill

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Checking for an apostrophe is actually quite easy, just compare the current letter to '\''.

    Your problem will be that you separate each check and then re-prompt if the check fails. This is not good design in the first place, since the original prompt came from somewhere else in the code, but beyond that you will have an issue if the first attempt has an uppercase letter and then the user enters a name that is too long the second time. The check for length will have already been passed.

    You need to re-work your function to check for all three issues first, and if any of them are bad, then re-prompt and check all again. The best way to do that IMO is to leave the re-prompting to the calling code.

    A couple other notes- your loop runs from index = 0 to index <= len. That means that you might use an index of len on the string, but that is passed the bounds of the string and could lead to a crash or just an incorrect result. Use < instead of <=. Also, the static_cast<unsigned int> (templast_name.length()); lines don't do anything and aren't needed. You have already fixed your warning by using string::size_type as the type for len.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Thanks, I'll re-work my function tonight..

Popular pages Recent additions subscribe to a feed

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