函数意外输出

时间:2015-06-07 20:24:28

标签: c linux

我正在编写一个函数,用于从给定的字符串中找到字符

Test.c文件如下:

#include<stdio.h>

int findIndex(char *array, char search)
{
    int count=0;
    char test;
    int check = 0, repeat;

    if(search == ',')
        repeat = rand()%10;

    else
        repeat = rand()%11;

    while((test = array[count]) != NULL)
    {
        if(test == search)
        {

            if(check == repeat)
                return count;

          check++;
        }

        count++;
    }

}

int main()
{
    char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
    char testchar;  

    printf("Enter the search character: ");
    testchar = getc(stdin);

    printf("The search char found at: %d position.\n",findIndex(sc,testchar));

    fflush(stdin);

    while(testchar != 'N')
    {
        printf("The search char found at: %d posotion.\n",findIndex(sc,testchar));
        printf("Enter the search character: ");
        scanf("%c",&testchar);
                fflush(stdin);
    }        

    return 0;
}

预期的输出只是一次字符索引,但我得到了这个: -

[amarjeet@amarjeet ~]$ ./a.out 
Enter the search character: Y
The search char found at: 66 position.
The search char found at: 128 position.
Enter the search character: The search char found at: 0 position.
Enter the search character:

我哪里出错了? 请让我知道问题是什么以及如何解决。

2 个答案:

答案 0 :(得分:1)

您可能希望main函数看起来像这样:

int main()
{
    char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
    char testchar;

    printf("Enter the search character: ");
    scanf("%c",&testchar);
    while(testchar != '\n' && getchar() != '\n'); # discard any next chars

    while(testchar != 'N') {
        printf("The search char found at: %d position.\n",findIndex(sc,testchar));
        printf("Enter the search character: ");
        scanf("%c",&testchar);
        while(testchar != '\n' && getchar() != '\n'); # discard any next chars
    }
    return 0;
}

以上示例生成结果:

$ > ./a.out
Enter the search character: Y
The search char found at: 66 position.
Enter the search character: .
The search char found at: 0 position.
Enter the search character: ,
The search char found at: 83 position.
Enter the search character: g
The search char found at: 0 position.
Enter the search character: N
$ >

PS:输入流没有ISO定义fflush()。它在Microsoft的C运行时库中定义,但不可移植。

答案 1 :(得分:1)

您的代码中有一个必须更正的警告

EditText

以上行生成警告:指针与整数之间的比较。因此应更正为

while((test = array[count]) != NULL)

在你的输出中你得到两次字符索引,因为换行符也被拍摄并且它的位置也被打印(当你按下回车键时)。

为避免这种情况,你可以这样做

while((test = array[count])!='\0')

这就是你现在的程序。

注意:使用fflush(stdin)被认为是一种不好的做法并且具有未定义的行为。最好不要将它用于刷新输入流,因为它不可移植。请参阅更多信息Do not use fflush(stdin)

<强>输出

while(testchar != 'N')
{
       if(testchar!='\n'){//checking whether character is newline
        printf("The search char %d found at: %d posotion.\n",testchar,findIndex(sc,testchar));
        printf("Enter the search character: ");
        scanf("%c",&testchar);

        }
        else
        {
             scanf("%c",&testchar);//reading the newline character but not performing any operation(printing the index )
        }

}        

希望它有所帮助。快乐编码!!