注册 登录
编程论坛 C++教室

char 类型的数据输入时的验证问题

liliflying 发布于 2008-12-10 12:29, 612 次点击
定义结构体如下:
typedef struct student
{
    int num;
    char name[20];
    int age;
    float score[2];
}student;

typedef struct studentNode
{
    student data;
    studentNode* next;
}studentNode;

source如下:
void createList(studentNode *&head)
{
    int n, i = 1;
    studentNode* q;
    head = (studentNode*)malloc(sizeof(studentNode));
    head->next = NULL;
    studentNode* pnode = head;
    cout << "input the amount of the students: ";
    cin >> n;
    while (i <= n)
    {
        q = (studentNode*)malloc(sizeof(studentNode));
        pnode->next = q;
        pnode = q;
        cout << "input the " << i << "th student's information: " << endl;
        cout << "name: ";
        cin >> q->data.name;
        i++;
    }
    q->next = NULL;        
}
我是想在标下划线的地方输入name时,对他进行数据验证,要求只能由26个英文字母、下划线和数字组成,具体怎么个验证法不知道。请指教。
3 回复
#2
WiDark2008-12-10 12:39
提交时使用条件语句进行限制
#3
PcrazyC2008-12-10 12:41
写个函数不就得了,比如bool Check(char *name){//符合要求返回1,否则返回0
...}

cout << "name: ";
while(cin >> q->data.name){
if (Check(name)){
    break;
}
}
#4
liliflying2008-12-10 12:41
回复 第2楼 WiDark 的帖子
实在不知道怎么写,能在我的代码上改下吗?
1