Validate

I'm having trouble validating this string function in this format "Jones Brokers" The string can be less 15 and there must be a space between Jones and Brokers.


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


string ValidateCompany()
{
    string Name;
   
    const int COMPANYLENGTH = 15;
   
    bool Check;
   
  

    do {
       
       cout << endl;
       cout << "Please enter the realitor company name (ex: Jones Brokers ) ";
       cin.ignore();
       getline(cin, Name);


       if ( Name.length() > COMPANYLENGTH ) {
            cout << endl;
            cout << "Error: You have reached max name length" << endl;
            Check = false;
        }
        else {
            Check  = true;
        }
      
       
   } while (Check == false);
 
   return Name;
}

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
#include <iostream>
#include <string>
#include <regex>

using namespace std;


int main()
{
	regex validName("[[:alpha:]]* [[:alpha:]]*");

	cout << "Please enter a first and last name " << flush;
	string name = "";
	getline(cin, name);

	if (name.size() < 15 && regex_match(name, validName))
	{
		cout << name << " is valid";
	}
	else
		cout << name << " is not valid";

	cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.