C程序其他条款发生时不应该发生

时间:2017-03-27 22:32:52

标签: c while-loop

所以我是C(Java程序员)的新手,我不确定为什么会出现这种情况。

我正在模拟自动售货机,用户应该向终端输入一个字符作为硬币。以下是更好地解释的代码:

while(leftOverCost > 0){
  printf("Enter coin(nqrd): ");
  char coin;
  coin = getchar();
  if(coin=='N' || coin=='n'){
    printf("Nickle detected.\n");
    userTotal += 5;
    leftOverCost -= 5;
    printf("\tYou have inserted a total of %d cents.\n", userTotal);
    if(leftOverCost < 0) break;
    printf("\tPlease insert %d more cents.\n", leftOverCost);
  }
  else if(coin=='D' || coin=='d'){
    printf("Dime detected.\n");
    userTotal += 10;
    leftOverCost -= 10;
    printf("\tYou have inserted a total of %d cents.\n", userTotal);
    if(leftOverCost < 0) break;
    printf("\tPlease insert %d more cents.\n", leftOverCost);
  }
  else if(coin=='Q' || coin=='q'){
    printf("Quarter detected.\n");
    leftOverCost -= 25;
    userTotal += 25;
    printf("\tYou have inserted a total of %d cents.\n", userTotal);
    if(leftOverCost < 0) break;
    printf("\tPlease insert %d more cents.\n", leftOverCost);
  }
  else if(coin=='R' || coin=='r'){
    printf("bye\n");
    break;
  }
  else{
    printf("Unknown coin.");
  }
}

为什么这个else子句总是执行?我得到以下形式的输出:

    You have inserted a total of 25 cents.
        Please insert 30 more cents.
Enter coin (nqrd): Unknown coin rejected.

它不应该仅在用户输入我的代码无法识别的内容时执行吗?这与getchar()方法有关吗?当我输入无法识别的字符时,它会发生两次。

3 个答案:

答案 0 :(得分:0)

在C.

在您输入char后,由于“\ n”或“enter”命令,else正在执行。

你可以这样解决:

this

答案 1 :(得分:0)

标准函数function读取所有字符,包括空白字符。

而是使用函数getchar

scanf

scanf( " %c", coin );

答案 2 :(得分:0)

我花了一些时间,但我能解决你的问题。代码中的问题是break。我发现将break置于if/else语句中有点奇怪。我们通常使用break来摆脱循环。如果你摆脱它并修复内部if语句,那应该可以解决你的大部分问题。下一个问题是getchar()。在您的代码中使用这不是一个好主意,因此您应该尝试scanf代码。这样就容易多了。另外,在printf的开头添加scanfleftOverCost语句,因为它未设置。

代码:

#include <stdio.h>

main(){

  float leftOverCost = 345; //Remove '= 345' and add a printf and scanf statement to set the cost.
  float userTotal;
  while(leftOverCost > 0) {
                printf("Enter coin(nqrd): ");
                char coin;
                scanf(" %c", &coin);
                if(coin=='N' || coin=='n') {
                        printf("Nickle detected.\n");
                        userTotal += 0.05;
                        leftOverCost -= 0.05;
                        printf("\tYou have inserted a total of %f cents.\n", userTotal);
                        printf("%f", leftOverCost);

                        if(leftOverCost > 0){
                        printf("\tPlease insert %f more cents.\n", leftOverCost);
                        }
                }
                else if(coin=='D' || coin=='d') {
                        printf("Dime detected.\n");
                        userTotal += 0.10;
                        leftOverCost -= 0.10;
                        printf("\tYou have inserted a total of %f cents.\n", userTotal);

                        if(leftOverCost > 0){
                        printf("\tPlease insert %f more cents.\n", leftOverCost);
                        }

                }
                else if(coin=='Q' || coin=='q') {
                        printf("Quarter detected.\n");
                        leftOverCost -= 0.25;
                        userTotal += 0.25;
                        printf("\tYou have inserted a total of %f cents.\n", userTotal);

                        if(leftOverCost > 0){
                        printf("\tPlease insert %f more cents.\n", leftOverCost);
                        }

                }
                else if(coin=='R' || coin=='r') {
                        printf("bye\n");
                }
                else {
                printf("Unknown coin.");
                }
}
}

P.S,我稍微改了一下代码,以赚取更多十进制数字。四分之一是0.25美分。你理解我的意思。我使用float只保留小数。另外,添加一些if语句以查看leftOverCost是否小于零,它将提供饮料并进行任何更改。