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:
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.
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
//valid, try to verify that all other characters are lowercase now
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.
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.
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).
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.
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.