While/Switch 语句奇怪的输出
#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 的呢?
#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);
}
Let me output the process:
$ ./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
Why is it after the first printf its printing the printf
twice but skipping me from input the first time. then how is it calculating 5.24+5.57+5.24 to equal 28.14?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
enter
是一个按键 - 你需要考虑它:)至于你的数学,你永远不会将total
初始化为0
因此初始值不确定。没有注意范围 - 数学的真正答案是,当按下
enter
时,循环会重新添加之前的成本。 Mysticial 的回答中指出了这一点。enter
is a keystroke - you need to account for it :)As for your math, you never initializetotal
to0
therefore the initial value is indeterminate.Wasn't paying attention to the scoping - the real answer for the math is that the loop re-adds the previous cost when
enter
is pressed. This is noted in Mysticial's answer.正如其他人提到的,当您按 Enter 时,会输入两个字符,
您输入的字符 + 换行符
,您需要考虑这两个字符。可能的解决方案是:
方法 1:C 方式
在此处添加一个空格,或者更好的方法,
方法 2:C++ 方式
只需使用 C++ 方式从用户获取输入。
为什么结果未定义?
因为您没有初始化变量total
,这会导致未定义行为给您带来意外的输出。total
是一个全局变量,因此它将默认初始化为 0.0。未定义结果的真正原因在@Mystical 的答案中。
As others have mentioned, When you press Enter, two characters get inputted,
the character you enter + the newline
, You need to account for both of these.Possible solutions are:
Approach 1: The C way
Add a space here, or the better approach,
Approach 2: The C++ way
simply use the C++ way of getting input from user.
Why the result is Undefined?
Because you did not initialize the variabletotal
, 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.
既然已经提到了
换行符
,我将回答为什么是28.14
的另一个问题。请注意,在您的 switch 中,默认值只是 return。
cost
永远不会被设置。因此,当它读入换行符时,它会跳过 switch 块并保持成本不变。结果是这样的:
最终答案:
28.14
最后输入的
t
不会添加到total
中。Since the
newline
has been mentioned, I'll answer the other question of why28.14
.Notice that in your switch, the default is just return.
cost
is never set. Therefore, when it reads in thenewline
it skips the switch block and leaves cost untouched.So the result is this:
Final answer:
28.14
The
t
that is entered last doesn't get added tototal
.这很容易解释。当您输入
a
并按ENTER
键时,这会在输入缓冲区中放置两个 个字符,即a
和换行符
字符。这就是为什么除了第一个之外的所有情况,您都会收到一个虚假的提示,因为它会打印它,然后从标准输入获取换行符。
scanf
实际上是 C++ 中的 C 兼容性问题,您应该使用cin >>>用于 C++ 样式输入的某些
(或者任何与流相关的内容)。这种字符的双重命中也解释了错误的总数,因为当您得到该
换行符
时,您再次添加当前的成本值在你的主循环中。由于无论输入的值如何,您都会添加
成本
,因此您的总计由每个值的两个组成。输入
a,b,a
后,结果为4.25 + 5.57 + 4.25 = 14.07
-a
为4.25
代码>,而不是5.24
。28.14
正好是14.07
的两倍。This is easily explained. When you enter
a
and hit theENTER
key, this places two characters in the input buffer, thea
and thenewline
character.That's why, for all but the first, you have a spurious prompt since it prints it and then gets the
newline
from standard input.scanf
is really a C compatibility thing in C++, you should be usingcin >> something
(or any of the streams-related stuff really) for C++-style input.This double hit of charcaters also explains the errant total as well since, when you get that
newline
in, you add the current value of cost again in your main loop.Your total is composed of two of each value due to the fact that you're adding
cost
regardless of the value entered.With your entry of
a,b,a
, that would be4.25 + 5.57 + 4.25 = 14.07
-a
is4.25
, not5.24
. And28.14
is exactly twice14.07
.