C++ Cin in a while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19307979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:42:12  来源:igfitidea点击:

Cin in a while loop

c++while-loopcin

提问by Protomega

So, I've looked around and haven't been able to figure out what's going on with cin during my While loop. I'm working through the book C++ Primer (5th edition) and I noticed that during one of the exercises I couldn't use cin to grab strings without it not terminating the while loop. I fixed this issue by just using getline().

所以,我环顾四周,无法弄清楚在我的 While 循环中 cin 发生了什么。我正在阅读《C++ Primer》(第 5 版)这本书,我注意到在其中一个练习中,我无法使用 cin 来抓取字符串而不终止 while 循环。我通过使用 getline() 解决了这个问题。

The goal of the current exercise is to ask for user input from the values of 0 - 15, and converting that number into it's "Hex Equivelant" (Where 0 = 1, 1 = 2, 2 = 3, ... , 10 = A, 11 = B). I tried doing this without the book and failed, but then started questioning the code in the book. Here's the code in the book:

当前练习的目标是要求用户输入 0 - 15 的值,并将该数字转换为“Hex Equivelant”(其中 0 = 1, 1 = 2, 2 = 3, ... , 10 = A, 11 = B)。我尝试在没有这本书的情况下执行此操作并失败了,但随后开始质疑书中的代码。这是书中的代码:

//Note: <iostream> and <string> have been included. using namespace std is used 
const string hexdigits = "0123456789ABCDEF";
cout << "Enter a series of numbers between 0 and 15"
     << " separated by spaces. Hit ENTER when finished: "
     << endl;
string result;
string::size_type n;

while(cin >> n)
    if (n < hexdigits.size())
        result += hexdigits[n];
cout << "Your hex number is: " << result << endl;

If I were to run this code, it would never terminate the while loop after hitting enter without entering any code (essentially giving the input of whitespace I would think?).

如果我要运行这段代码,它永远不会在不输入任何代码的情况下按 Enter 键后终止 while 循环(基本上是输入我认为的空格?)。

I'm here for two reasons:

我来这里有两个原因:

1) Why does this code not work? 2) I would appreciate a nudge in the right direction, but not the answer to how to get this code to execute correctly

1)为什么这段代码不起作用?2)我希望朝着正确的方向轻推,但不是如何正确执行此代码的答案

If I can't receive reason 1 without compromising reason 2, I would much rather have reason 1 be fulfilled.

如果我不能在不妥协原因 2 的情况下接受原因 1,我宁愿让原因 1 得到满足。

Quick-Edit: I apologize, I'm using Visual Studio 2012

快速编辑:抱歉,我使用的是 Visual Studio 2012

采纳答案by Chris Ryding

Hitting Enter produces either a CR or LF or both, depending on your platform. This is a valid input so satisfies the condition to continue the while loop. You will need to either explicitly test for these characters at the beginning of your input or use Ctrl-C to break out of the loop.

按 Enter 会产生 CR 或 LF 或两者,具体取决于您的平台。这是一个有效的输入,因此满足继续 while 循环的条件。您需要在输入开始时显式测试这些字符,或者使用 Ctrl-C 来跳出循环。

As a readability issue, I would include braces around the code that you want in the loop. What you have there is valid C++ since without braces the whilewill loop on the next statement and the whole ifconditional is a single statement. Practice putting them in now even on single line loops and you'll save yourself some headache debugging in the future.

作为可读性问题,我会在循环中所需的代码周围包含大括号。您拥有的是有效的 C++,因为没有大括号,while将在下一个语句上循环,并且整个if条件是单个语句。现在练习将它们放入单行循环中,将来您将省去一些麻烦的调试工作。

回答by Dietmar Kühl

As long as you enter valid numbers it will continue to read numbers. You can terminate the loop by entering something which isn't a number, e.g., "goodbye". At the beginning of a line, i.e., immediately after hitting enter you should also be able to terminate the standard input using Ctrl-Z (I think; I normally work on UNIX-like systems where you'd terminate the standard input using Ctrl-D).

只要您输入有效数字,它就会继续读取数字。您可以通过输入非数字内容来终止循环,例如“再见”。在一行的开头,即,在按下 Enter 键后,您还应该能够立即使用 Ctrl-Z 终止标准输入(我认为;我通常在类 UNIX 系统上工作,在这种系统中您会使用 Ctrl-终止标准输入) D)。

Essentially, a stream converts to trueas long as neither std::ios_base::failbitnor std::ios_base::badbitis set in its state (you can look at stream's state using stream.rdstate()). As long as you successfully read integers from the stream, it has no reason to go into failure state. When you enter something which isn't an integer, it will get std::ios_base::failbitset. Likewise, when it reaches the end of the stream.

本质上,true只要既std::ios_base::failbit没有std::ios_base::badbit设置也没有设置流的状态(您可以stream使用来查看的状态stream.rdstate()),流就会转换为。只要您成功地从流中读取整数,就没有理由进入失败状态。当您输入不是整数的内容时,它将被std::ios_base::failbit设置。同样,当它到达流的末尾时。

回答by Nandakumar Edamana

@Protomega, You can use the same code, but press Ctrl+D to stop input stream.

@Protomega,您可以使用相同的代码,但按 Ctrl+D 停止输入流。

回答by PExplorer

CTRL-D works on Linux. A simpler code snippet is below:

CTRL-D 适用于 Linux。一个更简单的代码片段如下:

vector<double> vd;
for (double d; cin>> d;)
     vd.push_back(d);
for(int i=0; i<vd.size(); ++i)
   cout << vd[i] << endl;

回答by Zac Howland

There is an easier way to do this:

有一种更简单的方法可以做到这一点:

const string hexdigits = "0123456789ABCDEF";
cout << "Enter a series of numbers between 0 and 15 separated by spaces. Hit ENTER when finished: " << endl;
string line;
string result;
if (getline(cin, line)) // get the whole line
{
    istringstream iss(result); // break them by spaces
    int i;
    while (iss >> i)
    {
        result += hexdigits[i];
        result += " ";
    }
    cout << "Your hex result:  " << result << endl;
}
else
{
    cout << "Error handling input!" << endl;
}

With your solution, you would need to press Ctrl-D to end the input.

对于您的解决方案,您需要按 Ctrl-D 来结束输入。