如何在C[而不是C+;+;]中多次扫描包含多个单词的字符串?

如何在C[而不是C+;+;]中多次扫描包含多个单词的字符串?,c,string,fgets,gets,C,String,Fgets,Gets,我在谷歌上搜索了很多次,但我找不到我的问题的具体答案。 我知道fgets()允许它,get()也允许它。但是如果我做了多次,总会有错误。多次表示,例如,我想询问他的名字,然后是中间名,然后是姓氏,可以是一个或多个字符串 我想我是通过使用gets得到的,因为我无法使fgets()工作,但我的程序中有一些bug。所以,我想寻求一个比我所做的更好的解决方案。无论如何,下面是部分代码: BST *insert(BST *root, BST *temp){ if(root == NULL) roo

我在谷歌上搜索了很多次,但我找不到我的问题的具体答案。 我知道fgets()允许它,get()也允许它。但是如果我做了多次,总会有错误。多次表示,例如,我想询问他的名字,然后是中间名,然后是姓氏,可以是一个或多个字符串

我想我是通过使用gets得到的,因为我无法使fgets()工作,但我的程序中有一些bug。所以,我想寻求一个比我所做的更好的解决方案。无论如何,下面是部分代码:

BST *insert(BST *root, BST *temp){
    if(root == NULL) root = temp; //empty
    else{
        temp->parent = root;
        //if true i = 1, else i = 0
        int i = root->employee.emnumber <= temp->employee.emnumber;
        //recurse
        root->child[i] = insert(root->child[i], temp);
    }
    return root;
}

int checknum(BST *root, int num){
    if(root == NULL){
        if(num <= 99999 && num >= 1) return 0;
        else return 1;
    }
    if(root->employee.emnumber == num || num > 99999 || num < 1) return 1;

    int i = root->employee.emnumber <= num;
    checknum(root->child[i], num);
}

BST *add(BST *root){
    int invalid = -1;
    BST *temp = malloc (sizeof (BST) );
    temp->child[0] = temp->child[1] = temp->parent = NULL;

    printf("\n\tADD EMPLOYEE INFORMATION\n");
    printf("\nEnter Employee Number: ");
    scanf("%d", &(temp->employee.emnumber) );
    invalid = checknum(root,temp->employee.emnumber);
    if(invalid) printf("\nInvalid Input!\n(Either your input is not a 5-digit number or the employee number is already in the database)\n\n");
    else{
        printf("\nFull Name| First Name: ");
        getchar();
        gets(temp->employee.emname.fn);
        //printf("%d\n", strlen(temp->employee.emname.fn));
        if(strlen(temp->employee.emname.fn) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
        else{
            printf("           Middle Name: ");
            gets(temp->employee.emname.mn);
            if(strlen(temp->employee.emname.mn) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
            else{
                printf("           Last Name: ");
                gets(temp->employee.emname.ln);
                if(strlen(temp->employee.emname.ln) > max) printf("Invalid Input!\n(Input only %d characters)\n", max);
                else{
                    printf("\nBirth Date| Month: ");
                    scanf("%d", &(temp->employee.bdate.month) );
                    printf("            Day: ");
                    scanf("%d", &(temp->employee.bdate.day) );
                    printf("            Year: ");
                    scanf("%d", &(temp->employee.bdate.year) );
                    printf("\nCurrent Address: ");
                    scanf("%s", temp->employee.address);
                    printf("\nMonthly Gross Salary: ");
                    scanf("%d", &(temp->employee.salary) );
                    printf("\nHire Date| Month: ");
                    scanf("%d", &(temp->employee.hired.month) );
                    printf("           Day: ");
                    scanf("%d", &(temp->employee.hired.day) );
                    printf("           Year: ");
                    scanf("%d", &(temp->employee.hired.year) );
                    root = insert(root, temp);
                }
            }
        }
    }
}
BST*插入(BST*根,BST*临时){
如果(root==NULL)root=temp;//为空
否则{
temp->parent=root;
//如果为真,则i=1,否则i=0
int i=root->employee.emnumber employee.emnumber;
//重现
根->子[i]=插入(根->子[i],临时);
}
返回根;
}
int checknum(BST*根,int num){
if(root==NULL){
如果(num=1)返回0;
否则返回1;
}
如果(root->employee.emnumber==num | | num>9999 | | num<1)返回1;
int i=root->employee.emnumber子项[i],num);
}
BST*添加(BST*根){
int无效=-1;
BST*temp=malloc(sizeof(BST));
temp->child[0]=temp->child[1]=temp->parent=NULL;
printf(“\n\tADD员工信息\n”);
printf(“\n输入员工编号:”);
scanf(“%d”和(temp->employee.emnumber));
无效=checknum(root,temp->employee.emnumber);
if(无效)printf(“\n无效输入!\n(您的输入不是5位数字或员工编号已在数据库中)\n\n”);
否则{
printf(“\n全名|名:”);
getchar();
获取(temp->employee.emname.fn);
//printf(“%d\n”,strlen(temp->employee.emname.fn));
如果(strlen(temp->employee.emname.fn)>max)printf(“无效输入!\n(仅输入%d个字符)\n”),max;
否则{
printf(“中间名:”);
获取(temp->employee.emname.mn);
如果(strlen(temp->employee.emname.mn)>max)printf(“无效输入!\n(仅输入%d个字符)\n”),max;
否则{
printf(“姓氏:”);
获取(temp->employee.emname.ln);
如果(strlen(temp->employee.emname.ln)>max)printf(“无效输入!\n(仅输入%d个字符)\n”),max;
否则{
printf(“\n出生日期|月份:”);
scanf(“%d”和(temp->employee.bdate.month));
printf(“日:”);
scanf(“%d”和(temp->employee.bdate.day));
printf(“年份:”);
scanf(“%d”和(temp->employee.bdate.year));
printf(“\n当前地址:”);
scanf(“%s”,临时->员工地址);
printf(“\n月工资总额:”);
scanf(“%d”和(临时->员工工资));
printf(“\n点火日期|月份:”);
scanf(“%d”和(临时->员工雇佣月));
printf(“日:”);
scanf(“%d”和(临时->员工雇佣日));
printf(“年份:”);
scanf(“%d”和(临时->员工雇佣年份));
根=插入(根,温度);
}
}
}
}
}
错误在于,如果我在add中输入了一个无效的数字。假设我在employee number中输入了5,但是已经有了employee number 5,程序会说已经有employee 5,但是如果我打印了employee list,employee 5就不存在了。在“添加”中再次输入5将不会提示您,因为它删除了员工5。我不知道如何删除它,因为我没有调用delete函数。 请有人帮帮我

编辑: 我删除了checknum,并在我的搜索功能中搜索删除: 我的搜索代码是:

BST *search(BST *root, int x){
    int i;
    if(root == NULL || root->employee.emnumber == x) return(root);
    i = root->employee.emnumber <= x;
    return search(root->child[i], x);
}
BST*搜索(BST*根,int x){
int i;
if(root==NULL | | root->employee.emnumber==x)返回(root);
i=root->employee.emnumber子项[i],x);
}
现在是:

BST *add(BST *root){
    BST *invalid = NULL;
    BST *temp = malloc (sizeof (BST) );
    temp->child[0] = temp->child[1] = temp->parent = NULL;

    printf("\n\tADD EMPLOYEE INFORMATION\n");
    printf("\nEnter Employee Number: ");
    scanf("%d", &(temp->employee.emnumber) );
    if(temp->employee.emnumber > 99999 || temp->employee.emnumber < 1)
        printf("\nInvalid Input!\n(Input only 5-digit number)\n\n");
    else{
        invalid = search(root,temp->employee.emnumber);
        if(invalid != NULL) printf("\nInvalid Input!\n(Already in the database)\n\n");
        else{
           /*The same codes here*/
        }
    }
}
BST*添加(BST*根){
BST*无效=空;
BST*temp=malloc(sizeof(BST));
temp->child[0]=temp->child[1]=temp->parent=NULL;
printf(“\n\tADD员工信息\n”);
printf(“\n输入员工编号:”);
scanf(“%d”和(temp->employee.emnumber));
如果(temp->employee.emnumber>99999 | temp->employee.emnumber<1)
printf(“\n无效输入!\n(仅输入5位数字)\n\n”);
否则{
无效=搜索(root,temp->employee.emnumber);
如果(无效!=NULL)printf(“\n无效输入!\n(已在数据库中)\n\n”);
否则{
/*这里的代码相同*/
}
}
}
我认为现在的问题在于它遇到“无效输入”时返回到根目录的内容 我通过root=add(root)调用add(),因此,如果遇到问题,它将指向null或其他内容。 那你觉得我该怎么办

编辑[上次]:
问题真正在于add()遇到“无效输入”时返回什么。我将BST*add更改为void add以避免root=add(root),然后对函数进行了必要的更改,现在它可以工作了。谢谢你的回答,但我不能打勾,因为我认为没有人答对。我现在使用fgets()。

让我们从以下内容开始:如果需要通过检查子对象来递归,那么从
checknum()
返回什么


或者,等效地说,您是使用
-Wall
编译的,还是编译器激活所有警告的等效工具?有哪些警告?

您在
checknum()函数的末尾缺少一个
返回:

  int i = root->employee.emnumber <= num;
  return checknum(root->child[i], num);        //Added a return here
}
int i=root->employee.emnumber子项[i],num)//在这里添加了一个返回
}
如果没有
返回
checknum()
的递归调用没有效果,最终返回值可能不是什么