English 中文(简体)
虽然“Switch 声明”的产出是多余的
原标题:While/Switch Statement weird output
#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("
Enter Item Ordered [A/B/C/D] or T to calculate total:");
            scanf("%c", &item);
            total = total + cost;
        } while (loop(item)); 
        printf("Total Cost: $%f
", 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

为什么是在第一次印刷版后印制的f。 第一次从投入中抽取我两次。 那么,它如何计算5.24+5.57+5.24到28.14平等?

最佳回答

正如其他人提到的那样,当你报到时,有两种特性得到了投入, 您进入+新线。 你们都需要对这两种情况负责。

可能的解决办法是:

<>Approach 1: The C way

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

增加一席之地或更好的办法,

<Approach 2: The C++ way

简单地利用C++从用户那里获取投入的途径。

cin >> item;

Why the result is Undefined?
Because you did not initialize the variable total, This results in Undefined Behavior giving you unexpected output.
total is a global so it will be Default Initialized to 0.0.
Real reason for Undefined result is in @Mystical s answer.

问题回答

www.un.org/Depts/DGACM/index_french.htm 因此,初始价值没有确定。

注意范围——数学的真正答案是,在<代码>enter<>/code>时,休息时间重新计算以前的费用。 这一点在我的解答中指出。

由于提到了<代码>newline,我回答另一个问题,即为什么28.14

通知说,在你的开关中,违约只是回来。 <代码>费用从未确定。 因此,如果在<条码>中读到<>新线>,它就绕过了开关,留下了不起的费用。

结果是:

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  
  after the  a 

total += 5.57;    //  For b
total += 5.57;    //  For  
  after the  b 

total += 4.25;    //  For a
total += 4.25;    //  For  
  after the  a 

最后答复:28.14

<代码>t, 输入最后一行, 添加到<条码>。

这一点很容易解释。 当你进入<代码>a并打到ENTER key时,该编号为 2<>>>>>>>> /em> 载于投入缓冲中的特性,anewline

因此,除第一种情况外,你之所以能迅速印制,然后从标准投入获得<条码>新<>>。

确实,在C++中,你应当使用<代码>cin >> 某些(或any of the streams-relatedpinuff really)来提供C++型投入。

由于你get/em>:newline,你在主食中增加目前的费用again<>。

Your total is composed of two of each value due to the fact that you re adding cost regardless of the value entered.

<代码>a,b,a,即4.25 + 5.57 + 4.25 = 14.07 - a:4.25,而不是5.24。 <代码>28.14准确为14.07两倍。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?