【问题标题】:How to loop until user input N in C如何循环直到用户在C中输入N
【发布时间】:2015-03-11 12:07:45
【问题描述】:

我是 C 程序的初学者,我正在尝试制作餐厅订单菜单。 我从用户输入“Y”开始订购。 然后我希望程序继续接受订单,直到用户输入“N”停止。 当输入“N”时,将打印总销售额。 但是我不能循环播放,你介意帮助我吗?谢谢你。 :)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int code;
float totalPrice=0, totalSales = 0 ;
char choice, choice1;

printf("Welcome to Deli Sandwich! Enter Y to start your order!\n");
scanf("%c", &choice);

while(choice=='Y'|| choice=='y')
{
    printf("\n____________________________SANDWICH FILLING______________________________\n");
    printf("\n\t\t Menu \t\t Code \t\t Price\n");
    printf("\n\t\t Egg \t\t 1 \t\t RM 1.00\n");
    printf("\n\t\t Tuna \t\t 2 \t\t RM 2.00\n");
    printf("\n\t\t Seafood \t 3 \t\t RM 3.00\n");
    printf("\n\t\t Chicken Ham \t 4 \t\t RM 2.50\n");

    printf("\nSandwich Filling code: ");
    scanf("%d", &code);

    switch(code)
    {
    case 1:
        printf("Egg is picked.\n");
        totalPrice+= 1;
        break;
    case 2:
        printf("Tuna is picked.\n");
        totalPrice+= 2;
        break;
    case 3:
        printf("Seafood is picked.\n");
        totalPrice+= 3;
        break;
    case 4:
        printf("Chicken Ham is picked.\n");
        totalPrice+= 2.50;
        break;
    default :
        printf("invalid code.");

    }

    printf("\n_____________________________SANDWICH TYPE________________________________\n");
    printf("\n\t\t Menu \t\t Code \t\t Price\n");
    printf("\n\t\t Half \t\t 1 \t\t RM 3.00\n");
    printf("\n\t\t Whole \t\t 2 \t\t RM 5.00\n");

    printf("\nSandwich Type code: ");
    scanf("%d", &code);

    switch(code)
    {
    case 1:
        printf("Half is picked.\n");
        totalPrice+= 3;
        break;
    case 2:
        printf("Whole is picked.\n");
        totalPrice+= 5;
        break;
    default :
        printf("invalid code.");

    }

    printf("\nThe total price is RM%.2f.\n", totalPrice);
    printf("Thank You. Please come again!\n");

    totalSales+= totalPrice;

    printf("\nWelcome to Deli Sandwich! Enter Y to start your order!\n");
    scanf("%c", &choice);

}

printf("\nThe total sales is RM%.2f.\n", totalSales);

return 0;

}

再次感谢您:)

【问题讨论】:

    标签: c loops input while-loop scanf


    【解决方案1】:

    改变

    scanf("%c", &choice);
    

    scanf(" %c", &choice); // note the space before %c
    

    这样做是为了丢弃所有空白字符,例如 \nstdin 中的空格。

    当您为scanf 输入数据时,您输入一些数据并按回车键scanf 使用输入的数据并将 \n(enter 键) 留在输入缓冲区 (stdin)。当下次调用带有%cscanf 时,它会将\n 作为输入(由之前的scanf 留下),并且不会等待进一步的输入。

    在您的代码中,

    scanf("%c", &choice);
    

    while 循环消耗您输入的字符并将\n 留在stdin 之前。至于为什么

    scanf("%d", &code);
    

    等待输入是%d 格式说明符会跳过空白字符,而%c 不会。

    【讨论】:

    • 哦是这样吗?非常感谢!
    【解决方案2】:
    scanf(" %c", &choice);
    

    通过在%c之前放置一个空格来忽略输入末尾的换行符

    【讨论】:

      【解决方案3】:

      简单的在 %c 前添加空格

      【讨论】:

        【解决方案4】:

        提供输入后的 ENTER 键被存储到输入缓冲区 stdin 并被认为是 %c有效 输入重复出现 @987654323 的格式说明符@s。为了避免扫描存储的\n,您需要更改您的代码,例如

        scanf(" %c", &choice);
              ^
              |
        

        此前导空格指示忽略任何前导空格或类似空格的字符(包括\n)并扫描第一个非空格字符。 [在你的情况下y/Y/n...]

        【讨论】:

          猜你喜欢
          • 2021-11-19
          • 2020-08-15
          • 1970-01-01
          • 2014-04-12
          • 2015-06-05
          • 2011-06-27
          • 2017-11-30
          • 1970-01-01
          相关资源
          最近更新 更多