cin>> val有时会读取0,具体取决于Ctrl-Z [英] cin >> val sometimes reads 0 depending on Ctrl-Z
问题描述
我试图使用MinGW编译器在Windows中用C ++编写代码,我的代码计算并打印给定输入集中出现次数的连续次数.代码如下:
I was trying to write a code in C++ in Windows using MinGW compiler, my code counts and prints the number of consecutive times a number occurs in a given set of input. The code is as follows:
#include <iostream>
int main()
{
int c_val = 0,val = 0,cnt = 1;
std::cin>>c_val;
while(std::cin>>val){
if(val==c_val)
cnt++;
else{
std::cout<<c_val<<" occurs "<<cnt<< " times"<<std::endl;
c_val = val;
cnt = 1;
}
}
std::cout<<val<<" occurs "<<cnt<<" times";
}
输入:42 42 42 12 13 13 ^Z
(按Enter)
INPUT: 42 42 42 12 13 13 ^Z
(press Enter)
输出:
42 occurs 3 times
12 occurs 1 times
0 occurs 2 times
但是如果我在^Z
之前按Enter,则它看起来像:
But if I press Enter before ^Z
then it looks like:
输入:42 42 42 12 13 13
(按Enter)^Z
(按Enter)
INPUT: 42 42 42 12 13 13
(press Enter) ^Z
(press Enter)
输出:
42 occurs 3 times
12 occurs 1 times
13 occurs 2 times
我想知道为什么我的代码中的变量val
在按下回车键后使用^Z
键时会存储13
的原因,以及如果我同时输入^Z
键为什么会存储0
的原因用我的输入.
I want to know why the variable val
in my code stores 13
, when I use ^Z
key after pressing my return key and why does it store 0
if I give the ^Z
key along with my input.
推荐答案
这是发生的情况.我使用MinGW-w64 4.9.2进行了观察.无论是在Windows控制台中还是在Cygwin(而不是使用cygwin-mingw)下运行可执行文件,其行为都是相同的.
Here is what happens. I observed this using MinGW-w64 4.9.2. The behaviour was the same whether running the executable in a Windows console, or under Cygwin (but not using cygwin-mingw).
- 在行首按
^Z
设置文件结束条件 - 在其他任何地方按
^Z
键实际上将ASCII 26字符发送到流中
- Pressing
^Z
at the beginning of a line sets the end-of-file condition - Pressing
^Z
anywhere else actually sends ASCII 26 character to the stream
我还观察到:
-
如果
-
cin >> val
由于输入不包含数字而失败,则将val
设置为0
.
如果由于文件结尾而导致输入失败,则 -
cin >> val
保留val
不变.
cin >> val
setsval
to0
if it fails due to the input not containing a number.cin >> val
leavesval
unchanged if input fails due to end-of-file.
根据此线程,它是由以下行为指定的正确行为C ++ 11.
According to this thread that is the correct behaviour specified by C++11.
因此您的结果可以得到解释.输入42 42 42 12 13 13^Z
时,与编写42 42 42 12 13 13x
相同.读取前六个数字,然后对x
进行加密时,cin >> val
失败,并将val
设置为0
.
So your results can be explained. When you input 42 42 42 12 13 13^Z
, it is the same as if you had written 42 42 42 12 13 13x
. The first six numbers are read, and then when x
is encoutered, cin >> val
fails and sets val
to 0
.
但是,当您按Enter键然后按^Z
键时,就好像您正在从文件中读取文件一样,并且已到达文件末尾. cin >> val
保留val
不变,并且仍保留上次成功执行cin >> val
之后的值.
But when you press Enter and then ^Z
, it is as if you were reading from a file and you reached the end of the file. cin >> val
leaves val
unchanged and it is still holding the value that it had after the last successful cin >> val
.
如果您按照Gautam Jha的建议进行更改,则在两种情况下都将得到13
.这是因为他有效地读取了临时int,然后仅在读取成功的情况下才将临时int存储到实数val
中,从而避免了失败的读取将val
设置为0
的情况.
If you make the change suggested by Gautam Jha suggested then you will get 13
in both cases. This is because he effectively reads into a temporary int, and then only stores the temporary int into the real val
if the read succeeded, thereby avoiding the behaviour where a failed read sets val
to 0
.
这可能是所需的行为,尽管您可能还想检查cnt > 0
以避免在输入完全为空的情况下出现奇怪的输出.
This is probably the desired behaviour although you might also want to check cnt > 0
to avoid a weird output in the case of totally empty input.
这篇关于cin>> val有时会读取0,具体取决于Ctrl-Z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!