name consists of letters spaces, can contain 1 single quotation mark and 0ne hyphen mark

hi everyone im looking for some help on a program it is to "input persons name using only letters & spaces and can contain only 1 single quotation mark and only one single hyphen. if it has a illegal character the user will be asked to renter there name", ive spent about 8 hours at it so far and I know it would be easy for programmers with more experience but I am still only in the learing process. below is the direction I am going at the moment im unsure about using ascii as I have only been shown it for 30 mins in class ive also tried using getline to no avail

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
 using namespace std;
#include <conio.h>
#include <iostream>
#include <string>

int main(){

char name;
bool valid = true;

cout<<"Please enter your name: ";
cin>>name;

if (!isalpha(name))
   valid=false;
else if (!(name>='a'||name<='z'||name==' '||name=='-'))
    valid=false;

else if (name==' '*2||name=='-'*2)
    valid=false;


if (valid)
cout<<name<<" is a valid name";

else cout<<"sorry that is a incorrect name";

cout<<"Please enter your name: ";
cin>>name;

getch();
return 0;
}
I declare name of string rather than char to use the function length.

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
bool valid = true;
int counter = 0;
int numQuot = 0;	// number of quotation
int numHyp = 0;		// number of hyphen
do
{
	if	(!isalpha(name[counter]))
		switch (name[counter])
		{
			case ' ':	// do nothing when it is a space
				break;
				
			case '\'':
				numQuot += 1;
				break;
				
			case '-':
				numHyp += 1;
				break;
				
			// not alphabetic, space, quotation or hyphen
			default:
				valid = false;
				break;
		}
		
	if	((numQuot > 1) || (numHyp > 1))
		valid = false;
	counter++;
}	while	( (valid) && (counter < name.length()) );


And use getline(cin, name) for input.
Last edited on
Thanks very much for your help nikko ive been trying the code and it keeps asking me to enter name over and over, never sais its right or wrong and accepts all numbers here is what ive came up with so far
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
53
54
55
56
using namespace std;
#include <conio.h>
#include <iostream>
#include <string>

int main(){

string name;

int counter = 0;
int numQuot = 0;	// number of space
int numHyp = 0;		// number of hyphen
bool valid = true;

do
{
cout<<"Please enter your name: ";
getline(cin, name, '\n');

	if	(!isalpha(name[counter]))
		switch (name[counter])
		{
			case ' ':	// do nothing when it is a space
				break;

			case '\'':
				numQuot += 1;
				break;

			case '-':
				numHyp += 1;
				break;

			// not alphabetic, space, quotation or hyphen
			default:
				valid = false;
				break;
		}

	if	((numQuot > 1) || (numHyp > 1))
		valid = false;
	counter++;


}	while	( (valid) && (counter < name.length()) );

{cout<<name<<" is a valid name";}

if (!(valid));
cout<<"Please enter your name: ";
getline(cin, name, '\n');


getch();
return 0;
}

Last edited on
can anyone else shine some light on this please, still not getting anywhere.
Last edited on
Inputting name should be done before do...while.
What the loop is doing is to check the validity of the name, but not the inputting process.

I see you intend to prompt for input if it is invalid.
But your method does not check the re-inputted name.

Tips to do this:
You can consider my above code as a function, having prototype bool isValid(string), and return valid.
Last edited on
Thanks again nikko will give it a go there now.
Topic archived. No new replies allowed.