lundi 20 avril 2015

How to test a char array for letters only (including whitespaces)?

I'm making a function for basic information input. This function will be later used to get information and store on disk. I've made two checks. is_alpha and is_digit. The issue with is_alpha is, it return "0" if it detects white space(which is not what i want). I'm taking input for "Name" and obviously it can contain spaces! Can you please tell me how to make a method which checks my char array if it is a name? (Alphabets and white spaces)

class Bankaccount
{
protected:
int id;
char name[50];
char address[100];
char phone_no[50];
static int count;

public:
Bankaccount()
{

    count++;
    id = count;
}



bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
bool is_alpha(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
{cin.ignore(); return !s.empty() && it == s.end();}
}

void basics ()
    {system("cls");
    cout << "Enter Name (Letters only): " << endl;
    cin.ignore();
    cin.getline(name,50);


    {cout << "Enter Address: " << endl;
            cin.ignore();
cin.getline(address,100);
    cout << "Enter Phone Number (Digits only): " << endl;
    cin.ignore();
    cin.getline(phone_no,50);
    //CHECK FOR DIGITS ONLY
    bool temp=0;
    temp=is_number(phone_no);
    {while(temp!=1)
        {cout << "ReEnter Phone Number: " << endl;
        cin.ignore();
        cin.getline(phone_no,50);
        temp=is_number(phone_no);
        }
    }//while ends

    }



    {cout << "ReEnter Name: " << endl;
    cin.ignore();
    cin.getline(phone_no,50);

    cout << "Enter Address: " << endl;
            cin.ignore();
cin.getline(address,100);
    cout << "Enter Phone Number (Digits only): " << endl;
    cin.ignore();
    cin.getline(phone_no,50);
    //CHECK FOR DIGITS ONLY
    bool temp;
    temp=is_number(phone_no);
    if (temp==1)
    {}
    else {cout << "ReEnter Phone Number: " << endl;
    cin.ignore();
    cin.getline(phone_no,50);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire