cin.get() 不工作
我今天写了这个简单的程序,但我发现 cin.get()
拒绝工作,除非有两个。有什么想法吗?
#include <iostream>
using namespace std;
int main(){
int base;
while ((base < 2) || (base > 36)){
cout << "Base (2-36):" << endl;
cin >> base;
}
string base_str = "0123456789abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < base; i++){
for (int j = 0; j < base; j++){
for (int k = 0; k < base; k++){
cout << base_str[i] << base_str[j] << base_str[k] << endl;
}
}
}
cin.get();
cin.get();
}
如果我将 cin.get() 移动到嵌套循环之前,循环将运行然后暂停。如果我取出一个 cin.get() ,程序就会结束。我正在使用最新版本的bloodshed c++ dev
I wrote this simple program today, but I found that cin.get()
refuses to work unless there are 2 of them. Any ideas?
#include <iostream>
using namespace std;
int main(){
int base;
while ((base < 2) || (base > 36)){
cout << "Base (2-36):" << endl;
cin >> base;
}
string base_str = "0123456789abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < base; i++){
for (int j = 0; j < base; j++){
for (int k = 0; k < base; k++){
cout << base_str[i] << base_str[j] << base_str[k] << endl;
}
}
}
cin.get();
cin.get();
}
if i move a cin.get()
to before the nested loops, the loops run then pause. if i take one cin.get()
out, the program just ends. im using the latest version of bloodshed c++ dev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有初始化“base”变量,但是虽然这会导致错误,但它与您在 cin 中看到的行为没有(直接)相关,尽管有时根据编译器的不同,它会导致您跳过循环。您可能正在以零初始化或其他方式的调试模式进行构建。
也就是说,假设这是固定的:
当您键入一个值(例如 5)并按 Enter 键时,流中的数据为
5
--operator<<不会从流中提取换行符,但 cin.get() 会提取换行符。您的第一个 cin.get() 从流中提取换行符,第二个 wait 等待输入,因为流现在为空。如果只有一个 cin.get() 调用,它将立即提取换行符并继续,并且由于 cin.get() 调用之后没有任何内容,程序将终止(正如它应该的那样)。看来您正在使用 cin.get() 来阻止程序在从调试器运行时关闭;您通常可以通过 IDE 中的特定“启动而不调试”命令来执行此操作;那么您就不需要为此目的滥用 cin.get() 。
You are not initializing the 'base' variable, but while that will cause bugs it isn't (directly) related to the behavior you're seeing with cin, even though it will sometimes, depending on the compiler, cause you to skip loops. You're probably building in debug mode that zero-initializes or something.
That said, assuming that was fixed:
When you type a value (say, 5) and hit enter, the data in the stream is
5<newline>
-- operator<< does not extract the newline from the stream, but cin.get() does. Your first cin.get() extracts that newline from the stream, and the second wait waits for input because the stream is now empty. If you had only the one cin.get() call, it would extract the newline immediately and continue, and since there is nothing after that cin.get() call, the program terminates (as it should).It seems that you're using cin.get() to stop your program from closing when run from the debugger; you can usually do this via a specific "start without debugging" command from your IDE; then you won't need to abuse cin.get() for this purpose.
变量
base
尚未初始化。您可以通过为
base
提供无效值来修复它:或者
更好地使用 do-while 循环作为:
您需要第二个
cin.get()
的原因是使用cin
读取base
值后,缓冲区中会留下\n
。第一次调用cin.get()
会消耗该\n
,第二次调用cin.get
会等待您的输入。为了避免这种情况,您需要在cin
之后通过调用cin.ignore
从缓冲区中刷新\n
Variable
base
has not been initialized.You can fix it by giving an invalid value to
base
as:or
better use a do-while loop as:
The reason why you need the 2nd
cin.get()
is that, after you read thebase
value usingcin
, a\n
is left in the buffer. The first call tocin.get()
consumes that\n
and the 2ndcin.get
waits for your input. To avoid this you need to flush the\n
from the buffer aftercin
by callingcin.ignore