Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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_Switch Statement - Fatal编程技术网

在C中的开关情况下选择默认值后,我似乎无法再次循环

在C中的开关情况下选择默认值后,我似乎无法再次循环,c,switch-statement,C,Switch Statement,大家好,我编写了肯德基菜单之类的代码,最后我让它工作了,但当我为菜单输入数字以外的东西时,例如:字母A,我无法让它再次循环到正常状态,相反,它完成了程序 #include <stdio.h> #include <stdlib.h> int main() { char counter='y'; float totalprice=0; while (counter=='Y' || counter=='y') { int

大家好,我编写了肯德基菜单之类的代码,最后我让它工作了,但当我为菜单输入数字以外的东西时,例如:字母A,我无法让它再次循环到正常状态,相反,它完成了程序

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

int main()
{
    char counter='y';
    float totalprice=0;

    while (counter=='Y'  ||  counter=='y')
    {
        int menu;
        float price=0;

        printf("\nplease select from menu:");
        scanf (" %i", &menu);

        switch(menu)
        {
          case  1: {
              printf("\none hotbox1 =RM10.50");
              totalprice=totalprice+10.50;
              break;
          }
          case   2: {
              printf ("\none hotbox2=RM10.60");
              totalprice=totalprice+10.60;
              break;
          }
          case   3:{
              printf ("\none hotbox3=RM10.70");
              totalprice=totalprice+10.70;
              break;
          }
          default : {
              printf ("\nplease enter proper number please:");
              scanf("%2f", &menu);
              break;
          }
        }

        printf("\n\nadd order?(Y/N):");
        scanf (" %c", &counter);
    }

    printf("\n\nThe total price is: %f", totalprice);
    return 0;
}
您应该首先使用fgets,然后使用sscanf,检查它的返回值以确定它是否是一个数字

char inputBuffer[MAX_BUFFER];

do
{
    fgets(inputBuffer, MAX_BUFFER, stdin);
}
while(sscanf(inputBuffer, "%d", &menu) != 1)
在默认情况下,您可以使用%f扫描,我很确定这是用于浮动的。使用%d.

删除扫描%2f,&菜单

当scanf%i,&菜单试图从输入中读取整数时,它会找到一个,它无法将其解释为数字,因此它不会读取它*。然后,下一个scanf继续从另一个scanf退出时读取输入,并愉快地读取字母“A”。因为只要读取的字母是“y”或“y”,而A两者都不是,就循环,所以它退出循环

*阅读scanf的文档,了解如何判断它是否遇到错误

注意:scanf%i,&菜单应为scanf%d,&菜单,因为%d`是整数的格式符号

一种解决方案是将循环条件更改为:

while (counter!='N' && counter!='n')
{
  ...
}
这样,只有在输入显式的“N”或“N”时,才结束循环


注意:如果您不小心为菜单项键入了“n”,则不会有任何帮助,因此请参阅上面有关错误处理的注释。

C中的Switch在Switch case中不支持char。在启动switch case之前,请验证用户输入。如果是一个数字,则进入开关盒,否则显示一条用户消息,仅输入数字值

我建议您通过在循环中的各个点(即读入计数器后,在循环底部等)打印计数器的值来调试此功能。。这将使您能够了解代码正在执行的操作。

您可以尝试类似的操作

#include <stdio.h>    
int main()
{
    char counter;
    float totalprice=0;
    int menu=0;
    do
    {
        printf("\n1. one hotbox1=RM10.50");
        printf("\n2. one hotbox2=RM10.60");
        printf("\n3. one hotbox3=RM10.70");
        printf("\nplease select from menu:");
        scanf ("%d", &menu);
        switch(menu)
        {
          case 1:
              printf("\none hotbox1 =RM10.50");
              totalprice=totalprice+10.50;
              break;
          case 2:
              printf ("\none hotbox2=RM10.60");
              totalprice=totalprice+10.60;
              break;
          case 3:
              printf ("\none hotbox3=RM10.70");
              totalprice=totalprice+10.70;
              break;
          default :
              printf ("\nplease enter proper number please:");
              scanf("%d", &menu);
        }
        printf("\n\nadd more order?(Y/N):");
        fflush(stdin);      //to empty the input stream.
        scanf("%c",&counter);
    }while(tolower(counter) != 'n'); //tolower returns the lowercase character.

    printf("\n\nThe total price is: %.2f", totalprice);
    return 0;
}

在你的案例陈述中不需要大括号。@ChristianTernus-但它有助于理解;有时,额外的作用域甚至会出现在手上。它应该出现在注释中。我想我理解你们的意思,但实际上它只是结束了我的整个程序,而不是循环返回。即使选择了默认值,还有什么方法可以循环返回?@Hafizzyazwan-首先尝试在菜单上输入字母“y”,看看它是否按照您的意愿循环返回。如果是这样的话,您需要理解为什么它在该输入上循环,而不是在“A”上循环。一旦您了解到您将能够更好地实施这些建议