C++ 如何正确使用cin.peek()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13501862/
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
How to properly use cin.peek()
提问by Zzz
This function is supposed to read a fraction and place it in an array. If the user enters '0' the function is supposed to exit. I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow the user to exit.
这个函数应该读取一个分数并将它放在一个数组中。如果用户输入“0”,则该函数应该退出。我正在尝试使用 cin.peek() 函数执行此操作,但执行始终进入 if 语句并且不允许用户退出。
How should I properly code this (I am open to not using peek(), I thought it was the simplest way of doing it.)
我应该如何正确编码(我愿意不使用 peek(),我认为这是最简单的方法。)
Thanks!
谢谢!
void enterFrac(Fraction* fracs[], int& index)
{
int n, d;
char c, slash;
cout << "Enter fractions (end by entering a 0): ";
c = cin.peek();
if ( c != '0')
{
cin >> n >> slash >> d;
Fraction* f = new Fraction();
f->num = n;
f->den = d;
fracs[index] = f;
index++;
}
}
This test of peek() works however:
然而,这个 peek() 测试有效:
#include <iostream>
using namespace std;
int main () {
char c;
int n;
char str[256];
cout << "Enter a number or a word: ";
c=cin.peek();
if ( (c >= '0') && (c <= '9') )
{
cin >> n;
cout << "You have entered number " << n << endl;
}
else
{
cin >> str;
cout << " You have entered word " << str << endl;
}
return 0;
}
回答by Dietmar Kühl
There are two issues with your use of std::istream::peek()
:
您的使用有两个问题std::istream::peek()
:
- This function access the next character and does notskip leading whitespace. You probably want to skip leading whitespace before determining what the next character is, e.g., using the manipulator
std::ws
:(std::cin >> std::ws).peek()
. - The result from
std::istream::peek()
is not achar
. Instead, it is anstd::char_traits<char>::int_type
(which is a fancy spelling ofint
). The result may, e.g., bestd::char_traits<char>::eof()
and if the value of'0'
happens to be negative (I'm not aware of any platform where it is; however, e.g., the funny character from my name'ü'
is a negative value on platforms wherechar
is signed) you wouldn't get the correct result, either. That is, you normally compare the result ofstd::istream::peek()
against the result ofstd::char_traits<char>::to_int_type()
, i.e., you'd use something like this:std::cin.peek() == std::char_traits<char>::to_int_type('0')
- 这个函数访问下一个字符,也不会跳过前导空白。在确定下一个字符是什么之前,您可能想跳过前导空格,例如,使用操纵器
std::ws
:(std::cin >> std::ws).peek()
。 - 结果来自
std::istream::peek()
不是char
. 相反,它是一个std::char_traits<char>::int_type
(这是 的花哨拼写int
)。结果可能是,例如,std::char_traits<char>::eof()
如果 的值'0'
恰好为负(我不知道它所在的任何平台;但是,例如,我名字中的有趣字符'ü'
在char
已签名的平台上是负值)您也不会得到正确的结果。也就是说,您通常将 的结果std::istream::peek()
与 的结果进行比较std::char_traits<char>::to_int_type()
,即,您将使用以下内容:std::cin.peek() == std::char_traits<char>::to_int_type('0')
That said, your program doesn't check whether it could successfully read the nominator and the denominator, separated by a slash. You alwayswant to verify that reading was successful, e.g., using something like
也就是说,您的程序不会检查它是否可以成功读取以斜线分隔的分母和分母。您总是想验证读取是否成功,例如,使用类似
if ((std::cin >> nominator >> slash >> denominator) && slash == '/') {
...
}
Just for entertainment, you can create a manipulator for testing that a character is a slash, indeed:
只是为了娱乐,你可以创建一个操纵器来测试一个字符是斜线,确实:
std::istream& slash(std::istream& in) {
if ((in >> std::ws).peek() != std::char_traits<char>::to_int_type('/')) {
in.setstate(std::ios_base::failbit);
}
return in;
}
This way, you'd encapsulate the test for slash. If you need to use this in multiple places this is quite handy.
这样,您就可以封装斜线测试。如果您需要在多个地方使用它,这非常方便。