0

I need to write a program that checks if the user-provided first and last names are correctly typed. The program needs to validate that only the first letter of each name part is uppercase.

I managed to write code that checks the first character of the input. So I have a problem when for example "JOHN" is entered. A correct input would be for example "John Smith".

Here's the code:

#include <iostream>
#include <string>

using namespace std;

int main ()
{

  std::string str;
  cout << "Type First Name: ";
  cin >> str;

    if(isupper(str[0]))
    {
        cout << "Correct!" <<endl;
    }

    else
    {
        cout << "Incorrect!" <<endl;
    }


  system("pause");
  return 0;
}
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Then you need to check the rest of the letters if they are `std::islower`. – Kiril Kirov Jun 22 '15 at 14:56
  • 1
    Related reading: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Biffen Jun 22 '15 at 14:56
  • 1
    Do you understand the code you've written thus far? Do you know what a for-loop is? Just using a for-loop and applying some step-by-step thinking (i.e. think what you should do at every step) should get you pretty far. – Bernhard Barker Jun 22 '15 at 14:59
  • I am beginner at this, i searched google but could not find answer. – Gordon Freeman Jun 22 '15 at 15:15
  • 1
    This sounds like a homework? Implementing this test is good practice for a programming student, but I should caution you that in the real world, "correct" spelling of a name is often something very different from "First letter capitalized". So you should think about this as "complies with the given rule", not "correct". – Ben Voigt Jun 22 '15 at 15:59
  • Of course, remember the _McDonalds_, _O'Flynns_ and _van Rossums_ of the world. – moooeeeep Jun 22 '15 at 16:32

1 Answers1

0

The simplest thing you can do is to use a for/while loop. A loop will basically repeat the same instruction for a number of n steps or until a certain condition is matched.

The solution provided is pretty dummy, if you want to read the first name and last name at the same time you will have to spit the string via " " delimiter. You can achieve this result using strtok() in C/C++ or with the help of find in C++. You can see some examples of how to split here.

You can easily modify your code to look like this:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    std::string str;
    std::vector<std::string> data = { "First", "Last" };
    int j;

    for (int i = 0; i < 2; i++) {
        cout << "Type " << data[i] << " Name: ";
        cin >> str;

        if (isupper(str[0])) {

            for (j = 1; j < str.size(); j++) {
                if (!islower(str[j]))
                {
                    cout << "InCorrect!" << endl;
                    break; // Exit the loow
                }
            }
            if(j==str.size())
                cout << "Correct!" << endl;
        }
        else {
            cout << "InCorrect!" << endl;
        }
    }

    system("pause");
    return 0;
}
Community
  • 1
  • 1
A B
  • 497
  • 2
  • 9
  • I get this error when running this code http://prntscr.com/7jzh2u I use Visual C++ 2010 Express, doesn't matter it dodn't need to be together First and Last name saparated is also fine. – Gordon Freeman Jun 22 '15 at 15:57
  • It seams your compiler doesn't support C++ 11. Try to replace std::vector data = { "First", "Last" }; with this const char* data[] = { "First", "Last" }; – A B Jun 22 '15 at 16:02
  • It works but instead of showing up last name it show again type first name check it http://prntscr.com/7jznw9 – Gordon Freeman Jun 22 '15 at 16:12
  • A small typo from me. It should be data[i] instead of data[0], because using 0 every time only the first value will be used. Please let me know if it works now. :) – A B Jun 22 '15 at 16:18