Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我试图在c中结束一个事件控制循环时,它需要两个输入?_C - Fatal编程技术网

当我试图在c中结束一个事件控制循环时,它需要两个输入?

当我试图在c中结束一个事件控制循环时,它需要两个输入?,c,C,当我运行它并尝试结束循环时,我必须输入-1两次。 有人知道为什么吗? 我是一个初学者,所以我真的不知道代码出了什么问题。 我需要知道出了什么问题,因为编译器没有告诉我这个问题 我用的是代码块。 这是我的代码: #include <stdio.h> int main() { int grilled_duck = 1; /*initializing grilled duck to 1 portion*/ int salad = 3; /*initializing sal

当我运行它并尝试结束循环时,我必须输入-1两次。 有人知道为什么吗? 我是一个初学者,所以我真的不知道代码出了什么问题。 我需要知道出了什么问题,因为编译器没有告诉我这个问题 我用的是代码块。 这是我的代码:

#include <stdio.h>

int main()
{
    int grilled_duck = 1; /*initializing grilled duck to 1 portion*/
    int salad = 3; /*initializing salad to 3 portions*/
    int rice = 4;  /*initializing rice to 4 portions*/
    int soup = 2; /*initializing soup to 2 portions*/
    int order; /*initializing order variable*/

    /*printing the menu on the screen and giving codes to every meal*/
    printf("  the menu:\n");
    printf("  %d portions of grilled duck (order code = 1).\n", grilled_duck);
    printf("  %d portions of salad (order code = 2).\n", salad);
    printf("  %d portions of rice (order code = 3).\n", rice);
    printf("  %d portions of soup (order code = 4).\n", soup);

    /*taking the orders from the user input using an event controlled loop
and decreasing the meals portion*/
    printf("  what would you like to order (to finish your order enter -1).\n");

    while(order!=-1)
    {
        scanf(" %d\n", &order);
        order=order;
        if(order==1 && grilled_duck!=0)
        {
            grilled_duck=grilled_duck-1;
            printf("what else?\n");
        }
        else if(order==2 && salad!=0)
        {
            salad=salad-1;
            printf("what else?\n");
        }
        else if(order==3 && rice!=0)
        {
            rice=rice-1;
            printf("what else?\n");
        }
        else if (order==4 && soup!=0)
        {
            soup=soup-1;
            printf("what else?\n");
        }
        else if(order==-1)
        {
            printf("thanks for ordering, your meal will be ready soon.\n");
        }
        else
        {
            printf("sorry we have any, anything else?\n");
        }
    }

    /*after taking the user orders the program print the updated menu*/
    printf("\n  the updated menu:\n");
    printf("  %d portions of grilled duck.\n", grilled_duck);
    printf("  %d portions of salad.\n", salad);
    printf("  %d portions of rice.\n", rice);
    printf("  %d portions of soup.\n", soup);

    return 0;
}
#包括
int main()
{
int grilled_duck=1;/*将烤鸭初始化为1份*/
int沙拉=3;/*初始化沙拉为3份*/
int rice=4;/*将大米初始化为4份*/
int soup=2;/*将汤初始化为2份*/
int-order;/*初始化订单变量*/
/*在屏幕上打印菜单并为每顿饭提供代码*/
printf(“菜单:\n”);
printf(“%d份烤鸭(订单代码=1)。\n”,烤鸭);
printf(“%d份沙拉(订单代码=2)。\n”,沙拉);
printf(“%d份大米(订单代码=3)。\n”,大米);
printf(“%d份汤(订单代码=4)。\n”,汤);
/*使用事件控制循环从用户输入获取命令
减少膳食的份量*/
printf(“您想要点什么(要完成订单,请输入-1)。\n”);
while(顺序!=-1)
{
scanf(“%d\n”、&order);
订单=订单;
如果(订单=1&&烤鸭!=0)
{
烤鸭=烤鸭-1;
printf(“还有什么?\n”);
}
否则如果(订单==2&&S沙拉!=0)
{
色拉=色拉-1;
printf(“还有什么?\n”);
}
否则如果(订单==3&&rice!=0)
{
rice=rice-1;
printf(“还有什么?\n”);
}
否则如果(订单==4&&soup!=0)
{
汤=汤-1;
printf(“还有什么?\n”);
}
否则如果(顺序==-1)
{
printf(“感谢订购,您的饭菜很快就会准备好。\n”);
}
其他的
{
printf(“对不起,我们还有其他的吗?\n”);
}
}
/*接受用户订单后,程序将打印更新的菜单*/
printf(“\n更新的菜单:\n”);
printf(“%d份烤鸭。\n”,烤鸭);
printf(“%d份沙拉。\n”,沙拉);
printf(“%d份大米。\n”,大米);
printf(“%d份汤。\n”,汤);
返回0;
}
问题在于:

scanf(" %d\n", &order);
格式字符串在
%d
后面包含一个换行符。这将导致在输入数字后输入的换行符被使用。因此,
scanf
仍在等待输入,然后返回

删除
%d
之后的尾随换行符(以及前导空格)。这将允许按ENTER键终止
scanf
调用

scanf("%d", &order);
问题在于:

scanf(" %d\n", &order);
格式字符串在
%d
后面包含一个换行符。这将导致在输入数字后输入的换行符被使用。因此,
scanf
仍在等待输入,然后返回

删除
%d
之后的尾随换行符(以及前导空格)。这将允许按ENTER键终止
scanf
调用

scanf("%d", &order);
改变这个

scanf(" %d\n", &order);

格式说明符中的尾随空白字符(
\n
)表示它在扫描
int
后忽略任何数量的空白。在scanf中您根本不需要任何空白字符,因为
%d
已经忽略了任何剩余的空白字符

另请注意,
订单
在第一次迭代期间未初始化。使用
do{..}while(..)循环可能更适合

另请参见:

更改此选项

scanf(" %d\n", &order);

格式说明符中的尾随空白字符(
\n
)表示它在扫描
int
后忽略任何数量的空白。在scanf中您根本不需要任何空白字符,因为
%d
已经忽略了任何剩余的空白字符

另请注意,
订单
在第一次迭代期间未初始化。使用
do{..}while(..)循环可能更适合


另请参见:

order=order
…em,什么?相反,为什么?
int顺序/*初始化订单变量*/
这绝对是错误的<代码>/*初始化订单变量*/
,不,您在这里声明该变量。但是不管怎样,删除那个注释是多余的,就像有这样的注释:
foo=bar;//将bar分配给foo
谢谢我很好地纠正了这些问题,但是平均问题呢?@ahmed11037练习:消除多次调用
printf(“还有什么?\n”)
order=order
…em,什么?相反,为什么?
int顺序/*初始化订单变量*/
这绝对是错误的<代码>/*初始化订单变量*/
,不,您在这里声明该变量。但是不管怎样,删除那个注释是多余的,就像有这样的注释:
foo=bar;//将bar分配给foo
谢谢我很好地纠正了这些问题,但是平均问题呢?@ahmed11037练习:消除多次调用
printf(“还有什么?\n”)