【问题标题】:While/Switch Statement weird outputWhile/Switch 语句奇怪的输出
【发布时间】:2011-10-18 04:46:49
【问题描述】:
#include <stdio.h>
#include <iostream>
using namespace std;

float cost, total;
bool loop(char item){
        switch (toupper(item)) {
            case 'A':
                cost = 4.25;        
                return true;
            case 'B':
                cost = 5.57;
                return true;
            case 'C':
                cost = 5.25;
                return true;
            case 'D':
                cost = 3.75;
                return true;
            case 'T':
                return false;
        }
        return true;
}

int main(){
        char item;
        do {
            printf("\nEnter Item Ordered [A/B/C/D] or T to calculate total:");
            scanf("%c", &item);
            total = total + cost;
        } while (loop(item)); 
        printf("Total Cost: $%f\n", total);
}

让我输出过程:

$ ./Case3.o 

Enter Item Ordered [A/B/C/D] or T to calculate total:a

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:b

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:a

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:t
Total Cost: $28.139999

为什么在第一次 printf 之后它打印了两次 printf 但第一次跳过了我的输入。那么如何计算 5.24+5.57+5.24 等于 28.14?

【问题讨论】:

  • 如果是C++,使用cout将输出写入流,cin接受输入。
  • 输入中的换行符是原因。见stackoverflow.com/questions/1247989/…
  • printf 让我觉得很花哨……
  • scanf("%c%*c", &amp;item);修复

标签: c++ switch-statement do-while


【解决方案1】:

enter 是一个按键 - 你需要考虑它:)

至于你的数学,你永远不会将total 初始化为0,因此初始值是不确定的。

没有注意范围 - 数学的真正答案是,当按下 enter 时,循环会重新添加先前的成本。这在 Mysticial 的回答中有所说明。

【讨论】:

  • 我不确定您对不确定的total 是否正确?至少在 C 中,静态变量被初始化为零。这在 C++ 中不一样吗?
  • 是的 - 我没有注意范围。真正的答案在另一个答案中详细说明,它重新添加了最后一个cost
【解决方案2】:

正如其他人所提到的,当您按 Enter 时,会输入两个字符,the character you enter + the newline,您需要考虑这两个字符。

可能的解决方案是:

方法 1:C 方式

 scanf(" %c", &item);
       ^^^

在这里加个空格,或者更好的办法,

方法 2:C++ 方式

只需使用 C++ 方式从用户那里获取输入。

cin >> item;

为什么结果是未定义的?
因为你没有初始化变量total,这会导致Undefined Behavior给你意外的输出。
total 是一个全局变量,因此它将默认初始化为 0.0。
未定义结果的真正原因在@Mystical 的回答中。

【讨论】:

  • 我可以使用scanf("%c%*c", &amp;item); 不过谢谢
  • @JohnRiselvato:确实,重要的是理解why?的一部分,然后可以有n的解决方案,很高兴你能理解:)
  • 希望有一天有人能回到这个问题上并发现您的解决方案很有帮助。干杯
  • 不,@John,说真的。要么是 C 程序员,要么是 C++ 程序员。没有中间地带:-)
  • @JohnRiselvato:你可以同时是 C 和 C++ 程序员,但不要在 C++ 中使用 C,它们都服务于并且设计用于本质上不同的目的。
【解决方案3】:

既然提到了newline,我来回答为什么28.14的另一个问题。

请注意,在您的开关中,默认值只是返回。 cost 从未设置。因此,当它读入 newline 时,它会跳过 switch 块并保持 cost 不变。

所以结果是这样的:

total = 0;  // It's actually undefined since you didn't initialize, but it probably started as zero.

total += 4.25;    //  For a
total += 4.25;    //  For '\n' after the 'a'

total += 5.57;    //  For b
total += 5.57;    //  For '\n' after the 'b'

total += 4.25;    //  For a
total += 4.25;    //  For '\n' after the 'a'

最终答案:28.14

最后输入的t 不会添加到total

【讨论】:

    【解决方案4】:

    这很容易解释。当您输入a 并按下ENTER 键时,这会将两个 字符放入输入缓冲区,即anewline 字符。

    这就是为什么除了第一个之外,您会得到一个虚假提示,因为它会打印它然后从标准输入中获取newline

    scanf 在 C++ 中确实是 C 兼容性的东西,您应该使用 cin &gt;&gt; something(或 任何 真正与流相关的东西)进行 C++ 风格的输入。

    这个字符的双重命中也解释了错误的总数,因为当您得到 newline 时,您将当前的成本值再次添加到您的主循环。

    您的总数由每个值中的两个组成,因为无论输入的值如何,您都添加了cost

    输入a,b,a,即4.25 + 5.57 + 4.25 = 14.07 - a4.25,而不是5.24。而28.14 正好是14.07 的两倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2015-06-19
      • 2015-08-05
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多