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
usingnamespace 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;
elseif (!(name>='a'||name<='z'||name==' '||name=='-'))
valid=false;
elseif (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;
}
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()) );
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
usingnamespace 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;
}