C程序跳过switch语句中函数的一部分。

C程序跳过switch语句中函数的一部分。,c,function,switch-statement,C,Function,Switch Statement,我写了一个太长的程序,无法复制到这个网站。但是需要的东西会被粘贴到网站上 以下是switch语句: void enterName(); int adminChoice; printf("\nEnter Numeric Choice: "); scanf("%d", &adminChoice); switch(adminChoice) { case(1): { enterName()

我写了一个太长的程序,无法复制到这个网站。但是需要的东西会被粘贴到网站上

以下是switch语句:

    void enterName();

    int adminChoice;
    printf("\nEnter Numeric Choice: ");
    scanf("%d", &adminChoice);

    switch(adminChoice)
    {
        case(1):
        {
            enterName();
        }
    }
以下是实际功能:

void enterName()
{
    FILE *fp = fopen("/home/matthew/Desktop/BBE.txt", "w");

    if (fp == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }
    char comment[100];

    printf("Enter, String\n");

    fgets(comment, sizeof comment, stdin);
    fputs(comment,fp);
}

程序要求用户输入字符串。但不允许有时间输入所需字符串。节目就这样结束了

问题在于
scanf()
留下一个
\n
字符,该字符在不读取任何内容的情况下立即终止
fgets()
在看到换行符或EOF时将停止读取


您可以使用一种黑客方法并使用
getchar()就在调用前
fetgs()。更好的选择是使用
fgets()
读取
adminChoice
(并使用or函数将其转换为整数),以避免
scanf()
。在任何情况下,使用
fgets()

时都必须注意换行符。问题是
scanf()
会留下一个
\n
字符,该字符会在不读取任何内容的情况下立即终止
fgets()
在看到换行符或EOF时将停止读取


您可以使用一种黑客方法并使用
getchar()就在调用前
fetgs()。更好的选择是使用
fgets()
读取
adminChoice
(并使用or函数将其转换为整数),以避免
scanf()
。在任何情况下,在使用
fgets()

时都必须注意换行符。当您为
adminChoice
输入值时,请记住以换行符结尾(按
enter
键)。此键被添加到输入缓冲区中,以便在下次要读取内容时从
stdin
读取。现在想想当这个换行符是
stdin
输入缓冲区中的第一个字符时,调用
fgets
会发生什么。当您输入
adminChoice
的值时,请记住您以一个换行符(回车
键)结束它。此键被添加到输入缓冲区中,以便在下次要读取内容时从
stdin
读取。现在想想当这个换行符是
stdin
输入缓冲区中的第一个字符时,调用
fgets
会发生什么。