Accepting Hyphens in String

Hello,

I'm a student just beginning to learn C++. My current task is to write code which will accept a user's first, middle, and last names, as well as various other bits of info which do not pertain to my current question.

For the first name, the requirement is that the user's input will only be accepted when it's at least 2 and not more than 10 characters, the first letter must be capitalized, the only symbol accepted must be a hyphen and if they input a hyphen then the very next character must be a capital letter as well. Every other character must be lowercase. I'm having trouble knowing how exactly to accept hyphens and no other symbols, as well as only accepting 1 capital after the hyphen.

I made a function which will accept the first name string and return a true/false value depending on the user's input. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool fName(std::string firstName)
{
     bool success;
     if(firstName.length() >= 2 && firstName.length() <= 10 && isupper(firstName[0]))
     {
          success = true;
     }
     else
     {
          success = false;
     }

     return success;
}


I am stuck as to how to complete the rest of my requirements for the first name. Maybe a for loop that will count its way through the other characters to check if they're lowercase? However, this method wouldn't be useful for accepting the hyphen + uppercase case.

Any help is much appreciated!
Last edited on
1
2
3
4
5
6
7
if firstName[K] == '-' and is_uppercase(firstName[K+1])
   //valid, next to check K += 2
else if is_lowercase(firstName[K])
   //valid, next to check K += 1
else
   //invalid
   return false
The C library has several character handling routines that could used for exclude non-alphabetic characters in your string, as well as determining if the character is a '-' or not.

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

<cctype> also has functions to convert letters to upper or lower case.

After you have the input you want with a possible '-' std::string::find could be used to locate the character for making the next available letter upper-case (if it exists!)

http://www.cplusplus.com/reference/string/string/find/
Do I need to use the .find command to find where/if K is a hyphen? For example:

After the first if statement could I have something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int K = firstName.find("-");
if(firstName[K] < firstName.length())       //because firstName[K] will produce huge gibberish number if there is no hyphen
{
     if(isupper(firstName[K+1]))
     {
          //valid, try to verify that all other characters are lowercase now
     }
     else
     {
          //invalid
     }
}
else        //will run if there is no hyphen
{
     //loop to verify that all characters except for the first are lowercase
} 


Just shooting in the dark, thanks for your patience.
Don't bother checking if the letter after a '-' is upper-case, just make it upper-case.

Same for the first letter in the name.

The steps I would take to get a valid name by your criteria:

1. get a string from the user.

2. check the string is > 2 and < 11 characters long. If not, input error. Inform the user and get the name string again.

3. check if there any non-alphabetic characters in the string. If there are check if the non-letters are NOT '-'. If those checks are true, input error.

4. convert the entire string to lower-case. (Trying to convert a '-' won't cause an error.)

5. convert the first letter to upper-case.

6. Find the '-'. Check if there is a letter following. If not, input error. If there is a letter convert the letter to upper case.
Hmm, I'm pretty sure that I need to actually verify if the user provides the information in this specific manner. I would 100% agree with you that your method would work if I was going to store the information in an external file or something, however, all my program needs to do is get the information in a very specific way, i.e. if the user inputs "Mark-Tony" it's acceptable but if they enter "Mark-tony" then they need to try again. Not the most useful of programs, I'd say, but it's what I need to come up with.
Then no need for std::string::find.

Modify steps 4 - 6:

4. check the first character is upper case. If not, input error.

5. walk-through (loop) the string, starting at the 2nd character.

6. check each character is either a lower-case letter or a '-'.

6a. if a letter that is not lower-case. input error. If is a lower-case letter, increment the loop by 1 (using continue will skip any later code you may have written)

6b. if the character is a '-' check the next character exists and is upper case. If that fails, input error. If the next character after the '-' is upper-case, increment the loop by 2. (continue and loop variable +1).

https://en.cppreference.com/w/cpp/language/continue

I could just write the code I've described above, but you will learn more if you try to implement it first. Getting compile/run-time errors, and trying to solve them, is a very good learning experience.
Thank you, I've figured it all out.

You've helped me realize that when I'm met with things like this I should take a minute to lay it all out in front of me without the code which I'm sure will be even more valuable as I continue to learn.
Topic archived. No new replies allowed.