C空字符导致程序行为问题

时间:2014-06-25 19:42:38

标签: c arrays

我遇到的问题是这个程序是菜单驱动的。当我输入#34; i"在input数组中输入大小为MAX_LENGTH+1的数组。通过GDB我发现"我"在input数组的第0个索引中输入,其余空格输入NULL_CHAR个字符。无论如何,当我按下" i"对于插入菜单,我遇到了一个字段,告诉我输入一个值。我输入任何整数并按inter。而不是受到"命令的欢迎?:"字段,让我有机会输入一个条目,它立即告诉我,我的输入无效,并告诉我再次输入一个命令。这就是我的意思:

Commands are I (insert), D (delete), S (search by name),
  V (search by value), P (print), Q (quit).

Command?: i
45

Command?: 
Invalid command.
Commands are I (insert), D (delete), S (search by name),
  V (search by value), P (print), Q (quit).


Command?: 

我发现发生这种情况的原因是因为再次调用safegets函数时,由于某种原因,函数safegets中的局部变量c的值为NULL_CHAR,可能来自于事实上,输入字符数组中的所有其他值都包含所有其他条目NULL_CHAR。我不明白为什么c会自动分配NULL_CHAR的值,因为在while循环中,因为有一个语句c = getchar()应该再次询问我的输入。但由于某些原因,在每次输入后,c的默认值变为NULL_CHAR,并在下次调用safegets时要求您输入。

这是我想要的输出结果:

Commands are I (insert), D (delete), S (search by name), 
  V (search by value), P (print), Q (quit). 
 
Command?: I 
  value: 50000 
 
Command?: I
  value: 

这是主要功能:

const int MAX_LENGTH = 1023;
const char NULL_CHAR = '\0';
const char NEWLINE = '\n';


    int main (void)
    { 
        const char bannerString[]
            = "Personal Team Maintenance Program.\n\n";
        const char commandList[]
            = "Commands are I (insert), D (delete), S (search by name),\n"
              "  V (search by value), P (print), Q (quit).\n";

        // Declare linked list head.
        //   ADD STATEMENT(S) HERE TO DECLARE LINKED LIST HEAD.


        // announce start of program
        printf("%s",bannerString);
        printf("%s",commandList);

        char response;
        char input[MAX_LENGTH+1];
        int value;
        do
        {
            printf("\nCommand?: ");
            safegets(input,MAX_LENGTH+1);
            // Response is first char entered by user.
            // Convert to uppercase to simplify later comparisons.
            response = toupper(input[0]);

            if (response == 'I')
            {
                // Insert a player entry into the linked list.
                // Maintain the list in correct order (G, D, M, S).
                //   ADD STATEMENT(S) HERE

                // USE THE FOLLOWING PRINTF STATEMENTS WHEN PROMPTING FOR DATA:
                // printf("  family name: ");
                // printf("  first name: ");
                // printf("  position: ");
                   printf(" value: ");
                   scanf("%d", value);






            }
            else if (response == 'D')
            {
                // Delete a player from the list.

                printf("\nEnter family name for entry to delete: ");

                //   ADD STATEMENT(S) HERE

            }
            else if (response == 'S')
            {
                // Search for a player by family name.

                printf("\nEnter family name to search for: ");

                //   ADD STATEMENT(S) HERE

            }
            else if (response == 'V')
            {
                // Search for players that are worth less than or equal a value.

                printf("\nEnter value: ");

                //   ADD STATEMENT(S) HERE

            }
            else if (response == 'P')
            {
                // Print the team.

                //   ADD STATEMENT(S) HERE

            }
            else if (response == 'Q')
            {
                ; // do nothing, we'll catch this case below
            }
            else 
            {
                // do this if no command matched ...
                printf("\nInvalid command.\n%s\n",commandList);
            }
        } while (response != 'Q');

        // Delete the whole linked list that hold the team.
        //   ADD STATEMENT(S) HERE


        // Print the linked list to confirm deletion.
        //   ADD STATEMENT(S) HERE


        return 0;
    }

调用辅助函数:

void safegets (char s[], int arraySize)
{
    int i = 0, maxIndex = arraySize-1;
    char c;
    while (i < maxIndex && (c = getchar()) != NEWLINE)
    {
        s[i] = c;
        i = i + 1;
    }
    s[i] = NULL_CHAR;
}

3 个答案:

答案 0 :(得分:1)

你的代码很好。你需要改变

scanf("%d", value); 
//          ^Place & before the argument value because scanf expect pointer as its argument

scanf("%d", &value);

答案 1 :(得分:0)

如果修复ze代码

      else 
        {
            // do this if no command matched ...
            if (response != '\0')
              printf("\nInvalid command.\n%s\n",commandList);
        }

答案 2 :(得分:0)

我相信您的代码问题在于您在调用scanf后没有清空输入缓冲区(例如,如果输入&#39;我输入了&#39;值&# 39;读取。每次调用scanf后,你仍然会在输入缓冲区中有一个回车符和/或换行符。下一次调用safegets看到输入仍在输入缓冲区中并读取它要修复它,请在每次调用scanf后清空输入缓冲区:

char strip;

. . .

if (response == 'I')
{
    printf(" value: ");
    scanf("%d", value);

    /* empty the input buffer after scanf */
    do {
        strip = getchar();
    } while (strip != '\n');

    printf

}