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

标签 c arrays

我遇到的问题是这个程序是菜单驱动的。当我输入字母“i”时,它会被输入到大小为 MAX_LENGTH+1 的 input 数组中。通过GDB我发现“i”是在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 code>,大概是因为输入字符数组中的所有其他值的所有其他条目均为 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;
}

最佳答案

你的代码没问题。你需要改变

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

scanf("%d", &value);

关于C 空字符导致程序行为出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24417200/

相关文章:

c - 这个程序中的ctime函数是如何工作的?

mtrace 可以用 valgrind 成功运行吗?

java - 帕斯卡三角递归程序不起作用

javascript - 如何断言两个数组不相交?

c - 显示所有有符号短号码的程序

c - 数组和点问题

c - float 相等性检查

java - 在二维数组中搜索对角线

java - 如何设置 JLabel 数组中元素的文本?

c - 在 c 中为数组赋值时出现意外结果