Validating if First name & Last Name is Typed Correctly

I need to write a program that checks if first name and last name are correctly typed in, the program need to checks if first letter of name is uppercase, it return "Correct", if it's not it returns "Incorrect" same for last name.

I managed to write code that does this but only for first name, and i have problem, when i write name for example JOHN it says "correct", same for John. The only correct thing would be ex John Smith.

I searched google a lot but could not find anything on this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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;
}
easiest thing would be to ask the user for their first name and perform your test (like you are doing), and then repeat the question but ask for the user's surname and perform the same test. i.e. wrap up the testing code into a function.
easiest thing would be to ask the user for their first name and perform your test (like you are doing), and then repeat the question but ask for the user's surname and perform the same test. i.e. wrap up the testing code into a function.


Yeah i can do that, but my main problem for now is that this code returns "correct" for JOHN and John when only correct thing would be John, this is just example for a name.
Then you need to check all characters in your string. That the first one is uppercase and all the rest are lower.
Use a for loop for that.
Last edited on
I know but how
i've done this in a very long-winded way so you can see what's going on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

using namespace std;

int main ()
{

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

	

	bool firstCharUpper(false);
	bool theOthersAllLower(true);

	for(int i = 0;i < str.size();++i)
	{	
		if(i==0)
		{
			if(isupper(str[i]))
			{
				firstCharUpper = true;
			}
		}
		else
		{
			if(isupper(str[i]))
			{
				theOthersAllLower = false;
				break;
			}
		}
	}

	bool allGood = firstCharUpper && theOthersAllLower;

	if(allGood)
	{
		cout << "Correct!" <<endl;
	}
	else
	{
		cout << "Incorrect!" <<endl;
	}
	
	return 0;
}


and then stick it in a function;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>

using namespace std;

bool ValidString(string str);

int main ()
{

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

	if(ValidString(str))
	{
		cout << "Correct!" <<endl;
	}
	else
	{
		cout << "Incorrect!" <<endl;
	}
	
	return 0;
}

bool ValidString(string str)
{
	bool firstCharUpper(false);
	bool theOthersAllLower(true);

	for(int i = 0;i < str.size();++i)
	{	
		if(i==0)
		{
			if(isupper(str[i]))
			{
				firstCharUpper = true;
			}
		}
		else
		{
			if(isupper(str[i]))
			{
				theOthersAllLower = false;
				break;
			}
		}
	}

	return (firstCharUpper && theOthersAllLower);
}
Last edited on
There seems to be a parallel thread on this topic: http://www.cplusplus.com/forum/beginner/167905/

It is more efficient (and polite) to keep discussion (about one topic) in one thread.
grrr i didnt see his second sneaky post :/
Topic archived. No new replies allowed.